// A demonstration of unix pipes where the output of the parent
// program is redirected to the input of a child
#include <iostream>
#include <unistd.h>

using namespace std;

void parent_main();
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) {
   //parent process does the writing
   dup2(p[1], STDOUT_FILENO);

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

   parent_main();
 } 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() {
 for(int i=0; i<10; i++) {
   cout << i << endl;
 }
}

void
child_main() {
 //read the entire input stream
 int i;
 while(cin >> i) {
   cout << "Child received: " << i << endl;
 }

}