Interprocess Communication CSCI 8530 Advanced Operating Systems Pipes FIFOs Message Queues and Shared Memory Signals provide a limited mechanism by which processes can communicate In general however processes need more flexible schemes for communication In general some sort of synchronization is also required since a process may need to wait for communication from another before it proceeds Indeed in many cases the synchronization is all that is required Updated 11 27 2007 2 Mechanisms Summary of POSIX Mechanisms Numerous mechanisms exist for coordination of multiple processes including these Message passing Shared memory Counting semaphores Mutexes and condition variables Readers writers locks Tasking and rendezvous Event flags Name Reliable Flexible Fast Signals Sometimes Very No Messages Yes Not very Not particularly Semaphores Yes Very Very w shared memory Shared Memory Yes Very Very w semaphores 3 Signals 4 Message Passing These are normally used for tasks unrelated to communication or synchronization They can pass small limited messages between processes using real time signals only The interface is very different from that used for the other communication schemes Most applications are formulated in terms of messages between tasks Pipes and FIFOs are provided in POSIX 1 base and are a form of message passing POSIX 4 real time extension provides a more general message passing scheme Message queues can be made transparent to machine boundaries thus extending to an entire network 5 6 1 Shared Memory Synchronization Low level mechanism More difficult to use than signals or message queues Additional synchronization e g semaphores usually required for shared memory access Unlikely to work well with multiple machines on a network e g a distributed system Advantages very flexible and very fast POSIX counting semaphores basic implementation of typical semaphores System V semaphores much more general and bewildering File locking limited form of synchronization Message passing and signals can also be used for synchronization 7 8 POSIX 1 Pipes and FIFOs A pipe is a byte wide communication path between processes that are related e g parent and child Once created the interface is the same as for regular files read and write Creation is accomplished using the pipe system call int pipefd 2 pipe pipefd Pipes and FIFOs pipefd 0 file descriptor for reading pipefd 1 file descriptor for writing 10 Pipe Sharing Pipe Caveats Once created by a process a pipe must be shared with one or more child processes to obtain interprocess communication capabilities Recall that when a process forks all file descriptors open in the parent are also open in the child w some exceptions As a result a pipe must be created in the parent before the fork call that creates the child if the parent and the child are to communicate through the pipe The dup2 system call can be used to give explicit numbers to file descriptors so the pipe ends can have consistent numbers known to all participating processes 11 A child process must know the pipe file descriptors that is the numbers returned by the pipe system call created by the parent A read on a pipe will block if there is no data in the pipe There is no structure to the data in a pipe it is just a sequence of bytes Any structure must be inferred from the contents by the reader and agreed to by all processes that are communicating through the pipe Pipes are normally only one way communication channels Pipes cannot be used for communication between unrelated processes End of file on read is not returned unless 1 the pipe contains no data and 2 all write file descriptors are closed 12 2 FIFOs FIFOs are named pipes The name of a FIFO appears in the file namespace just like the name of a directory or regular file Once created a FIFO is opened just like a regular file using open with O RDONLY or O WRONLY A FIFO cannot be opened for reading and writing A FIFO is created using mkfifo char name mode t mode A FIFO is deleted using the unlink system call Writers or readers block unless at least one writer and reader have the FIFO open There is no way to control the size of a FIFO Message Queues 13 POSIX 4 Message Queues mq open name oflag Unlike pipes and FIFOs message queues support messages that have structure Like FIFOs message queues are persistent objects that must be initially created and eventually deleted when no longer required Message queues are created with a specified maximum message size and maximum number of messages Message queues are created and opened using a special version of the open system call mq open name Must start with a slash and contain no other slashes QNX puts these in the dev mqueue directory oflag O CREAT to create a new message queue O EXCL causes creation to fail if queue exists O NONBLOCK usual interpretation mode usual interpretation mqattr address of structure used during creation can be NULL 15 mq attr structure 16 mq close descriptor This structure pointed to by the last argument of mq open has at least the following members mq maxmsg maximum number of messages that may be stored in the message queue mq msgsize the size of each message in bytes mq flags not used by mq open but accessed by mq getattr and mq setattr mq curmsgs number of messages in the queue 17 This function is used to close a message queue after it has been used As noted earlier the message queue is not deleted by this call it is persistent The message queue s contents are not altered by mq close unless a prior call by this or another process called mq unlink see next slide In this respect an open message queue is just like an open file deletion is deferred until all open instances are closed 18 3 mq unlink name Message Queue Persistence This call is used to remove a message queue Recall from the previous slide that the deletion is deferred until all processes that have the message queue open have closed it or terminated It is usually a good practice to call mq unlink immediately after all processes that wish to communicate using the message queue have opened it In this way as soon as the last process terminates closing the message queue the queue itself is deleted As noted a message queue is persistent Unlike a FIFO however the contents of a message queue are also persistent It is not necessary for a reader and a writer to have the message queue open at the same time A writer can open or create a queue and write messages to it then close it and terminate Later a reader can
View Full Document
Unlocking...