Skip to content

One producer and n consumers problem (m data elements and shared memory)

License

Notifications You must be signed in to change notification settings

PetropoulakisPanagiotis/producer-consumer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License: MIT

Producer-Consumer Problem


One producer and n consumers with shared memory

In this simulation, one producer sends m elements to n consumers. These elements are stored in a shared memory segment with a capacity of one element. Each element contains a random int and a timestamp. At the end of this simulation, each consumer writes its pid, the m elements and its running average time in a shared results.txt file.

Note: running average time = the time needed to collect all the m items.

How It Works

Semaphores:

  • full:      [0, n], initial value is 0
  • empty: [0, n], initial value is 0
  • mutex:  0||1,  initial value is 1(for the shared results.txt file)

Operations:

  • semDown(semaphore): Reduce semaphore's value by one
  • semOpN(semaphore, value): Set semaphore's value
  • semWaitUntilZero(semaphore): Block process till semaphore's value becomes zeros

Producer:
Publish a new element to the shared memory segment, set(semOpN) full and empty semaphores to n and wait(semWaitUntilZero) empty semaphore to become zero(wait all consumers to read the current element).
Repeat this process m times.

Consumers:
Reduce(semDown) full semaphore, get an element from the shared memory segment, wait(semWaitUntilZero) full semaphore to become zero(wait all consumers to read the current element), and reduce(semDown) the empty semaphore.
Repeat this process m times.

Note: When the empty semaphore becomes zero, the producer adds a new element into the shared memory segment.

Compilation

  1. make
  2. ./test

Author

About

One producer and n consumers problem (m data elements and shared memory)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published