Advertisement
paulogp

Processos e pipes

Jul 14th, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. /* pretende-se que o filho termine depois do pai usando pipes */
  2. // processos e pipes (pipes, fork)
  3.  
  4. // Apple Xcode
  5. // paulogp
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <unistd.h>
  10.  
  11. int main (int argc, const char * argv[])
  12. {
  13.     int the_pid;
  14.     int the_pfd[2];
  15.     char the_char;
  16.    
  17.     // postfix delivery to external command
  18.     // the pipe daemon processes requests from the Postfix
  19.     // queue manager to deliver messages to external commands
  20.     pipe(the_pfd); // uma vez mais, sem verificacao de erros
  21.    
  22.     the_pid = fork();
  23.     if (the_pid == 0)
  24.     {
  25.         read(the_pfd[0], &the_char, 1); // wait for parent
  26.         printf("%d\n", getpid());
  27.     }
  28.     else
  29.     {
  30.         printf("%d\n", getpid());
  31.         write(the_pfd[1], "c", 1);
  32.     }
  33.    
  34.     printf("\ndone\n");
  35.    
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement