I have a C++ pthread Thread invoking shell scripts using Popen.
What is observed in one case is that a pthread_join failure is caught (i.e pthread_join != 0) and the child process remains as zombie ( as per ps output).
This also causes the main program to hang.
Right now, I don't know why the pthread_join would fail, because it never did in other scenarios.
But I would like to know whether collecting the wait status of the child process spawned by Popen is part of thread_join. If so atleast I can be sure that the join failure is the root cause for the zombie and the program hang..
Thanks in advance..
After calling
popen
, there needs to be a corresponding call topclose
, otherwise the child process spawned by thepopen
call will remain in the zombie state. What might have happened is that the thread encountered an unhandled exception that causedpclose
to not be called, resulting in a zombie and a "failed" result ofpthread_join
.One solution is to make sure your code properly handles all possible exceptions.
Another solution would be to implement something similar to
popen()
but does not requirepclose()
to reap the zombie. This can be accomplished by using a double fork. You callfork()
, then call it again before callingexec()
. The parent process allows the intermediate child process to exit and be reaped withwait4()
. The grandchild process can now exit without leaving a zombie since it will be reaped by theinit
process. To create a communication channel between the grandchild and parent, usepipe()
aspopen()
does, orsocketpair()
if you need bidirectional communication. Usedup2()
in the grandchild process to allow your choices ofstdin
,stdout
, andstderr
to be redirected through the pipe or socket before callingexec()
.