Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. void pipes(node_t *node)
  2. {
  3.     debug();
  4.     int pipefd[2];
  5.     pid_t cpid1;
  6.     pid_t cpid2;
  7.     if (pipe(pipefd) == -1)
  8.     {
  9.         perror("pipe");
  10.         _exit(EXIT_FAILURE);
  11.     }
  12.  
  13.     cpid1 = fork();
  14.     if (cpid1 == 0)
  15.     {
  16.         close(1);
  17.         if(dup2(pipefd[1], 1) == -1) perror("dup2"); if(close(pipefd[1]) == -1) perror("close");
  18.         close(pipefd[0]);
  19.         if(execvp(node->pipe.parts[0]->command.program,node->pipe.parts[0]->command.argv)==0) perror("Error pipe");
  20.         }
  21.          
  22.    
  23.     else
  24.     {
  25.         cpid2 = fork();
  26.         if (cpid2 == 0)
  27.         {  
  28.             close(0);
  29.             if(dup2(pipefd[0], 0) == -1) perror("dup2"); if(close(pipefd[0]) == -1) perror("close");
  30.             close(pipefd[1]);
  31.             if(execvp(node->pipe.parts[1]->command.program,node->pipe.parts[1]->command.argv)==0) perror("Error pipe");
  32.  
  33.         }
  34.  
  35.         else
  36.         {
  37.             exit(-1);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement