fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <semaphore.h>
  5. #include <unistd.h>
  6.  
  7.  
  8. //global variables
  9. int c1,c2; //counters for imp1, imp2
  10. pthread_mutex_t m1, m2;
  11. sem_t corridor;
  12.  
  13. //implementation thread attendant
  14. void* attendant(void* arg){
  15. sleep(2); //rest 2 seconds
  16. if (c1==0 && c2==0){
  17. printf("I'm starting to clean up\n");
  18. sem_wait(&corridor);
  19. sleep(2);
  20. sem_post(&corridor);
  21. printf ("I finished cleaning n");
  22. }else{
  23. printf ("I can't clean, the corridor is busy\n");
  24. }
  25. return NULL;
  26. }
  27.  
  28.  
  29. //thread employee type 1
  30. void* emp1(void *arg){
  31. printf("I'm the number %d\n", c1);
  32. pthread_mutex_lock(&m1); //beginning critical section
  33. c1++; //it increases to signal the presence of a thread of the same type that wants to enter the corridor
  34. if (c1 == 1){ //the thread is the only one in the corridor. Can pass
  35. printf ("I am the first of my group n");
  36. sem_wait(&corridor); //takes possession of the corridor
  37. }
  38. pthread_mutex_unlock(&m1); //allows other threads of the same type to pass in the corridor since it was the first in his group. End of critical section
  39.  
  40. // invents "passage" function
  41.  
  42. pthread_mutex_lock(&m1); //beginning of the critical section. Once crossed the corridor, the variable c1 is modified. A mutex is used to avoid inconsistency
  43. c1--;
  44. if(c1 == 0){
  45. printf ("I am the last of my group n");
  46. sem_post(&corridor);
  47. } //if c1 == 0, it is the last thread imp1 and releases the corridor
  48. pthread_mutex_unlock(&m1); //end critical section
  49. return NULL;
  50. }
  51.  
  52.  
  53. //thread employee type 2
  54. void* emp2(void *arg){
  55. printf("I'm the number %d\n", c1);
  56. pthread_mutex_lock(&m2); //beginning critical section
  57. c2++; //it increases to signal the presence of a thread of the same type that wants to enter the corridor
  58. if (c2 == 1){ // the thread is the only one in the corridor. Can pass
  59. printf ("I am the first of my group n");
  60. sem_wait(&corridor); //takes possession of the corridor
  61. }
  62. pthread_mutex_unlock(&m2); //allows other threads of the same type to pass in the corridor since it was the first in his group. End of critical sectionritica
  63.  
  64. // invents "passage" function
  65.  
  66. pthread_mutex_lock(&m2); //beginning of the critical section. Once crossed the corridor, the variable c1 is modified. A mutex is used to avoid inconsistency
  67. c2--;
  68. if(c2 == 0){
  69. printf ("I am the last of my group n");
  70. sem_post(&corridor);
  71. }//if c1 == 0, it is the last thread imp1 and releases the corridor
  72. pthread_mutex_unlock(&m2); //end critical section
  73. return NULL;
  74. }
  75.  
  76.  
  77. int main(int argc, char const *argv[]) {
  78. //pthread_t emp1, emp2, attendant;
  79. pthread_t idt;
  80. int r; //var random to create thread emp1 or emp2
  81. int i; //index
  82.  
  83. //variable initialization
  84. c1 = c2 = 0;
  85. pthread_mutex_init(&m1, NULL);
  86. pthread_mutex_init(&m2, NULL);
  87. sem_init(&corridor,0,1);
  88.  
  89. pthread_create(&idt,NULL,attendant,NULL);
  90. while(i<40){
  91. r = rand()%2;
  92. if(r==0){
  93. printf("Employee creation 1\n");
  94. pthread_create(&idt,NULL,emp1,NULL);
  95. }else{
  96. printf("Employee creation 1\n");
  97. pthread_create(&idt,NULL,emp2,NULL);
  98. }
  99. i++;
  100. }
  101. return 0;
  102. }
Success #stdin #stdout 0s 42256KB
stdin
Standard input is empty
stdout
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1
Employee creation 1