Because parent/child processes do not share any memory space, they cannot communicate by variables. The most common way for a parent/child process pair to communicate is via files (stdin and stdout) or sockets.
Particularly, because the parent and child processes share file descriptors, we can use a pipe to communicate. The following code snippet creates a pipe to let the parent and child communicate:
After this code, the child and parent processes can use their respective fromMeFd and toMeFd file descriptors to communicate with the other process in a full duplex (concurrent read/write) way.
While this solution works, it has its potential problems. Particularly, both processes use the same stdin, stdout and stderr open file descriptors. When both processes write to stdout at the same time, the output may interleave in any arbitrary order. When both processes read from stdin, and stdin corresponds to a console, then only one process may get the “foreground” of the console.
The process that does not have the “foreground” will be blocked until the “foreground” is switched. On a command line, the switch by the using control-Z to pause a process, optionally use bg to switch the paused process to the background, and fg to bring another process to the foreground.