C语言 wait() 在 Unix 上做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13216554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:18:04  来源:igfitidea点击:

What does wait() do on Unix?

cunixubuntuwait

提问by Naruto

I was reading about the wait()function in a Unix systems book. The book contains a program which has wait(NULL)in it. I don't understand what that means. In other program there was

我正在阅读wait()一本 Unix 系统书中的函数。这本书包含一个程序wait(NULL)。我不明白这是什么意思。在其他程序中有

while(wait(NULL)>0) 

...which also made me scratch my head.

……这也让我挠头。

Can anybody explain what the function above is doing?

谁能解释一下上面的函数在做什么?

回答by iabdalkader

man wait(2)

男人等待(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 specificchild process to change its state, if a pidis not specified, then it's equivalent to calling wait()and it waits for anychild process to change its state.

所以wait()允许一个进程等待直到它的一个子进程改变它的状态,例如存在。如果waitpid()使用进程 id 调用,则等待该特定子进程更改其状态,如果pid未指定 a,则相当于调用wait()并等待任何子进程更改其状态。

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

wait()函数在成功时返回子 pid,因此当它在这样的循环中被调用时:

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)

这意味着等待所有子进程退出(或更改状态)并且不再等待子进程(或直到发生错误)

回答by Aniket Inge

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

一个快速的谷歌建议,wait(NULL)等待任何子进程完成

回答by Rahul Tripathi

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

wait(NULL)这应该相当于 waitpid(-1, NULL, 0)

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

wait(NULL)等待所有子进程完成