你的子进程没有调用 signal 来设置它自己对应 SIGALM的处理函数,看看我改后的代码,子进程调用了signal来设置信号处理函数。
子进程给父进程发信号后,父进程打印3秒后,会向子进程也发同一信号,子进程也能处理这个信号了。
#include
#include
#include
#include
static int alarm_fired = 0;
void ding(int sig)
{
alarm_fired = 1;
}
int main()
{
pid_t pid;
printf("alarm application starting\n");
pid = fork();
switch(pid) {
case -1:
/* Failure */
perror("fork failed");
exit(1);
case 0:
/* child */
sleep(5);
(void) signal(SIGALRM, ding);
printf("Child send sig to parent\n");
kill(getppid(), SIGALRM);
pause();
if (alarm_fired) printf("Ding!\n");
printf("Child exit\n");
exit(0);
}
/* if we get here we are the parent process */
printf("waiting for alarm to go off\n");
(void) signal(SIGALRM, ding);
pause();
if (alarm_fired) printf("Ding!\n");
sleep(3);
printf("Parent send sig to child\n");
kill(pid, SIGALRM);
wait(NULL);
exit(0);
}
取消
评论