#include #include #include /* to test how pipe '|' works. */ int main(void) { int pid; int exam; char buf[BUFSIZ]; int fd[2]; /* used for pipe */ /* this message actually goes to the screen */ printf("hello before we are in pipe \n"); /* now create a pipe */ if (pipe(fd) == -1) { perror(" pipe failed\n"); exit(1); } if ((pid = fork()) == -1) { perror(" fork failed \n"); exit(2); } if (pid == 0) /* child process */ { close(0); /* close the stdin */ dup(fd[0]); /* dup() uses fd[0] (stdin) */ /* because the close(0) call was */ /* right before it. At this point, */ /* '1' becomes the lowest un-used */ /* file id number. Thus, it (1) */ /* will be used to 'dup' fid. This */ /* is exactly what we want Now all */ /* the output to stdout will be */ /* redirected to the file that */ /* 'fid' was associated with. */ execlp("./dummy",NULL); perror("error in execvp\n"); /* scanf("%s",buf); printf("%s\n",buf); */ } else /* parent process */ { close(1); /* close stdout */ dup(fd[1]); printf("message-from-parent\n"); wait(NULL); /* waiting for child to terminate */ } return 0; }