DOC PREVIEW
UNO CSCI 8530 - Pipes, FIFOs, Message Queues, and Shared Memory

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CSCI 8530Advanced Operating SystemsPipes, FIFOs, Message Queues, and Shared MemoryUpdated: 11/27/20072Interprocess CommunicationSignals 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.3MechanismsNumerous mechanisms exist for coordination of multiple processes, including these:Message passingShared memoryCounting semaphoresMutexes and condition variablesReaders/writers locksTasking and rendezvousEvent flags4Summary of POSIX MechanismsVery (w/ semaphores)VeryYesShared MemoryVery (w/ shared memory)VeryYesSemaphoresNot particularlyNot veryYesMessagesNoVerySometimesSignalsFast?Flexible?Reliable?Name5SignalsThese are normally used for tasks unrelated to communication or synchronizationThey can pass small limited messages between processes (using real time signals only)The interface is very different from that used for the other communication schemes6Message PassingMost applications are formulated in terms of messages between tasksPipes and FIFOs are provided in POSIX.1 (base), and are a form of message passingPOSIX.4 (real time extension) provides a more general message passing schemeMessage queues can be made transparent to machine boundaries, thus extending to an entire network27Shared MemoryLow-level mechanismMore difficult to use than signals or message queuesAdditional synchronization (e.g. semaphores) usually required for shared memory accessUnlikely to work well with multiple machines on a network (e.g. a distributed system)Advantages: very flexible and very fast8SynchronizationPOSIX counting semaphores – basic implementation of typical semaphoresSystem V semaphores – much more general and bewilderingFile locking – limited form of synchronizationMessage passing and signals can also be used for synchronizationPipes and FIFOs10POSIX.1 – Pipes and FIFOsA 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);pipefd[0] = file descriptor for readingpipefd[1] = file descriptor for writing11Pipe SharingOnce created by a process, a pipe must be shared with one or more child processes to obtain interprocesscommunication 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 beforethe 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.12Pipe CaveatsA child process must know the pipe file descriptors (that is, the numbers returned by the pipesystem call) created by the parent.A readon 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.313FIFOsFIFOs 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 openwith O_RDONLY or O_WRONLY).A FIFO cannot be opened for reading and writing.A FIFO is created usingmkfifo(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 Queues15POSIX.4 Message QueuesUnlike 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.16mq_open(name,oflag,…)nameMust start with a slash and contain no other slashesQNX puts these in the /dev/mqueue directoryoflagO_CREAT – to create a new message queueO_EXCL – causes creation to fail if queue existsO_NONBLOCK – usual interpretationmode – usual interpretation&mqattr – address of structure used during creation (can be NULL)17mq_attr structureThis 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 queuemq_msgsize – the size of each message, in bytesmq_flags – not used by mq_open, but accessed by mq_getattr and mq_setattrmq_curmsgs – number of messages in the queue18mq_close(descriptor)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.419mq_unlink(name)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_unlinkimmediately 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.20Message Queue PersistenceAs 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


View Full Document

UNO CSCI 8530 - Pipes, FIFOs, Message Queues, and Shared Memory

Download Pipes, FIFOs, Message Queues, and Shared Memory
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Pipes, FIFOs, Message Queues, and Shared Memory and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Pipes, FIFOs, Message Queues, and Shared Memory 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?