Advertisement
SIKER_98

2

Jan 17th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. #include <sys/shm.h>
  2. #include <sys/sem.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. #include <err.h>
  8. #include <unistd.h>
  9. #include <time.h>
  10. #include <sys/types.h>
  11.  
  12. #define MAX 256
  13. #define ODCZYT 0
  14. #define ZAPIS 1
  15.  
  16.  
  17. /// ---- deklaracje
  18. int pamiec, semafory[2];
  19.  
  20. static struct sembuf semafor;
  21.  
  22. /*Nadanie slowom znacznie wartosci (aby ulatwic pisanie kodu)*/
  23. enum {
  24. PRACUJ, CZEKAJ, INIT
  25. } stan;
  26.  
  27. // informacje procesow
  28. typedef struct {
  29. int pid[4];
  30. char buf12[MAX];
  31. char buf23[MAX];
  32. } mem;
  33. mem *memory;
  34.  
  35. const char *kolejka[4] = {"", "fifo1", "fifo2", "fifo3"}; // kolejki
  36.  
  37. void push(int, int); // podnoszenie semafora
  38.  
  39. void pop(int, int); // opuszczanie semfora
  40.  
  41. static void get_sig(int); // reakcja na sygnal
  42.  
  43. void wait4pids(); // wstrzymanie procesow do momentu utworzenia ich wszystkich
  44.  
  45. void proces1();
  46.  
  47. void proces2();
  48.  
  49. void proces3();
  50.  
  51. ///----
  52.  
  53. int main(int argc, char *argv[]) {
  54.  
  55.  
  56. int i; // petle
  57.  
  58.  
  59. //semafory - utworzenie i przypisanie im wartosci
  60. for (i = 0; i < 2; i++) {
  61. semafory[i] = semget(IPC_PRIVATE, 2, 0644);
  62. semctl(semafory[i], 0, SETVAL, (int) 0);
  63. semctl(semafory[i], 0, SETVAL, (int) 1);
  64. }
  65.  
  66. //tworzenie segmentu pamieci wspoldzielonej
  67. //potomkowie dziedzicza pamiec
  68. pamiec = shmget(IPC_PRIVATE, sizeof(mem), 0644);
  69.  
  70. //podpiecie pod pamiec wspoldzielona
  71. memory = (mem *) shmat(pamiec, NULL, 0);
  72.  
  73. /*kolejki
  74. * 064
  75. * owner read & write
  76. * other read
  77. */
  78.  
  79. mkfifo(kolejka[1], 0644);
  80. mkfifo(kolejka[2], 0644);
  81. mkfifo(kolejka[3], 0644);
  82.  
  83. //sygnaly
  84. signal(SIGUSR1, get_sig);
  85. signal(SIGUSR2, get_sig);
  86. signal(SIGINT, get_sig);
  87. signal(SIGCONT, get_sig);
  88.  
  89. // zapisanie identyfikatora procesu macierzystego
  90. memory->pid[0] = getpid();
  91.  
  92. stan = INIT;
  93.  
  94. int pid[4];
  95.  
  96. //proces 1
  97. if ((pid[1] = fork()) == 0) {
  98. proces1();
  99. } else if ((pid[2] = fork()) == 0) {
  100. proces2();
  101. } else if ((pid[3] = fork()) == 0) {
  102. proces3();
  103. } else {
  104. for (i = 1; i < 4; memory->pid[i] = pid[i], i++)
  105. if (pid[1] < 0)
  106. return 1;
  107.  
  108. kill(memory->pid[1], SIGINT);
  109. kill(memory->pid[2], SIGINT);
  110. kill(memory->pid[3], SIGINT);
  111. }
  112.  
  113. return 0;
  114. }
  115.  
  116.  
  117. ///------
  118. //SEMAFORY
  119. ///------
  120.  
  121. void push(int semid, int semnum) {
  122. semafor.sem_flg = 0;
  123. semafor.sem_num = semnum; // numer semafora
  124. semafor.sem_op = 1; // kierunek semfaora
  125. if (semop(semid, &semafor, 1) == -1) {
  126. printf("Blad podnoszenia semafora");
  127. exit(1);
  128. }
  129. }
  130.  
  131. void pop(int semid, int semnum) {
  132. semafor.sem_flg = 0;
  133. semafor.sem_num = semnum; // numer semafora
  134. semafor.sem_op = -1; // kierunek
  135. if (semop(semid, &semafor, 1) == -1) {
  136. printf("Blad opuszczania semafora");
  137. exit(1);
  138. }
  139. }
  140.  
  141. static void get_sig(int signo) {
  142. if (stan == INIT) {
  143. stan == PRACUJ;
  144. return;
  145. }
  146. int pnr, fs, _pid = getpid();
  147.  
  148. for (pnr = 1; _pid != memory->pid[pnr] && pnr <= 3; ++pnr);
  149. if (pnr == 4) {
  150. printf("Przekroczenie");
  151. exit(1);
  152. }
  153.  
  154. if (signo != SIGUSR2) {
  155. int i;
  156. for (i = 1; i <= 3; ++i)
  157. if (i != pnr) {
  158. kill(memory->pid[i], SIGUSR2);
  159. int f = open(kolejka[i], O_WRONLY);
  160. write(f, &signo, sizeof(signo));
  161. close(f);
  162. }
  163. } else {
  164. fs = open(kolejka[pnr], O_RDONLY);
  165. read(fs, &signo, sizeof(signo));
  166. close(fs);
  167. }
  168.  
  169. if (signo == SIGINT) {
  170. if (pnr == 1) {
  171. shmctl(pamiec, IPC_RMID, 0);
  172. semctl(semafory[0], IPC_RMID, 0);
  173. semctl(semafory[1], IPC_RMID, 0);
  174. }
  175. unlink(kolejka[pnr]);
  176. exit(0);
  177. } else if (signo == SIGUSR1) {
  178. stan = CZEKAJ;
  179. } else if (signo == SIGCONT) {
  180. stan = PRACUJ;
  181. }
  182.  
  183.  
  184. }
  185.  
  186. void wait4pids() {
  187. pause();
  188.  
  189. memory = (mem *) shmat(pamiec, NULL, 0);
  190. if (!memory) {
  191. printf("Plad podpiecia pamieci");
  192. exit(1);
  193. }
  194. }
  195.  
  196. void proces1() {
  197. wait4pids();
  198.  
  199. char buf[MAX];
  200. while (fgets(buf, sizeof(buf), stdin) > 0) {
  201. while (stan == CZEKAJ)pause();
  202.  
  203. pop(semafory[0], ZAPIS);
  204.  
  205. strncpy(memory->buf12, buf, sizeof(memory->buf12));
  206.  
  207. push(semafory[0], ODCZYT);
  208. }
  209. get_sig(SIGINT);
  210. }
  211.  
  212. void proces2() {
  213. wait4pids();
  214.  
  215. char buf[MAX];
  216.  
  217. pop(semafory[0], ODCZYT);
  218.  
  219. strncpy(buf, memory->buf12, sizeof(buf));
  220.  
  221. push(semafory[0], ZAPIS);
  222.  
  223. //--funkcja();
  224. *buf = "15";
  225.  
  226. pop(semafory[1], ZAPIS);
  227.  
  228. strncpy(memory->buf23, buf, sizeof(memory->buf23));
  229.  
  230. push(semafory[1], ODCZYT);
  231. }
  232.  
  233. void proces3() {
  234. wait4pids();
  235.  
  236. char buf[MAX];
  237.  
  238. pop(semafory[1], ODCZYT);
  239.  
  240. strncpy(buf, memory->buf23, sizeof(buf));
  241.  
  242. push(semafory[1], ZAPIS);
  243.  
  244. printf(buf);
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement