// A demonstration of unix pipes where string are written
// to the input of a child
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <string>

using namespace std;

void parent_main(int fd);
void child_main();

int main(void) {
 int p[2];
 int i;
 int pid;

 //create the pipe
 pipe(p);

 //fork the process
 pid = fork();

 //set up the pipe
 if(pid) {
   //close the unused side of the pipe
   close(p[0]);

   parent_main(p[1]);
 } else {
   //child process does the reading
   dup2(p[0], STDIN_FILENO);

   //close the unused side of the pipe
   close(p[1]);

   child_main();
 }


 return 0;
}



void
parent_main(int fd) {
 const char *s = "Hello, world\n";
 for(int i=0; i<10; i++) {
   write(fd, s, strlen(s));
 }
}

void
child_main() {
 //read the entire input stream
 string s;

 while(!cin.eof()) {
   getline(cin, s);
   cout << "Child received: " << s << endl;
 }

}