This is the 26th day of my participation in the 18th Gwen Challenge. For details, see: the last Gwen Challenge 2021

1. Problem description around a round table, five philosophers sitting round table 5 stool, respectively on the round table with five bowl chopsticks 5, they are thinking and dine alternates, when philosophers hungry when he was trying to get around the most close to his chopsticks, only he got two chopsticks to eat, keep thinking about finished eating and put down the chopsticks.

2. Analysis of five philosophers have only five chopsticks, while

  • Methods a
Semaphore chopstick [5] =,1,1,1,1 {1}; P (chopstick[I]); // pick up the left chopstick; // Pick up the chopsticks on the right to eat... v(chopstick[i]); // put down your chopsticks.Copy the code

Suppose we five philosophers all picked up the left chopstick and waited for the right one, which would cause a deadlock.

  • Method 2

Since 5 people eating with chopsticks might cause a deadlock, let’s just let 4 people eating with chopsticks break the deadlock so we add a semaphore eat

Semaphore chopstick [5] =,1,1,1,1 {1}; Semaphore eat = 4; semaphore eat = 4; semaphore eat = 4; p(eat) p(chopstick[i]); // pick up the left chopstick; // Pick up the chopsticks on the right to eat... v(chopstick[i]); // put down your chopsticks. v(eat)Copy the code
  • Methods three

Can be restrictions on the number It can also limit philosopher must be about chopsticks can take can take (which means to take on both only take to a can’t take two don’t get it So we will need to set up a mutex mutex = 1)

Semaphore chopstick [5] =,1,1,1,1 {1}; semaphore mutex = 1; p(mutex) p(chopstick[i]); // pick up the left chopstick; // Pick up the chopsticks on the right to eat... v(chopstick[i]); // put down your chopsticks. v(mutex)Copy the code
  • Methods four

The philosopher with the base number is not the same as the philosopher with the even number, so let’s say that when the philosopher is hungry, he takes the left chopstick first and then the right chopstick, and when five philosophers are hungry, they all take the left chopstick and there’s a deadlock as in method one, Suppose we let the base number pick the left chopstick first and then the right even number pick the right first and then the left just to avoid this deadlock. (Anyway, the geven takes chopsticks first and can’t follow them.)

Semaphore chopstick [5] =,1,1,1,1 {1}; // If (I mod 2! = 0){// base p(chopstick[I]); // pick up the left chopstick; // Pick up the chopsticks on the right to eat... v(chopstick[i]); // put down your chopsticks. }else{ p(chopstick[i+1]%5); // pick up the right chopstick p(I); // Pick up the chopsticks on the left to eat... v(chopstick[i]); // put down your chopsticks. }Copy the code

Method 2 – method is a modified principle proposed by method 1 deadlock, but first we need to know that the philosopher’s core operation of eating is to get two pairs of chopsticks in order to eat, which is represented by an array of chopsticks as a mutex