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

Interprocess Communication

  • Upload
    eshe

  • View
    37

  • Download
    4

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Interprocess Communication

Interprocess Communication

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

Page 2: Interprocess Communication

InterProcess Communication (IPC)

Anonymous Pipe FIFO queues Message queues Shared memory with Semaphores Sockets STREAMSSee table on page 496 for which types of IPC are supported on which platforms

Page 3: Interprocess Communication

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

Page 4: Interprocess Communication

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

Page 5: Interprocess Communication

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 readingfiledes[0]

filedes[1]

pipe

Page 6: Interprocess Communication

Unnamed Pipes

Calling fork after calling pipe gives us

filedes[0]

filedes[1]

filedes[0]

filedes[1]

pipe

parent

child

Page 7: Interprocess Communication

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]

Page 8: Interprocess Communication

Unnamed Pipes

Black arrow – parent to child Red arrow – child to parent

filedes[0]

filedes[1]

filedes[0]

filedes[1]

parent

child

pipe

Page 9: Interprocess Communication

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

Page 10: Interprocess Communication

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

Page 11: Interprocess Communication

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

Page 12: Interprocess Communication

FIFOs (named pipes) On most systems mkfifo calls mknodint 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);

Page 13: Interprocess Communication

popen / pcloseFILE *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

Page 14: Interprocess Communication

popen / pclose

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