【发布时间】:2021-03-14 20:37:03
【问题描述】:
我正在尝试在引发“SIGUSR1”时发送子进程的退出代码 我写了一个等待进程的处理程序,但它没有正确接收退出代码 相反,它接收 -1
这是我在 main 的代码
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
【问题讨论】:
标签: process operating-system signals