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

2019-08-10 11:19发布

问题:

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.