djk77

vrtuljak

Apr 2nd, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <sys/types.h>
  8. #include <sys/ipc.h>
  9. #include <sys/msg.h>
  10.  
  11. struct msgbuf{
  12. long mtype;
  13. char mtext[128];
  14. };
  15.  
  16. int n = 8;
  17. int size = 4;
  18. int ukupnoPosjetitelja = 0;
  19. int brojDretve = 0;
  20. int zelim=0;
  21. int sjeo=0;
  22.  
  23. key_t keys;
  24. int vqid = -1;
  25. int qids[8] = { 0 };
  26.  
  27. void *posjetitelj(void *vargp) {
  28. brojDretve++;
  29. struct msgbuf pwrite;
  30. key_t pkey = keys+brojDretve;
  31. int msgqid = msgget(pkey, 0600 | IPC_CREAT);
  32. int sleeptime;
  33. int ponovi=0;
  34.  
  35. while(ponovi<3){
  36. sleeptime = rand() % (2 + 1 - 1) + 1;
  37. sleep(sleeptime); //SPAVAJ X MILISEKUNDI
  38. //ZELIM SE VOZITI
  39. if(zelim<8){
  40. qids[zelim]=msgqid;
  41. zelim++;
  42. pwrite.mtype = 1;
  43. strcpy(pwrite.mtext, "Zelim");
  44. size_t len = strlen(pwrite.mtext)+1;
  45. //printf("POSJETITELJ zeli reda %d > %d .\n", qids[zelim-1], zelim-1);
  46. msgsnd(vqid, &pwrite, len, 0);
  47.  
  48. int k=-1;
  49. while(k==-1) {
  50. int k=msgrcv(msgqid, &pwrite, 128, 1, 0);
  51. if(k!=-1){
  52. int checkSjeo = strcmp(pwrite.mtext, "Sjedi");
  53. if(checkSjeo==0){
  54. printf("Sjeo POSJETITELJ %d \n", msgqid);
  55. sjeo++;
  56. k=-1;
  57. }
  58. int checkUstao = strcmp(pwrite.mtext, "Ustani");
  59. if(checkUstao==0){
  60. printf("Sisao POSJETITELJ %d \n", msgqid);
  61. sjeo--;
  62. zelim--;
  63. break;
  64. }
  65. }
  66. }
  67.  
  68. ponovi++;
  69. //printf("ponovi %d POSJETITELJ %d \n",ponovi, msgqid);
  70. }
  71. }
  72. brojDretve--;
  73. msgctl(msgqid, IPC_RMID, NULL);
  74. printf("POSJETITELJ %d ZAVRSIO\n", msgqid);
  75. return NULL;
  76. }
  77.  
  78. void *vrtuljak(void *vargp) {
  79. struct msgbuf vread;
  80. key_t vkey = keys;
  81. vqid = msgget(vkey, 0600 | IPC_CREAT);
  82. //printf("VRTULJAK red %d kljuc %d.\n", vqid, vkey); //
  83.  
  84. while(1) {
  85.  
  86. while(ukupnoPosjetitelja < size){
  87. int status = msgrcv(vqid, &vread, 128, 1, 0);
  88. if(status != -1){
  89. int rckey=qids[ukupnoPosjetitelja];
  90. // printf("VRTULJAK > poruka -> %s od %d \n", vread.mtext, rckey);
  91. ukupnoPosjetitelja++;
  92.  
  93. if(ukupnoPosjetitelja <= size){
  94. vread.mtype = 1;
  95. strcpy(vread.mtext, "Sjedi");
  96. size_t len = strlen(vread.mtext)+1;
  97. // printf("VRTULJAK > poslana poruka -> %s prema %d.\n", vread.mtext, rckey);
  98. msgsnd(rckey, &vread, len, 0);
  99. }
  100. }
  101. }
  102.  
  103. if(ukupnoPosjetitelja == size & sjeo==size){
  104. printf("VRTULJAK pokrenut.\n");
  105. int sleeptime = rand() % (3 + 1 - 1) + 1;
  106. sleep(sleeptime); //SPAVAJ X MILISEKUNDI
  107. printf("VRTULJAK zaustavljen.\n");
  108. for(int j=0; j<size; j++){
  109. //printf("SIDI POSJETITELJU %d.\n", qids[j]);
  110. vread.mtype = 1;
  111. strcpy(vread.mtext, "Ustani");
  112. size_t l = strlen(vread.mtext)+1;
  113. msgsnd(qids[j], &vread, l, 0);
  114. //pomakni red cekanja
  115. qids[j]=qids[j+size];
  116. //printf("sljecedi red %d.\n", qids[j]);
  117. }
  118. while(sjeo!=0){}
  119. ukupnoPosjetitelja = 0;
  120. if(brojDretve<size){
  121. msgctl(vqid, IPC_RMID, NULL);
  122. printf("VRTULJAK GOTOV \n");
  123. break;
  124. }
  125. }
  126. }
  127.  
  128. return NULL;
  129. }
  130.  
  131.  
  132. int main(){
  133.  
  134. srand((unsigned)time(NULL));
  135.  
  136. pthread_t threads[n+1];
  137. int i=0;
  138.  
  139. keys=getuid();
  140. //pokreni vrtuljak();
  141. pthread_create(&threads[i], NULL, vrtuljak, NULL);
  142.  
  143. while(vqid==-1){}
  144.  
  145. for (i = 1; i <=n; i++) {
  146. //pokreni posjetitelja(i);
  147. pthread_create(&threads[i], NULL, posjetitelj, NULL);
  148. }
  149.  
  150. pthread_exit(NULL);
  151. return 0;
  152. }
Add Comment
Please, Sign In to add comment