I've looked through stackoverflow, and read the man pages about popen to try to understand its behavior. My understanding from this is that it creates a pipe, forks off a child process, and then executes the command in the child process. However, I'm trying to figure out - does the command that gets executed run to completion before the popen returns? I'm doing something like this:
fp = popen(cmd_str, "r");
if (!fp) {
ERR("Failed to execute cmd");
return;
}
Say the cmd_str takes a few minutes to finish. Will the popen return immediately? I tried googling for the popen.c source code, and the ones I found seem to indicate that the parent process returns the FILE ptr, while the child process goes off and does an exec on the cmd_str. Popen returns NULL if the pipe failed, the fork failed, some malloc failed, or maybe there were bad args.
It doesn't appear to me that the popen call is actually blocking to see if the cmd_str finished execution - the source code I've found doesn't have anything where the parent process waits for the child process to finish. Is this is how popen works? I'm just trying to make sure that my understanding of this is correct.