Advertisement
szilard-dobai

Untitled

Dec 9th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <wait.h>
  9.  
  10. void verifSig(int sig)
  11. {
  12. if(sig == SIGUSR1)
  13. {
  14. printf("C\n");
  15. }
  16. if(sig == SIGALRM)
  17. {
  18. printf("1\n");
  19.  
  20. }
  21. alarm(1);
  22. }
  23.  
  24. void handler(int sig)
  25. {
  26. if (sig==SIGCHLD)
  27. {
  28. pid_t pid;
  29. pid = wait(NULL);
  30. printf("Pid %d exited.\n", pid);
  31. }
  32. }
  33.  
  34. int main(int argc, char* argv[])
  35. {
  36. if(argc != 3)
  37. {
  38. perror("nr gresit de arg");
  39. exit(1);
  40. }
  41.  
  42. int pfd1[2], pfd2[2];
  43. int pid1, pid2, pid3;
  44. int fd1=open(argv[1],O_RDONLY);
  45. int fd2=open(argv[2],O_RDONLY);
  46.  
  47. struct sigaction sigActAlarm;
  48. sigActAlarm.sa_handler = verifSig;
  49. sigActAlarm.sa_flags = 0;
  50.  
  51. int rez;
  52. if((rez = sigaction(SIGALRM, &sigActAlarm, NULL)) < 0)
  53. {
  54. perror("eroare la sigalrm");
  55. exit(2);
  56. }
  57. if((rez = sigaction(SIGUSR1, &sigActAlarm, NULL)) < 0)
  58. {
  59. perror("eroare la sigusr1");
  60. exit(2);
  61. }
  62.  
  63. struct sigaction sigActChld;
  64. sigActChld.sa_handler = handler;
  65. sigActChld.sa_flags = 0;
  66.  
  67.  
  68. if((fd1 < 0) || (fd2 < 0))
  69. {
  70. perror("eroare la deschidere fisiere");
  71. exit(2);
  72. }
  73.  
  74. if((pipe(pfd1) < 0) || (pipe(pfd2) < 0))
  75. {
  76. perror("eroare la unul din pipes");
  77. exit(3);
  78. }
  79.  
  80. //COPIL 1
  81. if((pid1 = fork()) < 0)
  82. {
  83. perror("nu s-a creat primul copil");
  84. exit(-1);
  85. }
  86. else if(pid1 == 0)
  87. {
  88. close(pfd1[0]);
  89. close(pfd2[1]);
  90. close(pfd2[0]);
  91.  
  92. dup2(pfd1[1],1);
  93. execlp("cat", "cat", argv[1], NULL);
  94. printf("daca ajung aici, e eroare");
  95. exit(71);
  96. }
  97.  
  98. //COPIL 2
  99. if((pid2 = fork()) < 0)
  100. {
  101. perror("nu s-a creat al doilea copil");
  102. exit(2);
  103. }
  104. else if(pid2 == 0)
  105. {
  106. close(pfd1[0]);
  107. close(pfd1[1]);
  108. close(pfd2[0]);
  109.  
  110. dup2(pfd2[1],1);
  111. execlp("cat", "cat", argv[2], NULL);
  112. printf("daca ajung aici, e eroare");
  113. exit(72);
  114. }
  115.  
  116. //COPIL 3
  117. if((pid3 = fork()) < 0)
  118. {
  119. perror("nu s-a creat al treilea copil");
  120. exit(2);
  121. }
  122. else if(pid3 == 0)
  123. {
  124. close(pfd1[1]);
  125. close(pfd2[1]);
  126.  
  127. int nrc = 0;
  128. char buf1,buf2;
  129. printf("\n");
  130. while(read(pfd1[0], &buf1, 1) > 0)
  131. {
  132. if(buf1 >= '0' && buf1 <= '9')
  133. {
  134. nrc ++;
  135. kill(getppid(), SIGUSR1);
  136. }
  137. printf("%c", buf1);
  138. }
  139. printf("\n");
  140. while(read(pfd2[0], &buf2, 1) > 0)
  141. {
  142. if(buf2 >= '0' && buf2 <= '9')
  143. {
  144. nrc ++;
  145. kill(getppid(), SIGUSR1);
  146. }
  147. printf("%c", buf2);
  148. }
  149. printf("\n");
  150. exit(nrc);
  151. }
  152.  
  153. if((rez = sigaction(SIGCHLD, &sigActChld, NULL)) > 0)
  154. {
  155. perror("eroare la sigchld");
  156. exit(2);
  157. }
  158. kill(getpid(),SIGCHLD);
  159.  
  160. alarm(1);
  161.  
  162. close(pfd1[0]);
  163. close(pfd1[1]);
  164. close(pfd2[0]);
  165. close(pfd2[1]);
  166.  
  167. while(1);
  168. return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement