0% found this document useful (0 votes)
15 views2 pages

Wa0050.

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Wa0050.

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

classical problems of synchronization

In concurrent programming, the classical problems of synchronization are well-known


challenges that demonstrate typical issues faced when multiple processes or threads share
resources. These problems serve as models for understanding and implementing
synchronization techniques to avoid issues like race conditions, deadlocks, and starvation.

1. The Producer-Consumer Problem (Bounded Buffer Problem)

• 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).

2. The Dining Philosophers Problem

• 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:

o Allowing at most four philosophers to try eating simultaneously.

o Enforcing an ordering (e.g., odd-numbered philosophers pick up the left fork


first, even-numbered philosophers pick up the right fork first).

• Semaphores Used: One semaphore per fork.

3. The Readers-Writers Problem

• Scenario: A shared database allows multiple readers to access data simultaneously,


but only one writer should access it at a time.

• 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.

• Solution: Use semaphores to coordinate readers and writers:

o First Readers-Writers Problem: Prioritizes readers over writers, allowing


readers to access the resource as long as there is no writer.
o Second Readers-Writers Problem: Prioritizes writers, ensuring that once a
writer wants access, it does not wait indefinitely.

• Semaphores Used: mutex (for managing the number of readers), rw_mutex (for
exclusive writer access), and an integer count of readers.

4. The Sleeping Barber Problem

• 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.

• Semaphores Used: customers (count of waiting customers), barber (indicates barber’s


availability), and mutex (to manage access to the waiting room).

5. The Cigarette Smokers Problem

• 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.

• Semaphores Used: Three semaphores, one for each ingredient combination.

These problems highlight common synchronization challenges that arise in concurrent


programming. Solutions involve semaphores, mutex locks, and sometimes monitors to
coordinate access and ensure program correctness, efficiency, and fairness.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy