我无法理解以下程序的输出 . 我发现在子进程返回后,父进程在wait()之前没有休眠3秒 . 如果SIGCHLD设置为默认处理程序,则它会休眠3秒,调用wait并按预期返回 . 到底发生了什么?
# include <unistd.h>
# include <sys/types.h>
# include <stdio.h>
# include <sys/wait.h>
# include <signal.h>
void handler(int sig) {
printf("Iam in handler ...\n");
}
main() {
int status;
pid_t pid;
struct sigaction act;
//act.sa_flags=SA_NOCLDSTOP;
act.sa_handler=handler;
sigaction(SIGCHLD,&act,NULL);
if(!fork()) {
printf("child process id is %d\n",getpid());
return 1;
}
printf("xxx ...\n");
sleep(3);
pid = wait(&status);
printf("process terminated is %d\n",pid);
}
output::
xxx ...
child process id is 2445
Iam in handler ...
process terminated is 2445
2 回答
来自man for sleep():
您的孩子终止会导致信号唤醒您 .
sleep()
的返回值:如果你想帮助你“完成”睡眠,可以使用 .
当子进程死亡时,
SIGCHLD
将被发送给父进程 . 在你的情况下,它会中断sleep
,看起来好像进程没有睡眠 .问题的要点:
sleep
在被信号中断时不会重新启动 .