我必须编写一个C程序,它创建一个子程序并等待一个SIGHUP信号。收到后,它会向其孩子发送信号。子进程执行接收信号的默认动作。
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
void sigint();
void sighup();
pid_t pid, pid_parent;
int main(int argc, char const *argv[])
{
if ((pid = fork()) < 0) {
perror("fail1!");
exit(1);
}
else if (pid == 0) {
signal(SIGINT, sigint);
while(1) pause();
exit(1);
}
signal(SIGHUP, sighup);
printf("PARENT's pid: %d\n", pid_parent = getpid());
while(1) pause();
return 0;
}
void sighup()
{
signal(SIGHUP,sighup);
kill(pid,SIGINT);
printf("PARENT(%d): I received a SIGHUP. I'll send something to my child(%d).\n",getpid(),pid);
printf("OK");
}
void sigint()
{
signal(SIGINT,sigint);
printf("CHILD: I received a SIGINT. I'll execute it.");
signal(SIGINT,SIG_DFL);
}
似乎它在kill(pid, SIGQUIT)
之后无效。我真的没有想法为什么。