Do I need to wait() in the parent process after a fork?

梦想与她 提交于 2019-12-24 16:31:51

问题


I'm wondering if I have to wait() for all child process to finish in the parent program?

I have read the manuals and some online resources about fork(), but none of them mentioned that a wait() in the parent is enforced.

However, if I do not wait in the parent program, the process does not terminate but just does nothing until I press enter, and than terminates.


回答1:


If the parent process doesn't use a system call of the wait()-family for its children processes, it could simply die first.

When a parent process dies, all of its children processes are moved under process init with PID == 1 in the process tree. So the process init is their new parent process.


I guess the reason you observe this behaviour with <Enter> (just a guess, it could be wrong) is because the parent process is terminated earlier than its children, so your terminal's prompt is printed before some of the children's output. When you press <Enter>, the prompt is printed again. Check again the output carefully.



来源:https://stackoverflow.com/questions/23714398/do-i-need-to-wait-in-the-parent-process-after-a-fork

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