0% found this document useful (0 votes)
10 views14 pages

Lec 13 Classical Synchronization Problem

Uploaded by

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

Lec 13 Classical Synchronization Problem

Uploaded by

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

OPERATING

SYSTEM:
ACSE0403A
OUTLINE

 1. Bounded-buffer (or Producer-Consumer) Problem,


 2. Dining-Philosophers Problem,
1. PRODUCER-CONSUMER PROBLEM

 Bounded buffer problem or producer-consumer problem is a classical synchronization


problem where we have a buffer with n cells or n slots and there are 2 process
producers and consumers can produce and consume one article at a time.

 We need to write a solution using a semaphore to ensure overflow condition for


producers underflow for consume and a critical section for the buffer.
EXPLANATION FOR PRODUCER-CONSUMER PROBLEM
Producer: The producer produces the item and inserts it into the buffer. The value of the global
variable count got increased at each insertion. If the buffer is filled completely and no slot is available
then the producer will sleep, otherwise it keep inserting.

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

• "in" used in a producer code


represent the next empty
buffer
• "out" used in consumer code
represent first filled buffer
• count keeps the count number
of elements in the buffer
Consumer code • count is further divided into 3
lines code represented in the
block in both the producer and
consumer code
1. PRODUCER-CONSUMER PROBLEM
Producer code

Consumer code
1. PRODUCER-CONSUMER PROBLEM
Producer code

After insertion of F, Buffer looks like th


Where out = 0, but in = 6

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

1.void producer( void )


1.void consumer(void)
2.{
2.{
3. wait ( empty );
3. wait ( empty );
4. wait(S);
4. wait(S);
5. Produce_item(item P)
5. itemC = buffer[ out ];
6. buffer[ in ] = item P;
6. out = ( out + 1 ) mod n;
7. in = (in + 1)mod n
7. signal(S);
8. signal(S);
8. signal(empty);
9. signal(full);
9.}
10.
11.}
2. DINING PHILOSOPHER’S PROBLEM
 The dining philosopher's problem is the classical problem of
synchronization which says that Five philosophers are sitting around a
circular table and their job is to think and eat alternatively. A bowl of
noodles is placed at the center of the table along with five chopsticks
for each of the philosophers. To eat a philosopher needs both their right
and a left chopstick. A philosopher can only eat if both immediate left
and right chopsticks of the philosopher is available. In case if both
immediate left and right chopsticks of the philosopher are not available
then the philosopher puts down their (either left or right) chopstick and
starts thinking again.

 The dining philosopher's problem involves the allocation of limited


resources to a group of processes in a deadlock-free and starvation-free
manner
FIVE PHILOSOPHERS SITTING AROUND THE TABLE

 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

 We can use a semaphore to represent a chopstick and


this truly acts as a solution of the Dining Philosophers
Problem. Wait and Signal operations will be used for
the solution of the Dining Philosophers Problem, for
picking a chopstick wait operation can be executed
while for releasing a chopstick signal semaphore can
be executed.
Thank
You

You might also like