子进程会继承父进程的信号处理方式,直到子进程调用exec函数。子进程调用exec函数后,exec将父进程中设置为捕捉的信号变为默认处理方式,其余不变。例如在父进程中把SIGTERM设置为捕捉,SIGINT设置为忽略。子进程执行exec 1#include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <sys/wait.h> 5 6 int main ( int argc , char **argv) 7 { 8 pid_t pid; if (SIG_ERR == signal (SIGINT , SIG_IGN)) 9 if ( (pid = fork()) < 0) 10 { 11 printf ("fork error/n"); 12 } 13 else if (pid == 0) 14 { 15 execv ("term_int", argv); 16 printf ("never get here /n"); 17 } 18 19 wait (NULL); 20 } 下面是term_int.c #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 5 int main() 6 { 7 pause(); 8 return 0; 9 } 这里怎么看子进程与父进程的区别 在第一个主程序后加 wait前 pause(); 可以看到 execv后的pause后是能ctrl+c结束的 而主程序中的不能因为有: if (SIG_ERR == signal (SIGINT , SIG_IGN))
子进程调用execv函数后,对信号的处理不保留
最新推荐文章于 2021-12-22 11:10:22 发布