#include #include #include #include #include using namespace std; int main() { int fd[2]; int info; if (pipe(fd)) { perror("Pipe failed"); return 1; } // form the reader end of the pipe switch(fork()) { case 0: // close the write end of the pipe, we will not need it. close(fd[1]); // dup the read end of the pipe to stdin if( -1 == dup2(fd[0],STDIN_FILENO)) { perror("\tError dup2 :"); } execl("talker","talker",nullptr); perror("Exec failed"); return 3; case -1: perror("Fork failed"); return 2; } const char * a="A message from the parent\n"; write(fd[1],a, strlen(a)); // form the writer end of the pipe switch(fork()) { case 0: // close the read end of the pipe, we will not need it. close(fd[0]); // dup the write end of the pipe to stdin if (-1 == dup2(fd[1],STDOUT_FILENO)) { perror("\tError dup2 :"); } execl("smallCounter","smallCounter",nullptr); perror("Exec failed"); return 3; case -1: perror("Fork failed"); return 2; } // close the pipe ends, I don't need them. // this is important, if you don't do this, the reader will not get // and eof when the writer closes the pipe. close(fd[0]); close(fd[1]); int count = 0; while (count < 2) { pid_t pid = wait(&info); count ++; cout << "A child has returned (" << pid << ") " << endl; } cout << endl; cout << "All children have exited " << endl; cout << endl; return 0; }