2011年4月7日

popen hangs!?

The suspicion is waitpid()?
After comment it out, the code works well and doesn't hang on popen().

Ref: http://pubs.opengroup.org/onlinepubs/009695399/functions/pclose.html
The issue may be related to pclose().
The return value of pclose() is always "-1", and the next popen() call hangs then.
From functional description, it says:

The pclose() function shall close a stream that was opened by popen(), wait for the command to terminate, and return the termination status of the process that was running the command language interpreter. However, if a call caused the termination status to be unavailable to pclose(), then pclose() shall return -1 with errno set to [ECHILD] to report this situation. This can happen if the application calls one of the following functions:

  • wait()
  • waitpid() with a pid argument less than or equal to 0 or equal to the process ID of the command line interpreter
  • Any other function not defined in this volume of IEEE Std 1003.1-2001 that could do one of the above.

In any case, pclose() shall not return before the child process created by popen() has terminated.

And in my case, the signal handler routine is using waitpid(-1, &stat, WNOHANG). After change it to waitpid(forked_pid, &stat, WNOHANG) to ignore SIGCHLD triggered by popen(), the application doesn't hang anymore.

0 意見: