fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<pthread.h>
  4. #include<semaphore.h>
  5. #include<unistd.h>
  6. sem_t chopstick[5];
  7. void * philos(void *);
  8. void eat(int);
  9. int main()
  10. {
  11. int i,n[5];
  12. pthread_t T[5];
  13. for(i=0;i<5;i++)
  14. sem_init(&chopstick[i],0,1);
  15. for(i=0;i<5;i++){
  16. n[i]=i;
  17. pthread_create(&T[i],NULL,philos,(void *)&n[i]);
  18. }
  19. for(i=0;i<5;i++)
  20. pthread_join(T[i],NULL);
  21. }
  22. void * philos(void * n)
  23. {
  24. int ph=*(int *)n;
  25. printf("Philosopher %d wants to eat\n",ph);
  26. printf("Philosopher %d tries to pick left chopstick\n",ph);
  27. sem_wait(&chopstick[ph]);
  28. printf("Philosopher %d picks the left chopstick\n",ph);
  29. printf("Philosopher %d tries to pick the right chopstick\n",ph);
  30. sem_wait(&chopstick[(ph+1)%5]);
  31. printf("Philosopher %d picks the right chopstick\n",ph);
  32. eat(ph);
  33. sleep(2);
  34. printf("Philosopher %d has finished eating\n",ph);
  35. sem_post(&chopstick[(ph+1)%5]);
  36. printf("Philosopher %d leaves the right chopstick\n",ph);
  37. sem_post(&chopstick[ph]);
  38. printf("Philosopher %d leaves the left chopstick\n",ph);
  39. }
  40. void eat(int ph)
  41. {
  42. printf("Philosopher %d begins to eat\n",ph);
  43. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Philosopher 4 wants to eat
Philosopher 4 tries to pick left chopstick
Philosopher 4 picks the left chopstick
Philosopher 4 tries to pick the right chopstick
Philosopher 4 picks the right chopstick
Philosopher 4 begins to eat
Philosopher 3 wants to eat
Philosopher 3 tries to pick left chopstick
Philosopher 3 picks the left chopstick
Philosopher 3 tries to pick the right chopstick
Philosopher 2 wants to eat
Philosopher 2 tries to pick left chopstick
Philosopher 2 picks the left chopstick
Philosopher 2 tries to pick the right chopstick
Philosopher 1 wants to eat
Philosopher 1 tries to pick left chopstick
Philosopher 1 picks the left chopstick
Philosopher 1 tries to pick the right chopstick
Philosopher 0 wants to eat
Philosopher 0 tries to pick left chopstick
Philosopher 4 has finished eating
Philosopher 4 leaves the right chopstick
Philosopher 4 leaves the left chopstick
Philosopher 0 picks the left chopstick
Philosopher 0 tries to pick the right chopstick
Philosopher 3 picks the right chopstick
Philosopher 3 begins to eat
Philosopher 3 has finished eating
Philosopher 3 leaves the right chopstick
Philosopher 3 leaves the left chopstick
Philosopher 2 picks the right chopstick
Philosopher 2 begins to eat
Philosopher 2 has finished eating
Philosopher 2 leaves the right chopstick
Philosopher 2 leaves the left chopstick
Philosopher 1 picks the right chopstick
Philosopher 1 begins to eat
Philosopher 1 has finished eating
Philosopher 1 leaves the right chopstick
Philosopher 1 leaves the left chopstick
Philosopher 0 picks the right chopstick
Philosopher 0 begins to eat
Philosopher 0 has finished eating
Philosopher 0 leaves the right chopstick
Philosopher 0 leaves the left chopstick