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
5philosophers 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
P1will 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
P0comes first and take the left fork (f0), but before he could take the right fork (f1),P1came and take the left fork (f1), nowP0is waiting for the right fork (f1).
-
P0can getf1fork only ifP1getf2(right fork) and then executeeat()and putf1fork andf1become 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
5philosophers and forkS0,S1,S2,S3,S4 - All semaphore initialize with
1value.
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
P0come and if it want to take two fork then it requires two values of semaphore (S0,S1) - Then if
P1comes to get executed properly it also require two values of semaphore (S1,S2)
- Now suppose
P0want to eat so he executed thewait()operation and came into critical section and start eating (executingeat()) due to which the value ofS0andS1become0from1while other semaphore values at this point is1. - Suppose while
P0is in critical section andP1arrives and it requiresS1andS2but as we know the value ofS1is0soP1get blocked. - Now at the same time
P2also arrives and it requiresS2andS3value to be1to get into critical section, as the condition is true soP2will 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.
-
P0andP2are independent and do not have dependency becauseP0needf0andf1whileP2needsf2andf3, 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. -
P0arrives and it takes the left fork (f0) and get preempt. ThenP1arrives and take left fork (f1) and also get preempt. SimilarlyP2,P3andP4takef2,f3andf4fork (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 meansP4will 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
P3can take the right fork (f4) asS4is1already, and so on. By this we can overcome the deadlock state. - For
P4this 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






Top comments (0)