Rahul Mishra
Posted on December 6, 2020
This is a multipart blog article series, and in this series I am going to explain you the concepts of operating system. This article series is divided into multiple modules and this is the third module which consists of 10 articles.
In this article we will see about dining philosopher problem, various cases and there solutions in detail.
Dining philosopher problem
- To understand this problem letβs take an example.
- There is a dining table on which
5
philosophers are stetted and in between every philosopher there is a fork to eat the rice from rice bowl placed in center of the table. - Every philosopher has two state think and eat.
void philosopher(void) {
while(true) {
Thinking();
take_fork(i);
take_fork((i+1)%N);
EAT();
put_fork(i);
put_forl((i+1)%N);
}
}
Case 1:
- P0 comes.
- This will first complete the
thinking()
function, and then it will pick the left fork, which is in this case will bef0
(because i=0). And then it will take right fork which isf1
(because i=1). - Now after taking both fork it will start eating and
eat()
function will execute. - When eating is done (means
eat()
is executed completely) it will put left fork first and then the right fork.
- Now
P1
will come and do the same thing as done byP0
- This is the normal case in it every philosopher comes one by one and this program is fine.
Case 2:
- Suppose
P0
comes first and take the left fork (f0), but before he could take the right fork (f1),P1
came and take the left fork (f1), nowP0
is waiting for the right fork (f1).
-
P0
can getf1
fork only ifP1
getf2
(right fork) and then executeeat()
and putf1
fork andf1
become free. - So, if more than one philosopher came at same time then it will cause race condition.
- Now to remove the race condition we use the concept of semaphore (binary semaphore)
- We will use array of semaphore, five semaphore as we have
5
philosophers and forkS0
,S1
,S2
,S3
,S4
- All semaphore initialize with
1
value.
void philosopher(void) {
while(true) {
Thinking();
wait(take_fork(Sci + 1)%N);
EAT();
signal(put_fork(i));
signal(out_fork(i+1)%N);
}
}
- Now when
P0
come and if it want to take two fork then it requires two values of semaphore (S0
,S1
) - Then if
P1
comes to get executed properly it also require two values of semaphore (S1
,S2
)
- Now suppose
P0
want to eat so he executed thewait()
operation and came into critical section and start eating (executingeat()
) due to which the value ofS0
andS1
become0
from1
while other semaphore values at this point is1
. - Suppose while
P0
is in critical section andP1
arrives and it requiresS1
andS2
but as we know the value ofS1
is0
soP1
get blocked. - Now at the same time
P2
also arrives and it requiresS2
andS3
value to be1
to get into critical section, as the condition is true soP2
will successfully enter the critical section and start eating (executeeat()
) - But as we know that first condition of process synchronization is mutual exclusion accordingly to which at a time only one process can enter critical section. But this Dining philosopher problem is a special case for that.
-
P0
andP2
are independent and do not have dependency becauseP0
needf0
andf1
whileP2
needsf2
andf3
, Due to which more than one process can enter critical section (because both process are independent.)
Case 3:
- Suppose value of all semaphore is initialized with
1
. -
P0
arrives and it takes the left fork (f0) and get preempt. ThenP1
arrives and take left fork (f1) and also get preempt. SimilarlyP2
,P3
andP4
takef2
,f3
andf4
fork (left fork) respectively. And all the processes get preempt just before they were going to pick right fork. - Now every philosopher had taken the left fork but no one have the right fork. Value of all semaphore is
0
- Now every process is blocked and they cannot unblock if they try they get blocked again because value of all semaphore
0
. This is the Dead lock condition - The only solution to this problem is that we can change the sequence of fork of
P4
. Which meansP4
will first will take the right fork instead of left fork (f0) and then left fork instead of right fork (f4)
- So, when the P4 comes it cannot take any fork and get blocked due to which
P3
can take the right fork (f4) asS4
is1
already, and so on. By this we can overcome the deadlock state. - For
P4
this will be the sequence of entry section code.
Wait(take fork(S(i+1)%N)
Wait(take fork(Si)
So this was all about dining philosopher problem. Hope you liked it and learned something new from it.
If you have any doubt, question, quires related to this topic or just want to share something with me, then please feel free to contact me.
π± Contact Me
Twitter
LinkedIn
Telegram
Instagram
π§ Write a mail
π Other links
π πͺ π
π©
Rahul Mishra
Posted on December 6, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.