#include #include #include #include #include #include using namespace std; int main(int argc, char **argv) { int fds[2]; pid_t pid; if (pipe(fds) == -1) { // create pipe perror("pipe failed"); exit(1); } if ((pid = fork()) < 0) { // spawn child perror("fork failed"); exit(2); } if (pid == 0) { // child close(fds[1]); // close output char c; while (read(fds[0], &c, 1) > 0) { cout << "read: " << c << endl; } close(fds[0]); // close input cout << "child done" << endl; } else { // parent close(fds[0]); // close input char msg[] = "hello..world.."; write(fds[1], msg, strlen(msg)); close(fds[1]); // close output -> EOF waitpid(pid, 0, 0); // wait for child cout << "parent done" << endl; } exit(0); }