Use popen
if you want to run a shell command and want the parent process to be able to talk to the child. (It hooks the child's input or output up to the stream you get back.) Otherwise, prefer the exec
family of functions (likely in conjunction with fork
); exec
'd processes inherit most open file descriptors (including stdin, stdout, and stderr), so you can hook input and output up to whatever you like...plus, there are fewer security implications.
system
is generally best avoided unless you have to run shell commands. It spawns a shell to run the command, and the shell can parse the command any way it likes. In particular, certain environment variables (like $IFS
and/or $PATH
) can be modified in such a way as to cause the parent to execute programs you never intended it to. Although popen
appears to do the same thing, it at least provides functionality that makes it worthwhile in the general case.