In computer science, operating systems
are modelled to support multiprocessing to increase efficiency. Scheduling, concurrency
and resource allocation are core factors of Operating system efficiency. This
article examines operating system problems that emerge as result of these
factors. It is acknowledged that this complexity involves programming but this
article will not dive into programming details.
The Producer Consumer Problem
This problem is associated with the
multi-process synchronization. It demonstrates the need to synchronize systems
when several processes share a resource. In the problem, two processes share a
single fixed size buffer (Single resource). One process generates some data/information
and puts it in the fixed buffer, and starts again (Producer). Simultaneously, the
remaining process utilises or consumes the data/information by removing it from
the fixed buffer one piece at a time (Consumer). The problem is that these
process do not take turns accessing the buffer, instead, they work
concurrently. In that, if the producer tries to put piece of data into the
buffer and the buffer is full, a problem will occur. On the contrary, if the
consumer tries to take an item from an empty buffer, a problem will occur.
Grand puts it plainly, The problem is to make sure that the producer won't try
to add data into the buffer if it's full and that the consumer won't try to
remove data from an empty buffer (Grand, 2002). In order synchronization to
happen, using programming logic we should block the producer when the buffer
is full and block the consumer when the buffer is empty. However, you must
reactivate them as soon as the status changes from full to not full / empty to
not empty so that they continue with their work otherwise a deadlock will
emerge. Adam Donlin explains that, “In short, the Producer relies on the
Consumer to make space in the data-area so that it may insert more information
whilst at the same time, the Consumer relies on the Producer to insert
information into the data area so that it may remove that information. It
therefore follows that a mechanism is required to allow the Producer and
Consumer to communicate so that they know when it is safe to attempt to write
or read information from the data-area”.
Solutions
The solution for the producer is to
either go to sleep or discard data if the buffer is full. The next time the
consumer removes an item from the buffer, it notifies the producer, who starts
to fill the buffer again. In the same way, the consumer can go to sleep if it
finds the buffer to be empty. The next time the producer puts data into the
buffer, it wakes up the sleeping consumer. The solution can be reached by means
of inter-process communication, typically using semaphores. An inadequate
solution could result in a deadlock where both processes are waiting to be
awakened. The problem can also be generalized to have multiple producers and
consumers (Wikipedia, 2013). This problem can be handled using semaphores, monitors
and message passing. Further details check links below.
References
Mark Grand Patterns in Java, Volume
1, A Catalog of Reusable Design Patterns Illustrated with UML