I’ve run into some troubles lately trying to communicate processes through C++ STL-like streams with fork() and pipe() so I’ve created an example of how can this be done in a POSIX environment using the GCC’s libstdc++ (GCC >= 3.4.0).
The scenario is: A parent process wants to handle another program’s input and output streams, a process-level wrapper.
So the example is: A parent process forks into another child process. The parent sends the child a message, the child receives it and outputs it to the parent to show the communication works just fine.
The forked process calls the program we want to control. Which in this case is the easiest C++ program ever: Just reads a line from the standard input and outputs a message containing the line just read through the standard output. In addition, another message is sent through the standard error stream. seguir leyendo…





