Laboratory three tasked the student with implementing a classic problem in computer science, namely in synchronization. The “Producer-Consumer” problem or, the “bounded-buffer” problem is extremely important in understanding the fundamentals of multi-process synchronization. The problem uses two processes, a producer and a consumer. These two processes share a common buffer, of a fixed size. It is the job of the producer to churn out data items, piece by piece, into the buffer. And at the same time, the consumer is picking up that data, removing it from the buffer at the same rate – one piece at a time. Part of the problem lies in making sure the producer doesn’t add anything to the buffer if the buffer is full, and the consumer isn’t to take anything from the buffer if it is empty.
The solution calls for the producer to sleep, or stop producing if it is full, whereupon the next item removed by the consumer will wake up the sleeping producer to force it to push another item in the buffer. The same method applies to when the buffer is empty; as such the consumer shouldn’t be consuming anything and should go to sleep until the producer deposits a new item into the buffer. The solution provided uses semaphores, enabling inter-process communication.
A solution not implementing a semaphore will result in a race condition. (In this instance, we have only one producer and one consumer. Since it was my decision to code it this way, a mutex object would remedy the problem of a race condition with multiple producers and consumers. I chose to use semaphores instead, and my results are still correct). Race conditions arise from multiple, asynchronously executing threads that try to access a single object at the same time – producing the wrong result.
Comments are available in the code that will provide a full understanding of the program itself; however abstraction of the problem will be featured in this lab report. In the abstraction of this problem I had to...
Please join StudyMode to read the full document