Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/ipc.h>
  5. #include <sys/msg.h>
  6. #include <sys/shm.h>
  7. #include <time.h>
  8.  
  9. #define LICZBA_FILOZOFOW 5
  10. #define W 7
  11.  
  12. struct bufor
  13. {
  14. long mtype;
  15. int mvalue;
  16. };
  17.  
  18. void thinking();
  19. void eating();
  20. void test(int nr_filozofa);
  21. void showForks();
  22.  
  23. int msgID,shmID;
  24. int numer;
  25. int *pam;
  26. key_t kluczm, klucz;
  27. struct bufor komunikat;
  28.  
  29. int main(int argc,char *argv[])
  30. {
  31. srand(time(NULL));
  32.  
  33. if ( (kluczm = ftok(".", 'A')) == -1 )
  34. {
  35. printf("Blad kluczs (main)\n");
  36. exit(1);
  37. }
  38.  
  39. shmID=shmget(kluczm, LICZBA_FILOZOFOW*sizeof(int), IPC_CREAT|0666);//tworzenie pam. dz.
  40. if (shmID==-1){
  41. printf("blad pam dzielonej\n");
  42. exit(1);
  43. }
  44.  
  45. if ((klucz = ftok(".", 'B')) == -1)
  46. {
  47. printf("Blad ftok (main)\n");
  48. exit(1);
  49. }
  50. msgID = msgget(klucz, IPC_CREAT | 0666);
  51. if (msgID == -1)
  52. {
  53. printf("blad kolejki komunikatow\n");
  54. exit(1);
  55. }
  56.  
  57. pam=(int*)shmat(shmID,NULL,0);
  58.  
  59. if(*pam==-1){
  60. printf("Problem z przydzieleniem adresu.\n");
  61. exit(1);
  62. }
  63.  
  64. numer = atoi(argv[1]);
  65.  
  66. int pp=10;
  67. while(pp) {
  68.  
  69. thinking(); //myslenie
  70. printf("Filozof %d czeka na komunikat 1W\n",numer);
  71. msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), W, 0); //wait(w)
  72. printf("Filozof %d odebral komunikat 1W\n",numer);
  73. pam[numer-1] = 1; //stan[name] := 1
  74.  
  75. test(numer); //test(name)
  76.  
  77. komunikat.mtype = W;
  78. if (msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue),0) == -1) { //signal(w)
  79. printf("blad wyslania kom. pustego\n");
  80. exit(1);
  81. }
  82. printf("Filozof %d wyslal komunikat typu W\n",numer);
  83. printf("Filozof %d czeka na komunikat %d\n",numer,numer);
  84. msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), numer, 0); //wait(sem[name])
  85. printf("Filozof %d odebral komunikat %d\n",numer,numer);
  86. //msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), (numer+1)%5, 0);
  87. printf("filozof %d zaczal je\n", numer); //jedzenie
  88.  
  89. pam[numer] = 0;
  90. printf("Filozof %d czeka na komunikat 2W\n",numer);
  91. msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), W, 0); //wait(w)
  92. printf("Filozof %d odebral komunikat 2W\n",numer);
  93. //msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), (numer+1)%5, 0); //wait(w)
  94.  
  95. int filp,fill;
  96. fill=(numer-1)%5;
  97. if(fill==0)fill=5;
  98. filp=(numer+1)%5;
  99. if(filp==0) filp=1;
  100.  
  101. test(filp); //test((name+1)mod5)
  102. test(fill); //test((name-1)mod5)
  103.  
  104. komunikat.mtype = W;
  105. if (msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue),0) == -1) { //signal(w)
  106. printf("blad wyslania kom. pustego\n");
  107. exit(1);
  108. }
  109. sleep(1);
  110. pp--;
  111. }
  112.  
  113.  
  114.  
  115. /*printf("%d filozof mysli\n",numer+1);
  116. msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), numer + 1, 0);
  117. msgrcv(msgID, &komunikat, sizeof(komunikat.mvalue), (numer+1)%5 + 1, 0);
  118.  
  119. pam[numer]=numer+1;
  120. pam[(numer+1)%5]=numer+1;
  121. printf("%d filozof je, uzywa widelca %d i %d \n",numer+1,numer+1,(numer+1)%5+1);
  122. for(int i=0;i<LICZBA_FILOZOFOW;i++)
  123. printf("%d ",pam[i]);
  124. printf("\n");
  125.  
  126. pam[numer]=0;
  127. pam[(numer+1)%5]=0;
  128.  
  129. komunikat.mtype = numer+1;
  130. if (msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue),0) == -1)
  131. {
  132. // printf("blad wyslania kom. pustego\n");
  133. exit(1);
  134. }
  135. komunikat.mtype = ((numer+1)%5)+1;
  136. if (msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue),0 ) == -1)
  137. {
  138. // printf("blad wyslania kom. pustego\n");
  139. exit(1);
  140. } */
  141.  
  142. }
  143.  
  144. void thinking()
  145. {
  146. printf("Filozof %d mysli...\n", numer);
  147. //sleep(rand()%3);
  148. }
  149.  
  150. void showForks()
  151. {
  152. int i;
  153. printf("\ntablica stanow: \n");
  154. for(i = 0; i < LICZBA_FILOZOFOW; i++) {
  155. printf("%d", pam[i]);
  156. }
  157. printf("\n");
  158. }
  159.  
  160. void eating()
  161. {
  162. printf("----------- filozof %d je... \n", numer);
  163. // sleep(rand()%4);
  164. showForks();
  165. printf("Filozof %d skonczyl jesc \n", numer);
  166. }
  167.  
  168. void test(int nr_filozofa)
  169. {
  170. int fill, filp;
  171. fill=(nr_filozofa-1)%5;
  172. if(fill==0)fill=5;
  173. filp=(nr_filozofa+1)%5;
  174. if(filp==0) filp=1;
  175. if(pam[fill] != 2 && pam[nr_filozofa] == 1 && pam[filp] != 2)
  176. {
  177. pam[nr_filozofa] = 2;
  178. komunikat.mtype = nr_filozofa;}
  179. msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue), 0);
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement