Does pthread_join failure cause child process to be zombies?

↘锁芯ラ 提交于 2019-12-04 15:17:02

After calling popen, there needs to be a corresponding call to pclose, otherwise the child process spawned by the popen call will remain in the zombie state. What might have happened is that the thread encountered an unhandled exception that caused pclose to not be called, resulting in a zombie and a "failed" result of pthread_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 require pclose() to reap the zombie. This can be accomplished by using a double fork. You call fork(), then call it again before calling exec(). The parent process allows the intermediate child process to exit and be reaped with wait4(). The grandchild process can now exit without leaving a zombie since it will be reaped by the init process. To create a communication channel between the grandchild and parent, use pipe() as popen() does, or socketpair() if you need bidirectional communication. Use dup2() in the grandchild process to allow your choices of stdin, stdout, and stderr to be redirected through the pipe or socket before calling exec().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!