csenotes12

Program for Dining Philosophers Problem in C

May 1st, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3.  
  4. #define n 4
  5.  
  6. int compltedPhilo = 0,i;
  7.  
  8. struct fork{
  9.     int taken;
  10. }ForkAvil[n];
  11.  
  12. struct philosp{
  13.     int left;
  14.     int right;
  15. }Philostatus[n];
  16.  
  17. void goForDinner(int philID){ //same like threads concept here cases implemented
  18.     if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
  19.         printf("Philosopher %d completed his dinner\n",philID+1);
  20.     //if already completed dinner
  21.     else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
  22.             //if just taken two forks
  23.             printf("Philosopher %d completed his dinner\n",philID+1);
  24.  
  25.             Philostatus[philID].left = Philostatus[philID].right = 10; //remembering that he completed dinner by assigning value 10
  26.             int otherFork = philID-1;
  27.  
  28.             if(otherFork== -1)
  29.                 otherFork=(n-1);
  30.  
  31.             ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; //releasing forks
  32.             printf("Philosopher %d released fork %d and fork %d\n",philID+1,philID+1,otherFork+1);
  33.             compltedPhilo++;
  34.         }
  35.         else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){ //left already taken, trying for right fork
  36.                 if(philID==(n-1)){
  37.                     if(ForkAvil[philID].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
  38.                         ForkAvil[philID].taken = Philostatus[philID].right = 1;
  39.                         printf("Fork %d taken by philosopher %d\n",philID+1,philID+1);
  40.                     }else{
  41.                         printf("Philosopher %d is waiting for fork %d\n",philID+1,philID+1);
  42.                     }
  43.                 }else{ //except last philosopher case
  44.                     int dupphilID = philID;
  45.                     philID-=1;
  46.  
  47.                     if(philID== -1)
  48.                         philID=(n-1);
  49.  
  50.                     if(ForkAvil[philID].taken == 0){
  51.                         ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
  52.                         printf("Fork %d taken by Philosopher %d\n",philID+1,dupphilID+1);
  53.                     }else{
  54.                         printf("Philosopher %d is waiting for Fork %d\n",dupphilID+1,philID+1);
  55.                     }
  56.                 }
  57.             }
  58.             else if(Philostatus[philID].left==0){ //nothing taken yet
  59.                     if(philID==(n-1)){
  60.                         if(ForkAvil[philID-1].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
  61.                             ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
  62.                             printf("Fork %d taken by philosopher %d\n",philID,philID+1);
  63.                         }else{
  64.                             printf("Philosopher %d is waiting for fork %d\n",philID+1,philID);
  65.                         }
  66.                     }else{ //except last philosopher case
  67.                         if(ForkAvil[philID].taken == 0){
  68.                             ForkAvil[philID].taken = Philostatus[philID].left = 1;
  69.                             printf("Fork %d taken by Philosopher %d\n",philID+1,philID+1);
  70.                         }else{
  71.                             printf("Philosopher %d is waiting for Fork %d\n",philID+1,philID+1);
  72.                         }
  73.                     }
  74.         }else{}
  75. }
  76.  
  77. int main(){
  78.     for(i=0;i<n;i++)
  79.         ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;
  80.  
  81.     while(compltedPhilo<n){
  82.         /* Observe here carefully, while loop will run until all philosophers complete dinner
  83.         Actually problem of deadlock occur only thy try to take at same time
  84.         This for loop will say that they are trying at same time. And remaining status will print by go for dinner function
  85.         */
  86.         for(i=0;i<n;i++)
  87.             goForDinner(i);
  88.         printf("\nTill now num of philosophers completed dinner are %d\n\n",compltedPhilo);
  89.     }
  90.  
  91.     return 0;
  92. }
Add Comment
Please, Sign In to add comment