#include #include #include #include #include using namespace std; vector words{"now", "is", "the time" , "to", "STOP"}; int main() { int fd[2]; if (-1 == pipe(fd)) { perror("Pipe failed"); exit(1); } pid_t pid = fork(); if (pid == -1) { perror("Fork failed"); exit(1); } if (pid == 0) { // this is the child and it will write close(STDOUT_FILENO); dup(fd[STDOUT_FILENO]); close (fd[STDIN_FILENO]); cout << "This is a string" << endl; for (auto word: words) { cout << word << endl; } exit(0); } else { // the parent will read. string phrase; close(STDIN_FILENO); dup(fd[STDIN_FILENO]); close (fd[STDOUT_FILENO]); getline(cin, phrase); while(cin) { cout << phrase << endl; getline(cin,phrase); } exit(0); } // never encountered return 0; }