Advertisement
Guest User

Piter part1

a guest
Jan 21st, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1.  
  2.  
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <signal.h>
  9. #include <fcntl.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/ipc.h>
  13. #include <err.h>
  14. #include <sys/sem.h>
  15. #include <stdio.h>
  16. #include <sys/msg.h>
  17. #include <errno.h>
  18.  
  19.  
  20. key_t key1, key2, key3; // klucz dla semaforow
  21. int semid1, semid2, semid3; // ID semaforow
  22. union semun ctl; // unia do kontroli semafora
  23. int pid1, pid2, pid3, ppid;
  24.  
  25.  
  26. union semun
  27. {
  28. int val;
  29. struct semid_ds *buf;
  30. unsigned short int *array;
  31. struct seminfo *__buf;
  32. };
  33.  
  34. int semlock(int semid)
  35. {
  36.  
  37. struct sembuf opr;
  38. opr.sem_num = 0;
  39. opr.sem_op = -1;
  40. opr.sem_flg = 0;
  41.  
  42. if (semop(semid, &opr, 1) == -1){
  43. warn("Blad blokowania semafora!");
  44. return 0;
  45. }else{
  46. return 1;
  47. }
  48. }
  49.  
  50. int semunlock(int semid)
  51. {
  52. struct sembuf opr;
  53.  
  54. opr.sem_num = 0;
  55. opr.sem_op = 1;
  56. opr.sem_flg = 0;
  57.  
  58. if (semop(semid, &opr, 1) == -1)
  59. {
  60. warn("Blad odblokowania semafora!");
  61. return 0;
  62. }
  63. else
  64. {
  65. return 1;
  66. }
  67. }
  68. struct mymsgbuf {
  69. long mtype; /* typ wiadomości */
  70. int request; /* numer żądania danego działania */
  71. int i;
  72. } msg;
  73.  
  74. struct mymsgbuf buf;
  75.  
  76. int open_queue( key_t keyval ) {
  77. int qid;
  78. if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
  79. return(-1);
  80. return(qid);
  81. }
  82.  
  83. int send_message( int qid, struct mymsgbuf *qbuf ){
  84. int result, length;
  85. /* lenght jest rozmiarem struktury minus sizeof(mtype) */
  86. length = sizeof(struct mymsgbuf) - sizeof(long);
  87. if((result = msgsnd( qid, qbuf, length, 0)) == -1)
  88. return(-1);
  89. return(result);
  90. }
  91.  
  92. int remove_queue( int qid ){
  93. if( msgctl( qid, IPC_RMID, 0) == -1)
  94. return(-1);
  95. return(0);
  96. }
  97.  
  98. int read_message( int qid, long type, struct mymsgbuf *qbuf ){
  99. int result, length;
  100.  
  101. /* lenght jest rozmiarem struktury minus sizeof(mtype) */
  102. length = sizeof(struct mymsgbuf) - sizeof(long);
  103. if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
  104. return(-1);
  105. return(result);
  106. }
  107.  
  108. int main()
  109. {
  110. if ((key1 = ftok(".", 'A')) == -1)
  111. errx(1, "Blad tworzenia klucza!");
  112. if ((semid1 = semget(key1, 1, IPC_CREAT | 0600)) == -1)
  113. errx(2, "Blad tworzenia semafora!");
  114. ctl.val = 1;
  115. if (semctl(semid1, 0, SETVAL, ctl) == -1)
  116. errx(3, "Blad ustawiania semafora!");
  117.  
  118. if ((key2 = ftok(".", 'B')) == -1)
  119. errx(1, "Blad tworzenia klucza!");
  120. if ((semid2 = semget(key2, 1, IPC_CREAT | 0600)) == -1)
  121. errx(2, "Blad tworzenia semafora!");
  122. ctl.val = 1;
  123. if (semctl(semid2, 0, SETVAL, ctl) == -1)
  124. errx(3, "Blad ustawiania semafora!");
  125.  
  126. if ((key3 = ftok(".", 'C')) == -1)
  127. errx(1, "Blad tworzenia klucza!");
  128. if ((semid3 = semget(key3, 1, IPC_CREAT | 0600)) == -1)
  129. errx(2, "Blad tworzenia semafora!");
  130. ctl.val = 1;
  131. if (semctl(semid3, 0, SETVAL, ctl) == -1)
  132. errx(3, "Blad ustawiania semafora!");
  133. semlock(semid2);
  134.  
  135. int qid;
  136. key_t msgkey;
  137.  
  138. /* tworzymy wartość klucza IPC */
  139. msgkey = ftok(".", 'm');
  140.  
  141. /* otwieramy/tworzymy kolejkę */
  142. if(( qid = open_queue( msgkey)) == -1) {
  143. perror("Otwieranie_kolejki");
  144. exit(1);
  145. }
  146.  
  147. msg.mtype = 1; /* typ wiadomości musi być dodatni */
  148. msg.request = 1;
  149. msg.i = 220; /* wysyana liczba */
  150.  
  151.  
  152. if(fork() == 0) {
  153. char c;
  154. while(1){ // (c = fgetc(stdin)) != EOF
  155. semlock(semid1);
  156. printf("proces1\n"); fflush(stdout);
  157. //printf("%c", c); fflush(stdout);
  158. semunlock(semid2);
  159. }
  160. }
  161. if(fork() == 0) {
  162. while(1) {
  163. semlock(semid2);
  164. printf("proces2\n"); fflush(stdout);
  165. semunlock(semid3);
  166. }
  167. }
  168. if(fork() == 0) {
  169. while(1) {
  170. semlock(semid3);
  171. printf("proces2\n"); fflush(stdout);
  172. semunlock(semid31);
  173. }
  174. }
  175. sleep(2);
  176.  
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement