You are on page 1of 17

IPC MECHANISMS

Linux Architecture
User process System libraries Kernel System call interface Input/output File systems Networking Device drivers Process Scheduler Memory manager Interprocess communication

Architecture dependent code

Message Passing
• • • • Pipes System V message queues Posix message queues RPC

Sharing Information
• That resides within the kernel • That resides within the user space

Pipes
• Process pipes • Pipes • Named pipes / FIFO

Process pipes
FILE *popen(const char *command, const char *open_mode); int pclose(FILE *stream_to_close); Command : name of the program to run open_mode: “r” or “w”

Pipes
int pipe(int file_descriptor[2])

It’s important to realize that these are file descriptors, not file streams, so we must use the lower-level read and write calls to access the data, rather than fread and fwrite

Named pipes/FIFO
It is a special type of file that exists as a name in the file system but behaves like the unnamed pipes.

Cont…
Command line: mkfifo <pathname> Or mknod <pathname> p Inside the program: int mkfifo(const char *filename, mode_t mode) int mknod(cosnt char *filename, mode_t mode | S_IFIFO,(dev_t)0)

Struct pipe_inode_info {
Struct wait_queue *wait; Char *base; // address of FIFO bounded buffer Unsigned int start; Unsigned int len; Unsigned int lock; Unsigned int rd_openers; Unsigned int wr_openers; Unsigned int readers; // currently reading Unsigned int writers; // currently writing

} • • Read/Write will not block if O_NONBLOCK is set in descriptor. Write operation is atomic (no intermixing of writes) as long as data does not exceed buffer size.

What can you do with a signal?
• Ignore it
– signal(sig_num, SIG_IGN) – SIGKILL and SIGSTOP can not be ignored

• Use the default behavior
– signal(sig_num, SIG_DFL) – by default, all signals presented except SIGCHLD and SIGURD finish the process

• Treat the signal with your own function
– after the signal, the process is continued from the point where it was interrupted.

Signal Rules
• After fork the child inherits the user defined behavior for all signals. • After execXX all signals are reset to their default behavior. • After a signal is treated it is reset to the default behavior. The programmer must attach her signal function again after each signal.
– Exception: SIGTRAP, SIGILL

Cont…
• If a signal is received during a system call, the system call exits with error and the signal is treated. • If a signal is not caught, it is lost for ever.
– Always attach your function to the signal in the first program lines. – Exception: SIGCHLD

signal
• int signal(int sig_num, void (*function(int)))
– sig_num - the signal number – function
• SIG_DFL - default behavior • SIG_IGN - ignore signal • user defined function

– return the address of the previous signal handler, or SIG_ERR if error

pause
• int pause(void)
– suspends a process until a signal is received – returns -1 after the signal is treated

alarm
• unsigned int alarm(unsigned int sec)
– generates an SIGALRM signal after sec seconds – returns 0 or the number of seconds from the previous alarm request

kill
• int kill(pid_t pid, int sig_num)
– sends a signal to a process or group of processes – pid
• if positive, the signal is sent to the process with the id pid • if it is zero, the signal is sent to all the process in the same group with the sender • if negative, the signal is sent to all the processes with the group id |pid|.

– sig_num
• signal to be sent

– returns 0 or -1, if error