Advertisement
VisualPaul

Un

Jan 17th, 2012
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. void print_usage_info(const char *program);
  6. int main(int argc, char **argv)
  7. {
  8.    
  9.     if (argc != 2 + 1)
  10.         print_usage_info(argv[0]);
  11.     int pip[2][2]; /* Pipe 1: program1 -> program2 */
  12.                    /* Pipe 2: program1 <- program2 */
  13.     for (int i = 0; i < 2; ++i)
  14.         pipe(pip[i]);
  15.     /* Original process will execute 1st program, while other will take care about 2nd */
  16.     if (fork()) {
  17.         close(pip[0][0]);
  18.         close(pip[1][1]);
  19.         dup2(pip[1][0], 0);
  20.         dup2(pip[0][1], 1);
  21.         execl(argv[1], argv[1], NULL);
  22.     } else {
  23.         close(pip[0][1]);
  24.         close(pip[1][0]);
  25.         dup2(pip[0][0], 0);
  26.         dup2(pip[1][1], 1);
  27.         execl(argv[2], argv[2], NULL);
  28.     }
  29.     fprintf(stderr, "How did I got here: file %s, line %d\n", __FILE__, __LINE__);
  30.     return -1;
  31. }
  32.  
  33. void print_usage_info(const char *program)
  34. {
  35.     fprintf(stderr, "%s: incorrect arguments. Usage:\n%s program1 program2\n", program, program);
  36.     exit(1);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement