Advertisement
Guest User

16

a guest
Mar 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1.  
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <wait.h>
  6.  
  7. int create_childs(int amount) {
  8.     pid_t pid;
  9.     int i;
  10.  
  11.     for (i = 0; i < amount; i++) {
  12.         pid = fork();
  13.         if (pid == 0) {
  14.             return i; // Filhos
  15.         } else if (pid < 0) {
  16.             puts("Erro ao fazer fork");
  17.             exit(-1);
  18.         }
  19.     }
  20.  
  21.     return amount; // Pai
  22. }
  23.  
  24. void duplicate(int descriptor1, int descriptor2) {
  25.     if (dup2(descriptor1, descriptor2) == -1) {
  26.         puts("Nao foi possivel trocar o descritor");
  27.         exit(-1);
  28.     }
  29. }
  30.  
  31. void close_descriptor(int descriptor) {
  32.     if (close(descriptor) == -1) {
  33.         printf("Nao foi possivel fechar o pipe %d\n", descriptor);
  34.         exit(-1);
  35.     }
  36. }
  37.  
  38. void create_pipe(int fd[2]) {
  39.     if (pipe(fd) == -1) {
  40.         puts("Erro ao criar o pipe");
  41.         exit(-1);
  42.     }
  43. }
  44.  
  45. void ls(int ls_to_sort[2], int sort_to_wc[2]) {
  46.     close_descriptor(ls_to_sort[0]);
  47.     close_descriptor(sort_to_wc[0]);
  48.     close_descriptor(sort_to_wc[1]);
  49.  
  50.     duplicate(ls_to_sort[1], fileno(stdout));
  51.  
  52.     if (execlp("ls", "ls", "-la", (char *) NULL) == -1) {
  53.         puts("O exec do ls não teve sucesso");
  54.     }
  55.  
  56.     close_descriptor(ls_to_sort[1]);
  57. }
  58.  
  59. void sort(int ls_to_sort[2], int sort_to_wc[2]) {
  60.     close_descriptor(ls_to_sort[1]);
  61.     close_descriptor(sort_to_wc[0]);
  62.  
  63.     duplicate(ls_to_sort[0], fileno(stdin));
  64.     duplicate(sort_to_wc[1], fileno(stdout));
  65.  
  66.     if (execlp("sort", "sort", (char *) NULL) == -1) {
  67.         puts("O exec do sort não teve sucesso");
  68.     }
  69.  
  70.     close_descriptor(sort_to_wc[1]);
  71.     close_descriptor(ls_to_sort[0]);
  72. }
  73.  
  74. void wc(int ls_to_sort[2], int sort_to_wc[2]) {
  75.     close_descriptor(ls_to_sort[0]);
  76.     close_descriptor(ls_to_sort[1]);
  77.     close_descriptor(sort_to_wc[1]);
  78.  
  79.     duplicate(sort_to_wc[0], fileno(stdin));
  80.     if (execlp("wc", "wc", "-l", (char *) NULL) == -1) {
  81.         puts("O exec do wc não teve sucesso");
  82.     }
  83.  
  84.     close_descriptor(sort_to_wc[0]);
  85. }
  86.  
  87. int main() {
  88.     int id, ls_to_sort[2], sort_to_wc[2];
  89.  
  90.     create_pipe(ls_to_sort);
  91.     create_pipe(sort_to_wc);
  92.  
  93.     id = create_childs(2);
  94.     switch (id) {
  95.         case 0:
  96.             ls(ls_to_sort, sort_to_wc);
  97.             break;
  98.         case 1:
  99.             sort(ls_to_sort, sort_to_wc);
  100.             break;
  101.         case 2:
  102.             wc(ls_to_sort, sort_to_wc);
  103.             break;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement