[英]Why does SIGINT stop sleep more than one time?
I don't understand why after the first execution of kill function the sleep function doesn't pause the parent process. 我不明白为什么在首次执行kill函数后,sleep函数不会暂停父进程。 After the SIGINT has been delivered, many processes are generated.
交付SIGINT后,将生成许多进程。 Furthermore, it seems that a variable number of processes is generated.
此外,似乎生成了数量可变的进程。 Is a handler for SIGINT required for cleaning the first SIGINT from the pending signals?
从挂起的信号中清除第一个SIGINT是否需要SIGINT的处理程序?
void handler(int s) {
int status;
wait(&status);
printf("\n in the handler");
if (WIFSIGNALED(status)) {
int sig=WTERMSIG(status);
printf("\n child stopped by signal %d\n",sig);
}
if (WIFEXITED(status)) {
int ex=WEXITSTATUS(status);
printf("\n child stopped with exist status %d\n",ex);
}
}
int main() {
int pid,len, count, ret;
char s[1024];
signal(SIGCHLD,handler);
while(1) {
pid=fork();
if (pid==0) {
printf("\n Write something ");
scanf("%s",s);
len=strlen(s);
printf("\n Characters: %d",len);
return 1;
}
else {
sleep(20);
kill(pid,SIGINT);
}
}
}
The SIGCHLD
from the previous child-death-causing kill
is arriving just when you're in the next sleep
. 该
SIGCHLD
从以前的儿童死亡造成kill
,当你在接下来的是刚刚抵达sleep
。
Sleep is an interruptible function. 睡眠是一种可中断的功能。 If a signal handler runs while your thread is in it, the
sleep
is going to abort. 如果信号处理程序在线程处于线程中的
sleep
运行,则sleep
将中止。 So then you proceed to the next kill
, which is going to indirectly cause another SIGCHLD
which will most likely happen when your next iteration's sleep, resulting in several skipped sleeps. 因此,您接着进行下一个
kill
,这将间接导致另一个SIGCHLD
,最有可能在您的下一个迭代的睡眠时发生,从而导致多个跳过的睡眠。
If you insert sleep(1)
before fork()
, it should (practically) shift the timing just right so that next sleep(20);
如果在
fork()
sleep(1)
之前插入sleep(1)
,它应该(实际上)将定时恰好移到下一个sleep(20);
is uninterrupted. 不间断。
(I'm treating this as a quick-and-dirty experiment. In no way is it a good idea to rely on "adjustments" like this in production code (or to use printf
in signal handlers for that matter).) (我将其视为一项快速而又肮脏的实验。在生产代码中依靠这样的“调整”(或为此目的在信号处理程序中使用
printf
绝不是一个好主意。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.