#include #include #include int main(int argc, char *argv[]) { int p[2]; int pid; int res; char buf[100]; int status; if(argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } again: res= pipe(p); if(res < 0) { perror("pipe"); exit(1); } pid= fork(); if(pid == -1) { perror("fork"); exit(1); } else if(pid == 0) { /* Child. */ res= dup2(p[1], 1); if(res < 0) { perror("dup2"); exit(1); } close(p[0]); close(p[1]); res= execl(argv[1], argv[1], 0); if(res < 0) { perror("exec"); exit(1); } } else { /* Parent. */ close(p[1]); do { res= read(p[0], buf, sizeof(buf)); } while(res > 0); close(p[0]); res= waitpid(pid, &status, 0); if(res == -1) { perror("wait4"); exit(1); } printf("Child=%d waitpid=%d\n", pid, res); goto again; } }