fork download
  1.  
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include <semaphore.h>
  5. #include <unistd.h>
  6. #define N 5
  7. #define THINKING 0
  8. #define HUNGRY 1
  9. #define EATING 2
  10. int state[N];
  11. sem_t mutex;
  12. sem_t S[N];
  13. void test(int i)
  14. {
  15. if (state[i] == HUNGRY &&
  16. state[(i + 4) % N] != EATING &&
  17. state[(i + 1) % N] != EATING)
  18. {
  19. state[i] = EATING;
  20. printf("Philosopher %d takes forks and starts eating\n", i);
  21. sem_post(&S[i]);
  22. }
  23. }
  24. void take_forks(int i)
  25. {
  26. sem_wait(&mutex);
  27. state[i] = HUNGRY;
  28. printf("Philosopher %d is HUNGRY\n", i);
  29. test(i);
  30. sem_post(&mutex);
  31. sem_wait(&S[i]);
  32. }
  33. void put_forks(int i)
  34. {
  35. sem_wait(&mutex);
  36. state[i] = THINKING;
  37. printf("Philosopher %d puts down forks and starts THINKING\n", i);
  38. test((i + 4) % N);
  39. test((i + 1) % N);
  40. sem_post(&mutex);
  41. }
  42. void *philosopher(void *num)
  43. {
  44. int i = *(int *)num;
  45. for (int j = 0; j < 2; j++)
  46. {
  47. printf("Philosopher %d is THINKING\n", i);
  48. sleep(1);
  49. take_forks(i);
  50. sleep(1);
  51. printf("Philosopher %d finishes eating\n", i);
  52. put_forks(i);
  53. }
  54. return NULL;
  55. }
  56. int main()
  57. {
  58. pthread_t thread[N];
  59. int id[N];
  60. sem_init(&mutex, 0, 1);
  61. for (int i = 0; i < N; i++)
  62. {
  63. state[i] = THINKING;
  64. sem_init(&S[i], 0, 0);
  65. }
  66. for (int i = 0; i < N; i++)
  67. {
  68. id[i] = i;
  69. pthread_create(&thread[i], NULL, philosopher, &id[i]);
  70. }
  71. for (int i = 0; i < N; i++)
  72. {
  73. pthread_join(thread[i], NULL);
  74. }
  75. sem_destroy(&mutex);
  76. for (int i = 0; i < N; i++)
  77. sem_destroy(&S[i]);
  78. return 0;
  79. }
  80.  
  81.  
  82.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Philosopher 4 is THINKING
Philosopher 3 is THINKING
Philosopher 2 is THINKING
Philosopher 1 is THINKING
Philosopher 0 is THINKING
Philosopher 4 is HUNGRY
Philosopher 4 takes forks and starts eating
Philosopher 3 is HUNGRY
Philosopher 2 is HUNGRY
Philosopher 2 takes forks and starts eating
Philosopher 1 is HUNGRY
Philosopher 0 is HUNGRY
Philosopher 4 finishes eating
Philosopher 4 puts down forks and starts THINKING
Philosopher 0 takes forks and starts eating
Philosopher 4 is THINKING
Philosopher 2 finishes eating
Philosopher 2 puts down forks and starts THINKING
Philosopher 3 takes forks and starts eating
Philosopher 2 is THINKING
Philosopher 4 is HUNGRY
Philosopher 2 is HUNGRY
Philosopher 0 finishes eating
Philosopher 0 puts down forks and starts THINKING
Philosopher 1 takes forks and starts eating
Philosopher 0 is THINKING
Philosopher 3 finishes eating
Philosopher 3 puts down forks and starts THINKING
Philosopher 4 takes forks and starts eating
Philosopher 3 is THINKING
Philosopher 0 is HUNGRY
Philosopher 3 is HUNGRY
Philosopher 1 finishes eating
Philosopher 1 puts down forks and starts THINKING
Philosopher 2 takes forks and starts eating
Philosopher 1 is THINKING
Philosopher 4 finishes eating
Philosopher 4 puts down forks and starts THINKING
Philosopher 0 takes forks and starts eating
Philosopher 1 is HUNGRY
Philosopher 2 finishes eating
Philosopher 2 puts down forks and starts THINKING
Philosopher 3 takes forks and starts eating
Philosopher 0 finishes eating
Philosopher 0 puts down forks and starts THINKING
Philosopher 1 takes forks and starts eating
Philosopher 3 finishes eating
Philosopher 3 puts down forks and starts THINKING
Philosopher 1 finishes eating
Philosopher 1 puts down forks and starts THINKING