我需要发送一个信号给子进程3次。如何向儿童多次发送信号避免僵尸状态? C语言
问题是,孩子只接收信号一次,然后转化为僵尸。
预期输出是:
我孩子11385和我收到SIGUSR1
我孩子11385和我收到SIGUSR1
我孩子11385和我收到SIGUSR1
但真正的输出是:
我孩子11385和我收到SIGUSR1
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
void my_handler()
{
printf("\n I'm the child %i and i received SIGUSR1\n", getpid());
}
int main (int argc, char **argv) {
int *array;
int N = 10;
int i;
pid_t pid1;
array=(int*)malloc(sizeof(int)*N);
signal(SIGUSR1,my_handler);
for (i = 0; i< N; i++)
{
pid1 = fork();
if(pid1 < 0)
{
exit(EXIT_FAILURE);
}
else if (pid1 > 0)
{
array[i]= pid1;
}
else
{
sleep(100);
exit(EXIT_SUCCESS);
}
}
i=0;
while(i<3) // I need to call the son 3 times
{
kill(array[1], SIGUSR1);
i++;
}
}
'无效my_handler()' - >>'无效my_handler(INT正负号)'[使用错误的签名,从信号处理函数返回有效地杀死你的孩子]并且:您不应该在信号处理程序中调用printf()。 – wildplasser
@wildplasser该代码是特定信号的示例,因此它没有参数。 printf也是一个例子,并不是真正的问题:/ –
但是它被**称为带有参数的参数,它会中断堆栈(返回) – wildplasser