父进程定时器回调函数中fork,子进程execl,子进程无法接收到SIGALRM信号
父进程代码
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int execTime = 0;
void signalFunc()
{
printf("test_time\n");
if (!execTime++)
{
int pid = fork();
if (!pid)
{
alarm(0);
execl("/home/fic091/code/test_child", "/home/fic091/code/test_child", NULL);
}
}
alarm(10);
}
int main()
{
printf("father pid: %d\n", getpid());
signal(SIGALRM, signalFunc);
alarm(2);
while(1)
{
pause();
}
return 0;
}
子进程代码
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int execTime = 0;
void signalFunc()
{
printf("test_child\n");
alarm(2);
}
int main()
{
printf("child pid: %d\n", getpid());
signal(SIGALRM, signalFunc);
alarm(10);
while(1)
{
// sleep(2);
// signalFunc();
pause();
}
return 0;
}
子进程一直pause,通过命令行kill发送SIGALRM信号,STRACE监控,子进程没有接收到信号
父进程在SIGALRM回调函数中屏蔽了SIGALRM信号,子进程fork时继承了屏蔽,所以需要取消对SIGALRM信号的阻塞