Lec 13 Classical Synchronization Problem
Lec 13 Classical Synchronization Problem
SYSTEM:
ACSE0403A
OUTLINE
Consumer: On the consumer's end, the value of count got decreased by 1 at each consumption. If the
buffer is empty at any point of time then the consumer will sleep otherwise, it keeps consuming the
items and decreasing the value of count by 1.
The consumer will be waked up by the producer if there is at least 1 item available in the buffer which
is to be consumed. The producer will be waked up by the consumer if there is at least one slot available
in the buffer so that the producer can write that.
Limitation of Producer and Consumer: Well, the problem arises in the case when the
consumer got preempted just before it was about to sleep. Now the consumer is
neither sleeping nor consuming. Since the producer is not aware of the fact that
consumer is not actually sleeping therefore it keep waking the consumer while the
consumer is not responding since it is not sleeping.
This leads to the wastage of system calls. This leads to deadlock where neither
producer nor consumer is active and waiting for each other to wake them up
1. PRODUCER-CONSUMER PROBLEM
Producer code
Consumer code
1. PRODUCER-CONSUMER PROBLEM
Producer code
Consumer code
1. PRODUCER-CONSUMER PROBLEM
Producer code
A is removed now
After removal of A, Buffer look like
Where out = 1, and in = 6
Consumer code
SOLUTION FOR PRODUCER-CONSUMER PROBLEM
Producer Code- solution Consumer Code- solution
Let's understand the Dining Philosophers Problem with the below code, we have used
fig 1 as a reference to make you understand the problem exactly. The five
Philosophers are represented as P0, P1, P2, P3, and P4 and five chopsticks by C0, C1,
C2, C3, and Ca4.
Let's discuss the code given:
RACE CONDITION IN DINING PHILOSOPHER’S
Suppose Philosopher P0 wants to eat, it
will enter in Philosopher() function, and
execute take_chopstick[i]; by doing
this it holds C0 chopstick after that it
execute take_chopstick[ (i+1) %
5]; by doing this it holds C1
chopstick( since i =0, therefore (0 + 1)
% 5 = 1)
Similarly suppose now Philosopher P1
wants to eat, it will enter in Philosopher()
function, and
execute take_chopstick[i]; by doing
this it holds C1 chopstick after that it
execute take_chopstick[ (i+1) %
5]; by doing this it holds C2
chopstick( since i =1, therefore (1 + 1)
% 5 = 2)
But Practically Chopstick C1 is not
available as it has already been taken by
philosopher P0, hence the above code
SOLUTION TO THE DINING
PHILOSOPHERS PROBLEM