Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()

Similar presentations


Presentation on theme: "Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()"— Presentation transcript:

1 Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()

2 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

3 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

4 Unnamed Pipes Oldest form of IPC in UNIX Historically they are half duplex (uni- directional) Unnamed pipes can only be used between processes with a common ancestor Typically a process forks after creating an unnamed pipe

5 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] filedes[1] pipe

6 Unnamed Pipes Calling fork after calling pipe gives us filedes[0] filedes[1] filedes[0] filedes[1] pipe parentchild

7 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]

8 Unnamed Pipes Black arrow – parent to child Red arrow – child to parent filedes[0] filedes[1] filedes[0] filedes[1] parentchild pipe

9 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

10 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

11 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

12 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 3 rd parameter is ignored for FIFOs Ex: mknod(“myfifo”, S_IFIFO | 0666, 0);

13 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

14 popen / pclose pclose closes standard I/O stream, waits for the command to terminate and returns the exit status of the shell


Download ppt "Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()"

Similar presentations


Ads by Google