13.07.2015 Views

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Inter-process communication 299for writing. Pipes are constructed by a service call like this:CreatePipe. This call opens a new pipe and returns two descriptors for it. One isthe read descriptor, and the other is the write descriptor.The Read and Write operations available on files work just as well for pipes. <strong>An</strong>y informationwritten to the pipe is later available by reading from the pipe. Of course, the kernelmay wish to buffer only a limited amount of data for each pipe, so Write may blockthe caller if the pipe is too full. Likewise, Read will block the caller if the pipe is empty.In these cases, the Hysteresis Principle says that on a multiprocessor, it may be better towait a while before process switching. The pipe may become nonfull or nonempty soon,and the process switch can be avoided.Pipes are inherited by children. Figure 9.1 shows how a process A first creates apipe, then calls Split, becoming process B and C. Process B then closes the read end ofthe pipe, and C closes the write end, leaving a simple one-way communication linkbetween them.If the children B and C fail to close the appropriate ends of the pipe, confusionmay result. In Figure 9.2 we show a situation where first B writes 6 bytes on the pipe,then another 4 bytes. Next, C reads 5 bytes, and finally B reads 4 bytes. B will end upmissing the first 5 bytes, and C will never be able to read the next 4 bytes. One byteremains unread. The rule is that whichever process reads the next bytes gets them.We can enhance pipes in two ways. First, we can give them the broadcast property,which says that any data written to the pipe are saved until all the readers haveAABCBCFigure 9.1 Building a pipeB writesB writesunwritten0 1 2 3 4 5 6 7 8 9 10C reads B reads unreadFigure 9.2 Reads and writes on a pipe

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!