What does wait() do on Unix?

浪子不回头ぞ 提交于 2019-12-03 03:23:33

man wait(2)

All of these system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed. A state change is considered to be: the child terminated; the child was stopped by a signal; or the child was resumed by a signal

So wait() allows a process to wait until one of its child processes change its state, exists for example. If waitpid() is called with a process id it waits for that specific child process to change its state, if a pid is not specified, then it's equivalent to calling wait() and it waits for any child process to change its state.

The wait() function returns child pid on success, so when it's is called in a loop like this:

while(wait(NULL)>0) 

It means wait until all child processes exit (or change state) and no more child processes are unwaited-for (or until an error occurs)

a quick google suggests, wait(NULL) waits for any of the child processes to complete

wait(NULL) which should be equivalent to waitpid(-1, NULL, 0)

wait(NULL) waits for all the child processes to complete

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