fork download
  1. #include<stdio.h>
  2. #include<pthread.h>
  3. #include<stdlib.h>
  4. #include<semaphore.h>
  5. #include<unistd.h>
  6. #define Num_philosophers 5
  7.  
  8.  
  9. pthread_mutex_t forks[Num_philosophers];
  10.  
  11. sem_t mutex;
  12.  
  13. void *philosopher(int *arg){
  14. int phil_id = *arg;
  15. int left_fork=phil_id;
  16. int right_fork=(phil_id+1)%Num_philosophers;
  17. int temp=0;
  18.  
  19. while(temp<10){
  20. temp++;
  21. printf("philosopher %d is thinking \n",phil_id);
  22. usleep(1000);
  23. pthread_mutex_unlock(&forks[left_fork]);
  24. pthread_mutex_unlock(&forks[right_fork]);
  25.  
  26. printf("philosopher %d is done eating \n",phil_id);
  27. }
  28. return NULL;
  29. }
  30.  
  31.  
  32. int main(){
  33. pthread_t philosophers[Num_philosophers];
  34. sem_init(&mutex,0,1);
  35.  
  36. for(int i=0;i<Num_philosophers;++i){
  37.  
  38. pthread_mutex_init(&forks[i],NULL);
  39. }
  40.  
  41. for(int i=0;i<Num_philosophers;++i)
  42. {
  43. int *arg=malloc(sizeof(*arg));
  44. *arg=i;
  45. pthread_create(&philosophers[i],NULL,philosopher,arg);
  46. }
  47.  
  48. for(int i=0;i<Num_philosophers;++i){
  49.  
  50.  
  51. pthread_join(philosophers[i],NULL);
  52.  
  53. sem_destroy(&mutex);
  54.  
  55. for(int i=0;i<Num_philosophers;++i){
  56.  
  57. pthread_mutex_destroy(&forks[i]);
  58.  
  59. return 0;
  60. }
  61. }
  62.  
  63.  
  64. }
Success #stdin #stdout 0s 5308KB
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 done eating 
philosopher 4 is thinking 
philosopher 3 is done eating 
philosopher 3 is thinking 
philosopher 2 is done eating 
philosopher 2 is thinking 
philosopher 1 is done eating 
philosopher 1 is thinking 
philosopher 0 is done eating 
philosopher 0 is thinking 
philosopher 1 is done eating 
philosopher 1 is thinking 
philosopher 2 is done eating 
philosopher 2 is thinking 
philosopher 3 is done eating 
philosopher 3 is thinking 
philosopher 4 is done eating 
philosopher 4 is thinking 
philosopher 0 is done eating 
philosopher 0 is thinking 
philosopher 3 is done eating 
philosopher 3 is thinking 
philosopher 2 is done eating 
philosopher 2 is thinking 
philosopher 1 is done eating 
philosopher 1 is thinking 
philosopher 4 is done eating 
philosopher 4 is thinking 
philosopher 0 is done eating 
philosopher 0 is thinking 
philosopher 2 is done eating 
philosopher 2 is thinking 
philosopher 1 is done eating 
philosopher 1 is thinking 
philosopher 4 is done eating 
philosopher 4 is thinking 
philosopher 3 is done eating 
philosopher 3 is thinking 
philosopher 0 is done eating 
philosopher 0 is thinking 
philosopher 4 is done eating 
philosopher 4 is thinking 
philosopher 1 is done eating 
philosopher 1 is thinking 
philosopher 3 is done eating 
philosopher 3 is thinking 
philosopher 2 is done eating 
philosopher 2 is thinking 
philosopher 0 is done eating 
philosopher 0 is thinking 
philosopher 3 is done eating 
philosopher 3 is thinking 
philosopher 2 is done eating 
philosopher 2 is thinking 
philosopher 1 is done eating 
philosopher 1 is thinking 
philosopher 4 is done eating 
philosopher 4 is thinking 
philosopher 0 is done eating 
philosopher 0 is thinking 
philosopher 1 is done eating 
philosopher 1 is thinking 
philosopher 2 is done eating 
philosopher 2 is thinking 
philosopher 4 is done eating 
philosopher 4 is thinking 
philosopher 3 is done eating 
philosopher 3 is thinking 
philosopher 0 is done eating 
philosopher 0 is thinking 
philosopher 2 is done eating 
philosopher 2 is thinking 
philosopher 4 is done eating 
philosopher 4 is thinking 
philosopher 3 is done eating 
philosopher 3 is thinking 
philosopher 1 is done eating 
philosopher 1 is thinking 
philosopher 0 is done eating 
philosopher 0 is thinking 
philosopher 3 is done eating 
philosopher 3 is thinking 
philosopher 1 is done eating 
philosopher 1 is thinking 
philosopher 4 is done eating 
philosopher 4 is thinking 
philosopher 2 is done eating 
philosopher 2 is thinking 
philosopher 0 is done eating 
philosopher 0 is thinking 
philosopher 4 is done eating 
philosopher 1 is done eating 
philosopher 2 is done eating 
philosopher 3 is done eating 
philosopher 0 is done eating