Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose() InterProcess Communication (IPC) Anonymous Pipe FIFO queues Message queues Shared memory with Semaphores Sockets STREAMS See table on page 496 for which types of IPC are supported on which platforms IPC The uni-directional (half duplex) pipe and named pipe (FIFOs) are supported on all flavors of UNIX All of these forms of IPC except for sockets and STREAMS require both processes to be on the same machine Unnamed Pipes Oldest form of IPC in UNIX Historically they are half duplex (unidirectional) Unnamed pipes can only be used between processes with a common ancestor Typically a process forks after creating an unnamed pipe Unnamed Pipes int pipe(int filedes[2]); filedes[0] opened for reading filedes[1] opened for writing Writes to filedes[1] appear at filedes[0] for reading filedes[0] pipe filedes[1] Unnamed Pipes Calling fork after calling pipe gives us parent child filedes[0] filedes[0] pipe filedes[1] filedes[1] Unnamed Pipes We must choose whether a pipe sends messages from the parent to the child or from the child to the parent Messages from parent to child Parent closes filedes[0] Child closes filedes[1] Messages from child to parent Parent closes filedes[1] Child closes filedes[0] Unnamed Pipes Black arrow – parent to child Red arrow – child to parent parent child filedes[0] filedes[0] pipe filedes[1] filedes[1] Unnamed Pipes Reading from an empty pipe that has its write end closed returns 0 to indicate EOF Writing to a pipe when the read end has been closed generates SIGPIPE. Ignoring the signal, or returning from the handler causes the corresponding write to return an error with errno set to EPIPE FIFOs (named pipes) int mkfifo(const char *pathname, mode_t mode); pathname is a valid pathname in the filesystem mode same as for open function This only creates the pipe, we must still call the open function on it FIFOs (named pipes) If O_NONBLOCK is specified Open for read only returns immediately Open for write only returns -1 with errno set to ENXIO if no process has the FIFO open for reading If O_NONBLOCK not specified Open for read only blocks until another process opens the FIFO for writing Open for write only blocks until another process opens the FIFO for reading FIFOs (named pipes) On most systems mkfifo calls mknod int mknod(const char *pathname, mode_t mode, dev_t dev); can create new files of type S_IFIFO, S_IFBLK, S_IFCHR Only root can create device files Anyone can create FIFOs dev_t contains major and minor numbers of device file. This 3rd parameter is ignored for FIFOs Ex: mknod(“myfifo”, S_IFIFO | 0666, 0); popen / pclose FILE *popen(const char *command, const char *type); int pclose(FILE *stream); popen Creates a pipe, forks, closes un-needed ends of pipe, execs a shell to run command and waits for command to finish type can be either “r” to read from child’s stdout or “w” to write to child’s stdin FILE* returned is the created pipe popen / pclose pclose closes standard I/O stream, waits for the command to terminate and returns the exit status of the shell