Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <fcntl.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <sys/types.h>
  8. #include <sys/msg.h>
  9. #include <sys/wait.h>
  10.  
  11. #define MAX_TEXT_LENGTH 500
  12. #define READ 0
  13. #define WRITE 1
  14.  
  15. // znak oddzielajacy liczby w paczce
  16. const char *znak = "|";
  17.  
  18. int pdes1[2];
  19. int pdes2[2];
  20.  
  21. int flaga = 0;
  22. int qid;
  23.  
  24. // tablica pidow
  25. __pid_t pidy[3];
  26.  
  27. #define PLIK_Z_PIDAMI "pidy.txt"
  28.  
  29. struct msgbuf {
  30. long mtype;
  31. int komunikat;
  32. };
  33.  
  34. int wyslij_komunikat(int qid, struct msgbuf *data)
  35. {
  36. return msgsnd(qid, data, sizeof(struct msgbuf) - sizeof(long), 0);
  37. }
  38.  
  39. int odczytaj_komunikat(int qid, long type, struct msgbuf *output)
  40. {
  41. return msgrcv(qid, output, sizeof(struct msgbuf) - sizeof(long), type, 0);
  42. }
  43.  
  44. void usun_kolejke(int qid)
  45. {
  46. msgctl(qid, IPC_RMID, 0);
  47. }
  48.  
  49. void wczytaj_pidy()
  50. {
  51. int i;
  52.  
  53. FILE *f = fopen(PLIK_Z_PIDAMI, "r");
  54. for (i = 0; i < 3; i++)
  55. fscanf(f, "%d", &pidy[i]);
  56. fclose(f);
  57. }
  58.  
  59. int moze_byc_liczba(char *str, int n)
  60. {
  61. int i;
  62. for (i = 0; i < n; i++)
  63. {
  64. if (!((str[i] <= '9' && str[i] >= '0') || ( i == 0 && str[i] == '-')))
  65. return 0;
  66. }
  67. return 1;
  68. }
  69.  
  70. void wypelnij_tablice_zerami(char tab[], int n)
  71. {
  72. int i;
  73. for (i = 0; i < n; i++)
  74. tab[i] = 0;
  75. }
  76.  
  77. void sygnaly_dalsze_dzialanie(){
  78. struct msgbuf xd;
  79. odczytaj_komunikat(qid, getpid(), &xd);
  80. int wartosc = xd.komunikat;
  81.  
  82. if(wartosc == SIGUSR2){
  83. flaga = 0; //Wznowienie dzialania
  84. }
  85. else if(wartosc == SIGUSR1){
  86. flaga = 1; //Wstrzymanie dzialania
  87. }
  88. else if(wartosc == SIGTERM){
  89. exit(0); //Zakonczenie dzialania procesu
  90. }
  91. }
  92.  
  93. void obsluga_sygnalu(int wartosc){
  94. wczytaj_pidy();
  95.  
  96. struct msgbuf xd; //tworze strukture xd do wysylania komunikatow
  97. xd.komunikat = wartosc; //wartosc to nr sygnalu
  98. int i;
  99. for (i = 0; i < 3; i++)
  100. {
  101. xd.mtype = pidy[i];
  102. wyslij_komunikat(qid, &xd); //wysylamy komunikat ze otrzymalismy sygnal
  103. }
  104.  
  105. for (i = 0; i < 3; i++)
  106. {
  107. if(getpid() != pidy[i]) //Wyslij sygnal4 ktory powiadamioa ze mozliwy jest odczyt z kolejki
  108. kill(pidy[i], SIGCONT);
  109. }
  110.  
  111. sygnaly_dalsze_dzialanie();
  112. }
  113.  
  114. int main()
  115. {
  116. pipe(pdes1);
  117. pipe(pdes2);
  118.  
  119. signal(SIGUSR1, obsluga_sygnalu);
  120. signal(SIGUSR2, obsluga_sygnalu);
  121. signal(SIGTERM, obsluga_sygnalu);
  122. signal(SIGCONT, obsluga_sygnalu);
  123.  
  124. __key_t klucz_kolejki = ftok(".", 'C');
  125. qid = msgget(klucz_kolejki, IPC_CREAT | 0660); //tworze kolejke
  126.  
  127. if ((pidy[0] = fork()) == 0)
  128. {
  129. close(pdes1[READ]);
  130. char text[MAX_TEXT_LENGTH] = {};
  131.  
  132. while (1)
  133. {
  134. while (flaga);
  135.  
  136. if (fgets(text, MAX_TEXT_LENGTH, stdin) != NULL)
  137. write(pdes1[WRITE], text, strlen(text) + 1); // + 1 bo 0 końca stringa
  138. }
  139. }
  140.  
  141. if ((pidy[1] = fork()) == 0)
  142. {
  143. close(pdes1[WRITE]);
  144. close(pdes2[READ]);
  145.  
  146. char text[MAX_TEXT_LENGTH] = {};
  147. char payload[MAX_TEXT_LENGTH] = {};
  148. char *token;
  149. int liczba;
  150.  
  151. int index = 0;
  152. int n;
  153.  
  154. int poprawne_dane = 1;
  155.  
  156. while (1)
  157. {
  158. while (flaga);
  159.  
  160. wypelnij_tablice_zerami(text, MAX_TEXT_LENGTH);
  161. wypelnij_tablice_zerami(payload, MAX_TEXT_LENGTH);
  162. index = 0;
  163. poprawne_dane = 1;
  164.  
  165. n = read(pdes1[READ], text, MAX_TEXT_LENGTH);
  166.  
  167. // nadpisanie znaku nowej linii
  168. text[n - 2] = 0;
  169.  
  170. if (text[n - 3] < '0' || text[n - 3] > '9')
  171. {
  172. printf("Podano nieprawidlowe dane wejsciowe!\n");
  173. continue;
  174. }
  175.  
  176. token = strtok(text, "+"); //dzili string na segment co +
  177.  
  178. while (token != NULL)
  179. {
  180. if (!moze_byc_liczba(token, strlen(token)) || (token[0] == '0' && strlen(token) > 1))
  181. {
  182. printf("Podano nieprawidlowe dane wejsciowe!\n");
  183. poprawne_dane = 0;
  184. break;
  185. }
  186.  
  187. liczba = atoi(token); //zwraca string jako int
  188.  
  189. if (liczba == 0 && strlen(token) > 1)
  190. {
  191. printf("Podano nieprawidlowe dane wejsciowe!\n");
  192. poprawne_dane = 0;
  193. break;
  194. }
  195.  
  196. n = strlen(token);
  197. strncpy(&payload[index], token, n);
  198. index += n;
  199. strncpy(&payload[index], znak, 1);
  200. index++;
  201. token = strtok(NULL, "+"); //wczytuje dane dopuki nie napotka plusa
  202. }
  203.  
  204. if (poprawne_dane)
  205. write(pdes2[WRITE], payload, strlen(payload)); //wysyłamy dane do procesu 3
  206. }
  207. }
  208.  
  209. if ((pidy[2] = fork()) == 0)
  210. {
  211. close(pdes2[WRITE]);
  212.  
  213. int suma = 0;
  214. char text[MAX_TEXT_LENGTH] = {};
  215. char *token;
  216.  
  217. while (1)
  218. {
  219. while (flaga);
  220.  
  221. read(pdes2[READ], text, MAX_TEXT_LENGTH);
  222. token = strtok(text, znak);
  223.  
  224. while (token != NULL)
  225. {
  226. suma += atoi(token);
  227. token = strtok(NULL, znak);
  228. }
  229.  
  230. printf("Suma: %d\n", suma);
  231. wypelnij_tablice_zerami(text, MAX_TEXT_LENGTH);
  232. suma = 0;
  233. }
  234. }
  235.  
  236. FILE *f = fopen(PLIK_Z_PIDAMI, "w");
  237.  
  238. int z;
  239. for (z = 0; z < 3; z++)
  240. fprintf(f, "%d\n", pidy[z]);
  241.  
  242. fclose(f);
  243.  
  244. waitpid(pidy[2], NULL, 0);
  245. waitpid(pidy[1], NULL, 0);
  246. waitpid(pidy[0], NULL, 0);
  247.  
  248. close(pdes1[READ]);
  249. close(pdes1[WRITE]);
  250. close(pdes2[READ]);
  251. close(pdes2[WRITE]);
  252.  
  253. usun_kolejke(qid);
  254. remove(PLIK_Z_PIDAMI);
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement