Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <signal.h>
  5. #include <sys/wait.h>
  6. #include <stdlib.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9.  
  10. int pfd[2][2];
  11. int pid1, pid2, pid3;
  12. int letters, lowercase;
  13. int count = 0;
  14.  
  15.  
  16. void fiu(int p){
  17. printf("KKK\n");
  18. close(pfd[p][1]);
  19. int letters = 0;
  20. int rd, size = 1;
  21. char buffer[size];
  22.  
  23. while((rd = read(pfd[p][0], buffer, size)) != 0){
  24. printf("%s \n", buffer);
  25. letters++;
  26. }
  27.  
  28. sleep(4);
  29. close(pfd[p][0]);
  30. printf("letters: %d \n", letters);
  31. exit(letters);
  32. }
  33.  
  34. void fiu3(char *file){
  35. close(pfd[0][0]);
  36. close(pfd[0][1]);
  37. close(pfd[1][0]);
  38. close(pfd[1][1]);
  39. sleep(2);
  40. execlp("wc", "wc", "-w", file, (char*)0);
  41.  
  42. printf("Error at exec \n");
  43. exit(1);
  44.  
  45. }
  46.  
  47. void fiu_ends(int sig){
  48. int status, pid;
  49. printf("LLL\n");
  50.  
  51. if ((pid = wait(&status)) < 0) {
  52. printf("error wait \n");
  53. exit(1);
  54. }
  55.  
  56. printf("Fiul cu pid=%d exitcode=%d \n", pid, WEXITSTATUS(status));
  57.  
  58.  
  59. if (pid1 == pid){
  60. letters = WEXITSTATUS(status);
  61. }else if (pid2 == pid){
  62. lowercase = WEXITSTATUS(status);
  63. }
  64.  
  65. count++;
  66. if (count == 3){
  67. printf("Toti fiii s-au terminat\n");
  68.  
  69. printf("GATA \n ");
  70. exit(0);
  71. }
  72.  
  73. }
  74.  
  75. void parent(char *file){
  76. close(pfd[0][0]);
  77. close(pfd[1][0]);
  78.  
  79. struct sigaction act;
  80. act.sa_handler = fiu_ends;
  81. if (sigaction(SIGCHLD, &act, NULL) < 0){
  82. printf("error sigaction \n");
  83. exit(1);
  84. }
  85.  
  86. int fd;
  87. if ((fd = open(file, O_RDONLY)) < 0){
  88. printf("Cannot open file \n");
  89. exit(1);
  90. }
  91.  
  92. int rd, size = 512;
  93. char buffer[size];
  94.  
  95. while((rd = read(fd, &buffer, size)) != 0){
  96. for (int i = 0; i < rd; i++){
  97. printf("buff : %d \n", buffer[i]);
  98. if ((buffer[i] >= 'A' && buffer[i] <= 'Z') || (buffer[i] >='a' && buffer[i] <= 'z')) {
  99.  
  100. write(pfd[0][1], buffer[i], 1);
  101. }
  102.  
  103. if (buffer[i] >='a' && buffer[i] <= 'z') {
  104.  
  105. write(pfd[1][1], buffer[i], 1);
  106. }
  107.  
  108. }
  109.  
  110. }
  111.  
  112. close(pfd[0][1]);
  113. close(pfd[1][1]);
  114.  
  115.  
  116.  
  117.  
  118.  
  119. }
  120.  
  121.  
  122. int main(int argc, char *argv[]){
  123.  
  124. if (argc < 2){
  125. printf("Usage: %s file\n", argv[0]);
  126. exit(1);
  127. }
  128.  
  129. if (pipe(pfd[0]) < 0){
  130. printf("Error pipe 1\n");
  131. exit(1);
  132. }
  133.  
  134. if (pipe(pfd[1]) < 0){
  135. printf("Error pipe 2\n");
  136. exit(1);
  137. }
  138.  
  139. if ((pid1 = fork()) < 0){
  140. printf("Error fork 1\n");
  141. exit(1);
  142.  
  143. }else if (pid1 == 0){
  144.  
  145. fiu(0);
  146.  
  147. }
  148.  
  149. if ((pid2 = fork()) < 0){
  150. printf("Error fork 2\n");
  151. exit(1);
  152.  
  153. }else if (pid2 == 0){
  154.  
  155. fiu(1);
  156.  
  157.  
  158. }
  159.  
  160.  
  161. if ((pid3 = fork()) < 0){
  162. printf("Error fork 3\n");
  163. exit(1);
  164.  
  165. }else if (pid3 == 0){
  166.  
  167. fiu3(argv[1]);
  168.  
  169.  
  170. }
  171.  
  172.  
  173. parent(argv[1]);
  174. return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement