szilard-dobai

SO - Test Procese - Sub

Dec 9th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. #include <signal.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9.  
  10.  
  11. int pid_c1;
  12. int pid_c2;
  13. int pid_c3;
  14. int pid_p;
  15. int status;
  16. char litera;
  17.  
  18. void rutina_sc (int a) {
  19. int pid = wait(&status);
  20. printf("copil %d terminat cu %d\n", pid, WEXITSTATUS(status));
  21. }
  22.  
  23. void rutina_alarm (int a) {
  24. printf("1\n");
  25. alarm(1);
  26. }
  27.  
  28. void rutina_usr (int a) {
  29. printf("C\n");
  30. }
  31.  
  32. int main (int argc, char* argv[]) {
  33. int pfd13[2], pfd23[2];
  34. char buff[1000];
  35. int contor_cifre=0;
  36. int buff_size;
  37. int i;
  38.  
  39. pid_p = getpid();
  40.  
  41. struct sigaction sigActSC;
  42. sigActSC.sa_handler = rutina_sc;
  43. sigActSC.sa_flags = 0;
  44.  
  45. struct sigaction sigActUSR;
  46. sigActUSR.sa_handler = rutina_usr;
  47. sigActUSR.sa_flags = 0;
  48.  
  49. struct sigaction sigActALR;
  50. sigActALR.sa_handler = rutina_alarm;
  51. sigActALR.sa_flags = 0;
  52.  
  53. sigaction(SIGCHLD, &sigActSC, NULL);
  54. sigaction(SIGUSR1, &sigActUSR, NULL);
  55.  
  56. if (argc != 3) {
  57. printf("wrong arguments\n");
  58. exit(-1);
  59. }
  60.  
  61. if (pipe(pfd13) < 0) {
  62. printf("error pipe 1-3\n");
  63. exit(-2);
  64. }
  65.  
  66. if (pipe(pfd23) < 0) {
  67. printf("error pipe 2-3\n");
  68. exit(-3);
  69. }
  70.  
  71. if ((pid_c1 = fork()) < 0) {
  72. printf("error forking 1\n");
  73. exit(-4);
  74. } else if (pid_c1 == 0) {
  75. close(pfd13[0]);
  76. close(pfd23[0]);
  77. close(pfd23[1]);
  78.  
  79. dup2(pfd13[1], 1);
  80. execlp("cat", "cat", argv[1], NULL);
  81. printf("\n");
  82. close(pfd13[1]);
  83.  
  84. //kill(pid_p, SIGCHLD);
  85. exit(1);
  86. } else if ((pid_c2 = fork()) < 0) {
  87. printf("error forking 2\n");
  88. exit(-5);
  89. } else if (pid_c2 == 0) {
  90. close(pfd13[0]);
  91. close(pfd13[1]);
  92. close(pfd23[0]);
  93.  
  94. dup2(pfd23[1], 1);
  95. execlp("cat", "cat", argv[2], NULL);
  96. printf("\n");
  97. close(pfd23[1]);
  98. //printf("ana");
  99.  
  100.  
  101. //kill(pid_p, SIGCHLD);
  102. exit(1);
  103. } else if ((pid_c3 = fork()) < 0) {
  104. printf("error forking 3\n");
  105. exit(-6);
  106. } else if (pid_c3 == 0) {
  107. close(pfd13[1]);
  108. close(pfd23[1]);
  109.  
  110. while (buff_size = read(pfd13[0], &buff, 100)) {
  111. for(i=0; i<buff_size; i++) {
  112. if(buff[i] >= '0' && buff[i] <= '9') {
  113. contor_cifre++;
  114. if (kill(pid_p, SIGUSR1) == 0) {
  115. //printf("error sigusr1\n");
  116. }
  117. }
  118. }
  119. }
  120. close(pfd13[0]);
  121.  
  122. while (buff_size = read(pfd23[0], &buff, 1000)) {
  123. for(i=0; i<buff_size; i++) {
  124. if(buff[i] >= '0' && buff[i] <= '9') {
  125. contor_cifre++;
  126. kill(pid_p, SIGUSR1);
  127. }
  128. }
  129. }
  130. close(pfd23[0]);
  131.  
  132. //kill(pid_p, SIGCHLD);
  133. exit(contor_cifre);
  134. } else {
  135. close(pfd13[0]);
  136. close(pfd13[1]);
  137. close(pfd23[0]);
  138. close(pfd23[1]);
  139.  
  140. sigaction(SIGALRM, &sigActALR, NULL);
  141.  
  142. alarm(1);
  143. while(1);
  144. exit(4);
  145. }
  146.  
  147. }
Add Comment
Please, Sign In to add comment