Skip to content

Latest commit

 

History

History
22 lines (14 loc) · 1.81 KB

README.md

File metadata and controls

22 lines (14 loc) · 1.81 KB

The producer-consumer problem revisited

  • The supplied OSCP_A2.c file presents the solution to the multi-process synchronization problem called the Producer-consumer problem.

  • The essence of the problem is as follows: the producer and the consumer share the same initially fixed-size buffer. The producer is on charge of generating data that will be added to the buffer, whereas the consumer removes that data from the buffer one by one. To get an access for "entering" this buffer, both - producer and consumer - has to wait for the specific sign (i.e., the state of buffer which controls by the function sem_wait) to prevent producer adding data to already full buffer or consumer trying to remove data from the empty buffer.

  • As the solution, the semaphore approach was used: two semaphores (namely, sem_empty and sem_full) control the state of buffer, then waiting and awakening threads (pthread_mutex_lock and pthread_mutex_unlock, subsequently) "tell" the producer or the consumer if it is the time to proceed further actions (is the mutex locked or unlocked). In main function pthread_create and pthread_join functions are used to start a new thread in the calling process and then make the calling thread wait till the newly created thread returns.

  • The following pseudocode was used as a base:

pseudocode

  • To run the program you have to execute the file named Makefile.txt in a following way:
make -f Makefile.txt
./A2 no.of Producers no.of Consumers
  • Here is an example of the programm execution where the produced and consumed items can be checked:

example