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