Ex6: Simultaneous Productor and Consumer - miguelneto1123/CES-22 GitHub Wiki

The producer-consumer problem is a well-known example of multi-process synchronization problem. It is described as synchronization problem over a fixed size data buffer implemented as queue being modified by two or more different processes referred as producers and consumers. For a data buffer, we can have multiple number of producers and consumers. Producers task is to keep generating data and place it into buffer while Consumers task is to keep consuming data from buffer. Problem is to ensure that Producers do not add data into buffer if its full and Consumer do not consumer data if buffer is empty. In this example, Readers are the consumers which read data from the Buffer and print it to the console, while Writers are the producers which read data from a file then write it to the Buffer. The Buffer class is implemented like a circular queue, and the synchronization of the put(char x) and get() makes the problem work even if there are multiple consumers and producers. Using synchronized methods is one of the ways to do, the other one is implementing the BlockingDeque interface to the buffer, which produces the same results