fork download
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <pthread.h>
  4. #include <semaphore.h>
  5. #include <stdlib.h>
  6.  
  7. sem_t chopstick[5];
  8.  
  9. void eat(int a)
  10. {
  11. printf("\nphilosopher %d eats",a);
  12. }
  13.  
  14. void* philos(void* n)
  15. {
  16. int ph=*(int *)n;
  17. printf("\nphilosopher %d wants to eat",ph);
  18. printf("\nphilosppher %d tries to pick up the left chopstick",ph);
  19. sem_wait(&chopstick[ph]);
  20. printf("\nphilosopher %d successfully pick up the left chopstick",ph);
  21. printf("\nphilosppher %d tries to pick up the right chopstick",ph);
  22. sem_wait(&chopstick[(ph+1)%5]);
  23. printf("\nphilosopher %d successfully pick up the right chopstick",ph);
  24. eat(ph);
  25. sleep(2);
  26. printf("philosopher %d finished eating",ph);
  27. printf("\nphilosopher %d puts down the left chopstick",ph);
  28. sem_post(&chopstick[(ph+1)%5]);
  29. printf("\nphilosopher %d puts down the right chopstick",ph);
  30. sem_post(&chopstick[ph]);
  31. }
  32.  
  33.  
  34. int main()
  35. {
  36. int i,n[5];
  37. pthread_t T[5];
  38. for(i=0;i<5;i++)
  39. {
  40. sem_init(&chopstick[i],0,1);
  41. }
  42. for(i=0;i<5;i++)
  43. {
  44. n[i]=i;
  45. pthread_create(&T[i],NULL,philos,(void *)&n[i]);
  46. }
  47. for(i=0;i<5;i++)
  48. {
  49. pthread_join(T[i],NULL);
  50. }
  51.  
  52. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
philosopher 4 wants to eat
philosppher 4 tries to pick up the left chopstick
philosopher 4 successfully pick up the left chopstick
philosppher 4 tries to pick up the right chopstick
philosopher 4 successfully pick up the right chopstick
philosopher 4 eats
philosopher 3 wants to eat
philosppher 3 tries to pick up the left chopstick
philosopher 3 successfully pick up the left chopstick
philosppher 3 tries to pick up the right chopstick
philosopher 2 wants to eat
philosppher 2 tries to pick up the left chopstick
philosopher 2 successfully pick up the left chopstick
philosppher 2 tries to pick up the right chopstick
philosopher 1 wants to eat
philosppher 1 tries to pick up the left chopstick
philosopher 1 successfully pick up the left chopstick
philosppher 1 tries to pick up the right chopstick
philosopher 0 wants to eat
philosppher 0 tries to pick up the left chopstickphilosopher 4 finished eating
philosopher 4 puts down the left chopstick
philosopher 4 puts down the right chopstick
philosopher 3 successfully pick up the right chopstick
philosopher 3 eats
philosopher 0 successfully pick up the left chopstick
philosppher 0 tries to pick up the right chopstickphilosopher 3 finished eating
philosopher 3 puts down the left chopstick
philosopher 3 puts down the right chopstick
philosopher 2 successfully pick up the right chopstick
philosopher 2 eatsphilosopher 2 finished eating
philosopher 2 puts down the left chopstick
philosopher 2 puts down the right chopstick
philosopher 1 successfully pick up the right chopstick
philosopher 1 eatsphilosopher 1 finished eating
philosopher 1 puts down the left chopstick
philosopher 1 puts down the right chopstick
philosopher 0 successfully pick up the right chopstick
philosopher 0 eatsphilosopher 0 finished eating
philosopher 0 puts down the left chopstick
philosopher 0 puts down the right chopstick