wait() 在 Unix 上做什么? [英] What does wait() do on Unix?

查看:20
本文介绍了wait() 在 Unix 上做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

while(wait(NULL)>0)

...这也让我摸不着头脑.

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

解决方案

man wait(2)<块引用>

所有这些系统调用都用于等待状态变化调用进程的子进程,并获取有关子进程的信息谁的状态发生了变化.状态改变被认为是:子进程终止;孩子被一个信号拦住了;或者孩子被信号恢复了

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

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

while(wait(NULL)>0)

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

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

while(wait(NULL)>0) 

...which also made me scratch my head.

Can anybody explain what the function above is doing?

解决方案

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)

这篇关于wait() 在 Unix 上做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆