Lectur 7 Synchronizaton Problems
Lectur 7 Synchronizaton Problems
1
Producer-Consumer Problem
• Producer-consumer problem is a common paradigm for cooperating processes.
Producer process produces information that is used by the consumer
process.
• Shared buffer used that is filled by the producer and emptied by the consumer
concurrently.
• Must be synchronized so that the consumer does not try to consume an item that
has not yet been produced. Consumer has to wait for the producer
• Unbounded buffer places no particular limit on the size of the buffer
• Bounded buffers assumes that there is a fixed buffer size
Shared Buffer
Consume
Producer
r 2
Producer-Consumer Problem
Producer Consumer
while (TRUE) { while (TRUE) {
// produce an item while (count == 0);
nextProduced = ...
nextConsumed = buffer[out];
while (count == BUFFER_SIZE); out = (out + 1)% BUFFER_SIZE;
count--;
buffer[in] = nextProduced
in = (in + 1)% BUFFER_SIZE; //consume item nextConsumed
count++ }
}
3
Classical Problems of Synchronization
• Classical problems used to test newly-proposed synchronization schemes
• Bounded-Buffer Problem
• Readers-Writers Problem
• Dining-Philosophers Problem
4
Bounded-Buffer Problem
Producer Consumer
while (TRUE) { while (TRUE) {
// produce an item wait(full);
nextProduced = ... wait(mutex);
semaphore mutex = 1 5
semaphore full = 0 // 0 slots are full
semaphore empty = N // All slots are empty initially
Bounded-Buffer Problem
Full
BUF_SIZE
Producer
Sleeps
0 time
Consumer
Sleeps
6
Bounded-Buffer Problem
BUF_SIZE
times
all items consumed (Ct)
7
Readers-Writers Problem
• A data set is shared among a number of concurrent processes
• Readers – can only read the data set; they do not perform any updates
• Writers – can both read and write.
• Problem – allow multiple readers to read at the same time. Only one single
writer can access the shared data at the same time.
• Several variations of how readers and writers are considered – all involve some
form of priorities
rea
rea writ
der
der er
wait(rd_mutex);
reader_count--;
if(reader_count == 0) // Last Reader
signal(rw_mutex);
signal(rd_mutex);
}
9
int reader_count = 0
semaphore rd_mutex = 1 // protect reader_count variable
semaphore rw_mutex = 1 // protects the dataset
Readers-Writers Problem Variations
• The solution in previous slide can result in a situation where a writer process
never writes. It is referred to as the “First reader-writer” problem.
• No reader kept waiting unless writer has permission to use shared object
• No reader should wait for other readers to finish just because a writer is waiting
• Writers may starve
10
Dining-Philosophers Problem
• N philosophers’ sit at a round table with a bowel of rice in the middle.
• Problem – They spend their lives alternating thinking and eating. They do not
interact with their neighbors. Occasionally try to pick up 2 chopsticks (one at a
time) to eat from bowl. Need both to eat, then release both when done
(Chopsticks are like resources). While a philosopher is holding a chopstick,
another one can not have it.
a resource
a process
11
Dining-Philosophers Problem
• Is not a real problem, but a lot of real resource allocation problems look like
this. If we can solve this problem effectively and efficiently, we can also solve
the real problems.
12
Dining-Philosophers Problem
semaphore chopstick[N] = 1
semaphore mutex = 1
while (TRUE) {
// think for a while
wait(mutex);
wait(chopstick[i]);
wait(chopstick[(i+1)%N]);
signal(mutex);
signal(chopstick[i]);
signal(chopstick[(i+1)%N]);
} 13
Dining-Philosophers Problem Variations
• Allow to pick up the chopsticks only if both are available (pick up must be in
critical section)
• An odd philosopher picks up first his left chopstick and then his right, whereas
an even philosopher picks up his right chopstick first and then his left.
14