Chinaunix

标题: linux wait 等待进程状态改变,这里的状态包含哪些? [打印本页]

作者: socay2    时间: 2013-12-15 19:26
标题: linux wait 等待进程状态改变,这里的状态包含哪些?
DESCRIPTION
       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
.  In the case of a terminated child, performing a wait allows the system to release the resources associated with the
       child; if a wait is not performed, then the terminated child remains in a "zombie" state (see NOTES below).
这是源自man wait的解释,这里解释说进程的终止、停止、和唤醒都叫做状态的改变。可是通过kill 发送 SIGCONT 信号给子进程时,wait还是处于阻塞状态! 请问是不是wait是要等到子进程结束了才解除阻塞呢?
作者: 井蛙夏虫    时间: 2013-12-15 21:46
回复 1# socay2
文档继续向下看

   
作者: socay2    时间: 2013-12-15 22:52
回复 2# 井蛙夏虫
往下也没有解释呀!

   
作者: 井蛙夏虫    时间: 2013-12-16 01:11
回复 3# socay2
http://pubs.opengroup.org/online ... functions/wait.html

   
作者: socay2    时间: 2013-12-16 15:48
回复 4# 井蛙夏虫

谢谢!
这个解释和man手册描述的稍稍有一点不一样
   
作者: MMMIX    时间: 2013-12-16 16:03
socay2 发表于 2013-12-15 19:26
可是通过kill 发送 SIGCONT 信号给子进程时,wait还是处于阻塞状态!


在这之前子进程是处于 stop 状态么?发送完 SIGCONT 之后,子进程恢复运行了么?
作者: 井蛙夏虫    时间: 2013-12-16 20:38
本帖最后由 井蛙夏虫 于 2013-12-16 20:42 编辑

回复 5# socay2
这个是posix标准文档。
我的系统上的man文档是有说的,不知道为什么你系统的没有。
  1. The wait() system call suspends execution of the calling process until one of its children terminates.  The call wait(&status) is equivalent to:

  2.            waitpid(-1, &status, 0);
复制代码
wait只能是子进程终止。如果要子进程继续和暂停都可以,要用waitpid
  1. The  waitpid()  system  call  suspends  execution  of the calling process until a child specified by pid argument has changed state.  By default, waitpid() waits only for terminated children, but this
  2.        behavior is modifiable via the options argument, as described below.
复制代码
  1. The value of options is an OR of zero or more of the following constants:

  2.        WNOHANG     return immediately if no child has exited.

  3.        WUNTRACED   also return if a child has stopped (but not traced via ptrace(2)).  Status for traced children which have stopped is provided even if this option is not specified.

  4.        WCONTINUED (since Linux 2.6.10)
  5.                    also return if a stopped child has been resumed by delivery of SIGCONT.
复制代码

作者: socay2    时间: 2013-12-16 22:47
MMMIX 发表于 2013-12-16 16:03
在这之前子进程是处于 stop 状态么?发送完 SIGCONT 之后,子进程恢复运行了么?


在子进程中通过调用pause(),是否达到stop效果了呢?
作者: socay2    时间: 2013-12-16 22:59
回复 7# 井蛙夏虫
我的系统是ubuntu 12.04,在man手册中的描述就如标题所述。
你的系统解释确实是进程 terminated , 不是进程状态 changed
可能是glibc库版本不一样吧

   
作者: MMMIX    时间: 2013-12-17 09:51
socay2 发表于 2013-12-16 22:47
在子进程中通过调用pause(),是否达到stop效果了呢?

不行。pause() 会让调用它的进程 sleep,而不是 stop。查看进程的状态可以使用 ps(1)。
作者: socay2    时间: 2013-12-17 23:39
回复 10# MMMIX

哦! 那要stop一个进程只能通过发送SIGSTOP信号吗?  有没有某个API可以直接完成?
   
作者: MMMIX    时间: 2013-12-18 09:13
socay2 发表于 2013-12-17 23:39
回复 10# MMMIX

哦! 那要stop一个进程只能通过发送SIGSTOP信号吗?  有没有某个API可以直接完成?


直接调用 kill(3) 发送 SIGSTOP 信号就是了,还想要啥样的API?
作者: socay2    时间: 2013-12-18 17:20
回复 12# MMMIX

kill SIGSTOP信号能够将进程变为T+状态,kill SIGCONT信号也可以将进程状态从T+重新变为S+。
可是这样状态的改变,并不能让wait解除阻塞!

难道wait真只有等到子进程结束时才解除阻塞? 如@夏虫说的。
   
作者: MMMIX    时间: 2013-12-18 19:56
socay2 发表于 2013-12-18 17:20
回复 12# MMMIX
难道wait真只有等到子进程结束时才解除阻塞?

是的,如果你需要捕获其他状态改变,可以使用 waitpid(3)。
作者: socay2    时间: 2013-12-19 18:19
回复 14# MMMIX

嗯,对的!  感谢@MMMIX
   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2