waitpid 是否会阻塞已停止的作业?
我有一个已收到 SIGTSTP 信号的子进程。 当我调用
waitpid(-1,NULL,0);
父块时,但在文档中,它写道 waitpid 返回已停止作业的 pid。
#include<unistd.h>
#include<stdio.h>
#include<signal.h>
#include<sys/wait.h>
main() {
int pid;
if( (pid=fork()) > 0) {
sleep(5);
if(kill(pid,SIGTSTP) < 0)
printf("kill error\n");
int status;
waitpid(-1,&status,0);
printf("Returned %d\n",WIFSTOPPED(status));
}
else if(pid==0) {
while(1);
}
}
I have a child process which has received a SIGTSTP signal.
When I call
waitpid(-1,NULL,0);
the parent blocks, but in documentation, its written that waitpid returns with pid for stopped jobs.
#include<unistd.h>
#include<stdio.h>
#include<signal.h>
#include<sys/wait.h>
main() {
int pid;
if( (pid=fork()) > 0) {
sleep(5);
if(kill(pid,SIGTSTP) < 0)
printf("kill error\n");
int status;
waitpid(-1,&status,0);
printf("Returned %d\n",WIFSTOPPED(status));
}
else if(pid==0) {
while(1);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您错过了
waitpid
的选项WUNTRACED
(第三个参数)。否则它不会返回,直到作业终止。设置 WUNTRACED 选项后,由于
SIGTTIN
、SIGTTOU
、SIGTSTP
导致当前进程的子进程停止> 或SIGSTOP
信号也有其状态报告(来自 mac 手册页)。You missed the option
WUNTRACED
forwaitpid
(3rd argument). Otherwise it doesn't return until the job is terminated.When the
WUNTRACED
option is set, children of the current process that are stopped due to aSIGTTIN
,SIGTTOU
,SIGTSTP
, orSIGSTOP
signal also have their status reported (from the mac man page).