Advertisement
Guest User

opsys

a guest
Feb 19th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. int nG=0;//number of guides in the room
  6. int maxG=3;//number of all guides
  7. int nV=0;
  8. int maxV=30;
  9. int nI=0;
  10. int maxI=2;
  11. int R=10;//analogia episkeptwn/3enagwn
  12. int AlreadyEntered [3];//flag wste na ekteleitai mia fora to guide_enter apo ka8e numa guide
  13. //ews edw einai oi sun8hkes tou senariou
  14. pthread_mutex_t GuideLock;
  15. pthread_mutex_t VisitorLock;
  16.  
  17.  
  18. //Guide Functions=======================================================================
  19. void *Guide(void *threadid){
  20. while (1){
  21. long tid;
  22. tid=(long)threadid;
  23. int intID;
  24. intID=(int)tid;
  25. if(AlreadyEntered[intID]==0){
  26. guide_enter(tid);
  27. }//endif
  28. guide(tid);
  29. guide_exit(tid,AlreadyEntered);
  30. }//endwhile
  31. }//endGuide
  32.  
  33. void guide_enter(long tid){
  34. pthread_mutex_lock(&GuideLock);
  35. nG=nG+1;
  36. pthread_mutex_unlock(&GuideLock);
  37. printf("Guide %ld has entered the room, %d guides remaining.\n",tid,nG);
  38. }//endguide_enter
  39. void guide(long tid){
  40. printf("Guide %ld is working.\n",tid);
  41. sleep(420);
  42. }//endguide
  43. void guide_exit(long tid,int *AlreadyEntered){
  44. if((nG-1)*R>=nV){
  45. if(pthread_mutex_trylock(&GuideLock)==0){
  46. nG=nG-1;
  47. printf("Guide %ld has exited the room, %d guides remaining.\n",tid,nG);
  48. pthread_mutex_unlock(&GuideLock);
  49. pthread_exit(NULL);
  50. }//endif
  51. }//endif
  52. else{
  53. printf("Guide %ld attempted to leave but was unable to and is returning to work\n",tid);
  54. int intID;
  55. intID=(int)tid;
  56. AlreadyEntered[intID]=1;
  57. }//endelse
  58. }
  59. //Visitor Functions ======================================================================
  60. void *Visitor(void *threadid){
  61. while(1){
  62. long tid;
  63. tid=(long)threadid;
  64. visitor_enter(tid);
  65. learn(tid);
  66. visitor_exit(tid);
  67. }//endwhile
  68. }//endVisitor
  69.  
  70. void visitor_enter(long tid){
  71. float nVfloat=(float)nV;
  72. float temp=((nVfloat+1)/R);
  73. START:
  74. if(nG>=temp){
  75. pthread_mutex_lock(&VisitorLock);
  76. nV=nV+1;
  77. pthread_mutex_unlock(&VisitorLock);
  78. printf("Visitor %ld has entered the room, %d visitors remaining.\n",tid,nV);
  79. }//endif
  80. else goto START;
  81. }
  82. void learn(long tid){
  83. printf("Visitor %ld is learning.\n",tid);
  84. sleep(300);
  85. }//endlearn
  86. void visitor_exit(long tid){
  87. pthread_mutex_lock(&VisitorLock);
  88. nV=nV-1;
  89. pthread_mutex_unlock(&VisitorLock);
  90. printf("Visitor %ld has left the room, %d visitors remaining.\n",tid,nV);
  91. pthread_exit(NULL);
  92. }//endvisitor_exit
  93.  
  94. //Inspector Functions=======================================================================
  95. void *Inspector(void *threadid){
  96. while(1){
  97. long tid;
  98. tid=(long)threadid;
  99. inspector_enter(tid);
  100. verify_compliance(tid);
  101. inspector_exit(tid);
  102. }
  103. }
  104. void inspector_enter(long tid){
  105. printf("Inspector %ld has entered the room.\n",tid);
  106. }
  107. void verify_compliance(long tid){
  108. if(nG>=(nV/R)){
  109. printf("Inspector %ld has verified that the protocol is in place.\n",tid);
  110. }
  111. else{
  112. printf("Inspector %ld has found staff to be inadequate.\n",tid);
  113. }
  114. }
  115. void inspector_exit(long tid){
  116. printf("Inspector %ld has completed his task and is exiting the room.\n",tid);
  117. pthread_exit(NULL);
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124. int main(){
  125. pthread_mutex_init(&GuideLock,NULL);
  126. pthread_mutex_init(&VisitorLock,NULL);
  127. int rcg;
  128. int rcv;
  129. int rci;
  130. long g=0;
  131. long v=0;
  132. long i=0;
  133. pthread_t Gthreads[maxG];
  134. pthread_t Vthreads[maxV];
  135. pthread_t Ithreads[maxI];
  136. int temp1=0;
  137. int temp2=0;//auth h metavlhth xrhsimopoieitai gia ton xronismo dhmiourgias twn threads
  138. //Creating First Guide Thread ========================================
  139. rcg=pthread_create(&Gthreads[g],NULL,Guide,(void*)g);
  140. //Creating Visitor and Guide Threads============================================
  141. for(v=0;v<maxV;v++){
  142. rcv=pthread_create(&Vthreads[v],NULL,Visitor,(void*)v);
  143. sleep(60);
  144. temp1++;
  145. temp2++;
  146. if((temp1==5)&&(g<maxG-1)){
  147. temp1=0;
  148. g++;
  149. rcg=pthread_create(&Gthreads[g],NULL,Guide,(void*)g);
  150. if(rcg){
  151. printf("Error in pthread create %ld\n",g);
  152. exit(-1);
  153. }//endif
  154. }//endif
  155. if(temp2==14){
  156. rci=pthread_create(&Ithreads[i],NULL,Inspector,(void*)i);
  157. i++;
  158. temp2=0;
  159. if(rci){
  160. printf("Error in pthread create %ld\n",i);
  161. exit(-1);
  162. }//endif
  163. }//endif
  164. if(rcv){
  165. printf("Error in pthread create %ld\n",v);
  166. exit(-1);
  167. }//endif
  168. }//endfor
  169. //kanw join ola ta numata wste h main na perimenei thn ektelesh tous prin termatistei
  170. for(v=0;v<maxV;v++){
  171. rcv=pthread_join(Vthreads[v],NULL);
  172. if(rcv){
  173. printf("Error in pthread join %ld\n",v);
  174. exit(-1);
  175. }//endif
  176. }//endfor
  177. for(g=0;g<maxG;g++){
  178. rcg=pthread_join(Gthreads[g],NULL);
  179. if(rcg){
  180. printf("Error in pthread join %ld\n",g);
  181. exit(-1);
  182. }//endif
  183. }//endfor
  184. for(i=0;i<maxI;i++){
  185. rci=pthread_join(Ithreads[i],NULL);
  186. if(rci){
  187. printf("Error in pthread join %ld\n",i);
  188. exit(-1);
  189. }//endif
  190. }//endfor
  191. printf("Main Completed.\n");
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement