Right before you do the open(RCMD,...), fork(). Have the parent do a can_read on STDIN. Have it be prepared to receive SIGCHLD. The child just runs the code that you give above, then exits().