Advertisement
joineyn901

3

Nov 13th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<semaphore.h>
  3. #include<pthread.h>
  4. //Number of Philosophers
  5. #define M 5
  6. //Think
  7. #define THINK 0
  8. //Hungry
  9. #define HUNGRY 1
  10. //Eat
  11. #define EAT 2
  12. //Left chopstick
  13. #define LEFTCHOP (phNum+4)%M
  14. //Right chopstick
  15. #define RIGHTCHOP (phNum+1)%M
  16. //Semaphore variables
  17. sem_t ourMutex;
  18. sem_t S[M];
  19. //Declare the needed methods
  20. void* diningPhilospher(void* number);
  21. void takeFork(int);
  22. void putFork(int);
  23. void tester(int);
  24. //Declare the needed array
  25. int state[M];
  26. int philNum[M] = { 0,1,2,3,4 };
  27. //Driver
  28. int main()
  29. {
  30. //Declare the needed variable
  31. int j;
  32.  
  33. //Declare a thread
  34. pthread_t threadID[M];
  35.  
  36. //Initialize
  37. sem_init(&ourMutex, 0, 1);
  38.  
  39. //Loop
  40. for (j = 0;j < M;j++)
  41.  
  42. //Initialize
  43. sem_init(&S[j], 0, 0);
  44.  
  45. //Loop
  46. for (j = 0;j < M;j++)
  47. {
  48. //Create thread
  49. pthread_create(&threadID[j], NULL, diningPhilospher, &philNum[j]);
  50.  
  51. //Display message
  52. printf("Philosopher %d is thinking\n", j + 1);
  53. }
  54.  
  55. //Loop
  56. for (j = 0;j < M;j++)
  57.  
  58. //Thread
  59. pthread_join(threadID[j], NULL);
  60. }
  61. //Method definition diningPhilospher()
  62. void* diningPhilospher(void* number)
  63. {
  64. //Loop
  65. while (1)
  66. {
  67. //Initialize
  68. int* j = number;
  69.  
  70. //Function call
  71. takeFork(*j);
  72.  
  73. //Function call
  74. putFork(*j);
  75. }
  76. }
  77. //Method definition
  78. void takeFork(int phNum)
  79. {
  80. //Wait
  81. sem_wait(&ourMutex);
  82.  
  83. //Update state
  84. state[phNum] = HUNGRY;
  85.  
  86. //Display
  87. printf("Philosopher %d is Hungry\n", phNum + 1);
  88.  
  89. //Function call
  90. tester(phNum);
  91. sem_post(&ourMutex);
  92.  
  93. //Wait
  94. sem_wait(&S[phNum]);
  95. }
  96. //Method definition
  97. void tester(int phNum)
  98. {
  99. //Condition check
  100. if (state[phNum] == HUNGRY && state[LEFTCHOP] != EAT && state[RIGHTCHOP] != EAT)
  101. {
  102. //State
  103. state[phNum] = EAT;
  104. //Display
  105. printf("Philosopher %d takes fork %d and %d\n", phNum + 1, LEFTCHOP + 1, phNum + 1);
  106.  
  107. //Display
  108. printf("Philosopher %d is Eating\n", phNum + 1);
  109. sem_post(&S[phNum]);
  110. }
  111. }
  112. //Method definition
  113. void putFork(int phNum)
  114. {
  115. //Wait
  116. sem_wait(&ourMutex);
  117.  
  118. //State
  119. state[phNum] = THINK;
  120.  
  121. //Display
  122. printf("Philosopher %d putting fork %d and %d down\n", phNum + 1, LEFTCHOP + 1, phNum + 1);
  123.  
  124. //Display
  125. printf("Philosopher %d is thinking\n", phNum + 1);
  126.  
  127. //Function call
  128. tester(LEFTCHOP);
  129.  
  130. //Function call
  131. tester(RIGHTCHOP);
  132. sem_post(&ourMutex);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement