This document discusses three classic synchronization problems: the bounded buffer problem, readers-writers problem, and dining philosophers problem. It provides details on each problem, including how they can be modeled and potential solutions using semaphores to ensure mutual exclusion and avoid deadlock or starvation. For the bounded buffer problem, it describes a producer-consumer scenario using mutex, full, and empty semaphores. For the dining philosophers problem, it presents a solution where each philosopher picks up chopsticks in a specific order to eat then releases them to think again in a loop.
3. CS420:
Operating
Systems
3
The buffer pool has a maximum size, this problem is often called
the Bounded buffer problem.
This problem is generalised in terms of the Producer Consumer problem,
where a finite buffer pool is used to exchange messages between
producer and consumer processes.
Solution to this problem is, creating two counting semaphores "full" and
"empty" to keep track of the current number of full and empty buffers
respectively.
In this Producers mainly produces a product and consumers consume the
product, but both can use of one of the containers each time.
The main complexity of this problem is that we must have to maintain the
count for both empty and full containers that are available.
4. CS420:
Operating
Systems
4
• In a generalized Bounded Buffer problem, N buffers, each can
hold one data item
• Utilize three semaphores to control access to buffer between the
producer and the consumer
- Semaphore mutex locks access to critical region of code
where buffer is modified
• Initialized to the value 1
- Semaphore full keeps track of how many items are actually in the
buffer
• Initialized to the value 0
- Semaphore empty keeps track of how many available slots there
are in the buffer
• Initialized to the value N
5. CS420:
Operating
Systems
5
an item
do {
....
// produce
....
// inited to N
wait (empty);
wait (mutex);
....
buffer
// add
....
signal
signal
item to the
(mutex);
(full);
} while (TRUE);
inited to 0
do {
....
wait (full); // full
wait (mutex);
....
// remove item from buffer
....
signal (mutex);
signal (empty);
....
// consume the item
....
} while (TRUE);
Producer Process Consumer Process
6. CS420:
Operating
Systems
6
The dining philosopher's problem involves
the allocation of limited resources to a
group of processes in a deadlock-free and
starvation-free manner.
There are five philosophers sitting around
a table, in which there are five
chopsticks/forks kept beside them and a
bowl of rice in the centre, When a
philosopher wants to eat, he uses two
chopsticks - one from their left and one
from their right. When a philosopher
wants to think, he keeps down both
chopsticks at their original place.
7. CS420:
Operating
Systems
8
• Possible Solution??
- Instruct each philosopher to behave as follows:
Think until the left chopstick is available; when it is pick it up
Think until the right chopstick is available; when it is pick it up
Eat some rice
Put the left chopstick
down Put the right
chopstick down Go
back to thinking
8. CS420:
Operating
Systems
9
5] );
% 5] );
do {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) %
...
// eat
...
signal ( chopstick[i] );
signal ( chopstick[ (i + 1)
...
// think
...
} while (TRUE);