c语言怎么检测触发信号,关于c语言捕获信号的问题。

this is my code

#include

#include

#include

#include

#include

#include

#include

void handler1(int sig) {

int pid;

if ((pid = waitpid(-1, NULL, 0)) < 0)

printf("waitpid error");

printf("Handler reaped child %d\n", pid);

sleep(1);

}

int main() {

int i, n;

if (signal(SIGCHLD, handler1) == SIG_ERR) {

printf("signal error\n");

}

for (i = 0; i < 3; ++i) {

if (fork() == 0) {

printf("Hello from child %d\n", getpid());

exit(0);

}

}

// sleep(1);

printf("Father love you!\n");

while (1);

exit(0);

}

When I run it, it shows this :

Father love you!

Hello from child 7843

Hello from child 7844

Hello from child 7842

Handler reaped child 7842

But I think it should be

Father love you!

Hello from child 7843

Hello from child 7844

Hello from child 7842

Handler reaped child 7842

Handler reaped child 7843

There a repetition of Handler reaped child xx.

If I uncomment sleep(1);, it will show what i want :

Hello from child 7858

Handler reaped child 7858

Hello from child 7859

Hello from child 7860

Handler reaped child 7859

Father love you!

I dont know why the first one has only one Handler reaped child. Please help me, thank you in advence.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值