参考书籍:unxi环境高级编程
信号函数:
<span style="font-family:Microsoft YaHei;font-size:18px;">typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); </span>
其原型为:
<span style="font-family:Microsoft YaHei;font-size:18px;">void (*signal(int signum,void (*handler)(int))) (int);</span>
#include <stdio.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> void child_exit_handler(int signum) { if(signum == SIGCHLD) { printf("Child exit.\n"); wait(NULL); } } int main() { int pid; int i = 0; //想内核注册,处理 SIGCHLD信号的方式 signal(SIGCHLD,child_exit_handler); if((pid = fork()) < 0) { perror("Fail to fork"); exit(EXIT_FAILURE); }else if(pid == 0){ for(i = 0;i < 5;i ++) { printf("child loop.\n"); sleep(1); } }else{ for(i = 0;i < 5;i ++) { printf("Father loop.\n"); sleep(2); } } exit(EXIT_SUCCESS); }
原文地址:http://blog.csdn.net/chenxun_2010/article/details/46554759