Information Technology Reference
In-Depth Information
Producer
Consumer
write
read
User
Kernel
Pipe/Kernel Buffer
Figure3.9: Interprocess communication between a producer application and
a consumer.
3.4.1
Producer-consumer communication
Figure 3.9 illustrates how two processes communicate through the operating
system in a producer-consumer relationship. Via the shell, we establish a pipe
between the producer and the consumer. As one process computes and produces
a stream of output data, it issues a sequence of write system calls on the pipe
into the kernel. Each write can be of variable size. Assuming there is room
in the kernel buffer, the kernel copies the data into the buffer, and returns
immediately back to the producer.
At some point later, the operating system will schedule the consumer process
to run. (On a multicore, the producer and consumer could be running at the
same time.) The consumer issues a sequence of read calls. Because the pipe is
just a stream of bytes, the consumer can read the data out in any convenient
chunking | the consumer can read chunks in 1KB chunks, while the producer
wrote its data in 4KB chunks, or vice versa. Each system call read made by
the consumer returns the next successive chunk of data out of the kernel buffer.
The consumer process can then compute on its input, sending its output to the
display, a file, or onto the next consumer.
The kernel buffer allows each process to run at its own pace. There is no
requirement that each process have equivalent amounts of work to do. If the
producer is faster than the consumer, the kernel buffer fills up, and when the
producer tries to write to a full buffer, the kernel stalls the process until there
Search WWH ::




Custom Search