rabbitekk312

Untitled

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