Process 当提出“退出”时发送退出代码;SIGUSR1“;信号
我试图在引发“SIGUSR1”时发送子进程的退出代码 我使用了一个等待进程的处理程序,但它没有正确接收退出代码 我想它收到了-1 这是我在main的代码Process 当提出“退出”时发送退出代码;SIGUSR1“;信号,process,operating-system,signals,Process,Operating System,Signals,我试图在引发“SIGUSR1”时发送子进程的退出代码 我使用了一个等待进程的处理程序,但它没有正确接收退出代码 我想它收到了-1 这是我在main的代码 void main() { signal(SIGUSR1, handler); int pid = fork(); if (pid == -1) perror("error in forking"); else if (pid == 0) { print
void main()
{
signal(SIGUSR1, handler);
int pid = fork();
if (pid == -1)
perror("error in forking");
else if (pid == 0)
{
printf("\ni am child and my pid %d\n", getpid());
raise(SIGUSR1);
exit(100);
}
sleep(5);
printf("\nnow i am the parent and my = %d\n ", getpid());
}
这是处理程序代码
void handler(int signunm)
{
int pid, stat_loc;
pid = wait(&stat_loc);
if (!(stat_loc & 0x00FF))
printf("\nI am called with the process %d with value %d\n", pid, stat_loc >> 8);
}
输出类似于
i am child and my pid 45892
I am called with the process -1 with value 4
now i am the parent and my = 45891
它应该是100
而不是-1
您应该使用kill(PPID,SIGUSR1)
函数而不是raise(SIGUSR1)
函数,这样您就可以向家长发送一个信号来处理它,而不是向孩子本身发送信号。您可以通过调用getppid()