Wa0050.
Wa0050.
• Scenario: A shared buffer has a limited capacity. A producer process generates data
and puts it into the buffer, while a consumer process takes data from the buffer.
• Problem: The producer should not add data to the buffer if it is full, and the consumer
should not take data if the buffer is empty.
• Solution: Use semaphores to keep track of the buffer’s empty and full slots. A mutex
(binary semaphore) ensures only one producer or consumer accesses the buffer at a
time.
• Semaphores Used: empty (counts empty slots), full (counts filled slots), and mutex
(mutual exclusion).
• Scenario: Five philosophers sit at a round table with five forks (one between each pair).
Each philosopher either thinks or eats. To eat, a philosopher needs both forks adjacent
to them.
• Problem: If each philosopher picks up the fork on their right simultaneously, they will all
wait indefinitely for the fork on their left, leading to a deadlock.
• Solution: Use semaphores to represent each fork, with each philosopher requiring two
forks before they can eat. Solutions to avoid deadlock include:
• Problem: If a writer is writing, no readers should access the data, and vice versa. The
challenge is to manage access so that readers don’t starve writers or vice versa.
• Semaphores Used: mutex (for managing the number of readers), rw_mutex (for
exclusive writer access), and an integer count of readers.
• Scenario: A barber shop has a barber and a waiting area with limited chairs. When a
customer arrives and the barber is free, the customer gets a haircut. If the barber is
busy, the customer waits. If the waiting room is full, the customer leaves.
• Problem: Synchronize customers and the barber to ensure the barber only cuts hair
when there are customers, and customers wait if the barber is busy.
• Solution: Use semaphores to manage the waiting room capacity and barber’s
availability.
• Scenario: Three smokers each have one of three ingredients needed to make a
cigarette: tobacco, paper, or matches. An agent places two of the three ingredients on
the table. The smoker with the third ingredient makes the cigarette and smokes, after
which the agent places a new pair of ingredients.
• Problem: Coordinate the smokers and the agent so that the smoker with the missing
ingredient recognizes they can make a cigarette.
• Solution: Use semaphores to signal smokers when the correct ingredients are on the
table, ensuring each smoker only proceeds when they can make a cigarette.