Advertisement
razvanth21

Untitled

Dec 10th, 2018
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. /* prog file
  2. Fiul 1 transmite continutul <file> catre fiul 2, care filtreaza liniile ce contin cel putin o cifra ([0-9]) si le transmite catre fiul 3.
  3. Al treilea fiu afiseaza numarul de linii.
  4. Parintele si fiii se executa in paralel!
  5. Parintele afiseaza ... pana la term tuturor fiilor si afiseaza codurile de retur pe masura ce se termina fiecare fiu.
  6. */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <string.h>
  16.  
  17. int main()
  18. {
  19.     int pfd1[2];
  20.     int pfd2[2];
  21.     int pfd3[2];
  22.  
  23.     int status1, status2;
  24.  
  25.     pid_t pid[2];
  26.  
  27.     if (pipe(pfd1) < 0)
  28.     {
  29.         perror("eroare pipe 1");
  30.         exit(EXIT_FAILURE);
  31.     }
  32.  
  33.     if (pipe(pfd2) < 0)
  34.     {
  35.         perror("eroare pipe 2");
  36.         exit(EXIT_FAILURE);
  37.     }
  38.  
  39.     if (pipe(pfd3) < 0)
  40.     {
  41.         perror("eroare pipe 2");
  42.         exit(EXIT_FAILURE);
  43.     }
  44.  
  45.     if ((pid[0] = fork()) < 0)
  46.     {
  47.         perror("eroare fork 1");
  48.         exit(EXIT_FAILURE);
  49.     }
  50.  
  51.     if (pid[0] == 0)
  52.     {
  53.         if ((pid[1] = fork()) < 0)
  54.         {
  55.             perror("eroare fork 2");
  56.             exit(EXIT_FAILURE);
  57.         }
  58.         if (pid[1] == 0) // cod fiu 2
  59.         {
  60.             char block[32];
  61.             int i, r;
  62.  
  63.             close(pfd2[1]);
  64.             close(pfd1[0]);
  65.             close(pfd1[1]);
  66.             close(pfd3[0]);
  67.  
  68.             int v[26] = {0};
  69.  
  70.             while ((r = read(pfd2[0], block, sizeof(block))))
  71.             {
  72.                 for (i = 0; i < r; i++)
  73.                     v[block[i] - 'a'] ++;
  74.             }
  75.  
  76.             int fd;
  77.  
  78.             if ((fd = open("statistica.txt", O_WRONLY | O_TRUNC | O_CREAT, 0666)) == -1)
  79.             {
  80.                 perror("eroare statistica");
  81.                 exit(EXIT_FAILURE);
  82.             }
  83.  
  84.             char str[32];
  85.             int k = 0;
  86.  
  87.             for (i = 0; i < 26; i++)
  88.             {
  89.                 if (v[i] > 0)
  90.                 {
  91.                     sprintf(str, "%c - %d\n", i + 'a', v[i]);
  92.                     write(fd, str, strlen(str));
  93.  
  94.                     k ++;
  95.                 }
  96.             }
  97.  
  98.             sprintf(str, "%d litere distincte\n", k);
  99.             write(pfd3[1], str, strlen(str));
  100.  
  101.             close(fd);
  102.             close(pfd2[0]);
  103.             close(pfd3[1]);
  104.             exit(2);
  105.         }
  106.         else // cod fiu 1
  107.         {
  108.             char block[32];
  109.             int i, r;
  110.  
  111.             close(pfd1[1]);
  112.             close(pfd2[0]);
  113.             close(pfd3[0]);
  114.             close(pfd3[1]);
  115.  
  116.             while ((r = read(pfd1[0], block, sizeof(block))))
  117.             {
  118.                 for (i = 0; i < r; i++)
  119.                 {
  120.                     if (block[i] >= 'a' && block[i] <= 'z')
  121.                         write(pfd2[1], &block[i], sizeof(char));
  122.                 }
  123.             }
  124.  
  125.             close(pfd2[1]);
  126.             close(pfd1[0]);
  127.  
  128.             exit(1);
  129.         }
  130.     }
  131.     else // cod parinte
  132.     {
  133.         int fd;
  134.         char block[32];
  135.  
  136.         if ((fd = open("date.txt", O_RDONLY)) == -1)
  137.         {
  138.             perror("eroare fisier");
  139.             exit(EXIT_FAILURE);
  140.         }
  141.  
  142.         int r, i;
  143.  
  144.         close(pfd1[0]);
  145.         close(pfd2[0]);
  146.         close(pfd2[1]);
  147.         close(pfd3[1]);
  148.  
  149.         while ((r = read(fd, block, sizeof(block))))
  150.         {
  151.             for (i = 0; i < r; i++)
  152.                 write(pfd1[1], &block[i], sizeof(char));
  153.         }
  154.  
  155.         close(pfd1[1]);
  156.         close(fd);
  157.  
  158.         while ((r = read(pfd3[0], block, sizeof(block))))
  159.         {
  160.             for (i = 0; i < r; i++)
  161.                 printf("%c", block[i]);
  162.         }
  163.  
  164.         close(pfd3[0]);
  165.  
  166.         wait(&status1);
  167.         wait(&status2);
  168.     }
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement