How should I run another program from within my C
program? I need to be able to write data into STDIN
of the launched program (and maybe read from
I disagree with Nathan Fellman - the other question is not a duplicate of this, though the subject is related.
For simple unidirectional communication, popen() is a decent solution. It is no use for bi-directional communication, though.
IMO, imjorge (Jorge Ferreira) gave most of the answer (80%?) for bi-directional communication - but omitted a few key details.
If you do not close the unused ends of the pipes, you do not get sensible behaviour when one of the programs terminates; for example, the child might be reading from its standard input, but unless the write end of the pipe is closed in the child, it will never get EOF (zero bytes from read) because it still has the pipe open and the system thinks it might sometime get around to writing to that pipe, even though it is currently hung waiting for something to read from it.
The writing processes should consider whether to handle the SIGPIPE signal that is given when you write on a pipe where there is no reading process.
You have to be aware of pipe capacity (platform dependent, and might be as little as 4KB) and design the programs to avoid deadlock.