防止僵尸进程的fork编程
2016-07-10 01:29
267 查看
基本概念:
孤儿进程与僵尸进程
原理就是捕获SIGCHLD信号,通过waitpid函数处理子进程退出,直接上代码:
----- gcc fork_one.c -----
ps:还有一种办法就是双重fork,让子进程fork孙子进程,然后让子进程退出,孙子进程归init进程托管。
[b]----- gcc fork_two.c -----
[/b]
参考:《unix网络编程》·卷1、《unix环境高级编程》·第三版
孤儿进程与僵尸进程
原理就是捕获SIGCHLD信号,通过waitpid函数处理子进程退出,直接上代码:
----- gcc fork_one.c -----
#include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <signal.h> #include <errno.h> #include <stdio.h> void sig_chld(int signo) { pid_t pid; int stat; while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0) //while循环是表示处理所有待处理的信号,-1表示任意子进程,WNOHANG表示非阻塞 printf("child %d terminated\n", pid); return; } int main() { signal(SIGCHLD, sig_chld); pid_t cli = fork(); if (cli > 0) { printf("I am father\n"); } else { printf("I am child\n"); _exit(0); } while (1) { sleep(1); } }
ps:还有一种办法就是双重fork,让子进程fork孙子进程,然后让子进程退出,孙子进程归init进程托管。
[b]----- gcc fork_two.c -----
[/b]
#include <stdio.h> #include <unistd.h> #include <errno.h> #include <sys/types.h> #include <sys/wait.h> #include <stdlib.h> void Perror(const char *s) { perror(s); exit(EXIT_FAILURE); } int main() { pid_t pid; if ((pid = fork()) < 0) { Perror("fork error"); } else if (pid == 0) { /* first child */ if ((pid = fork()) < 0) { Perror("fork error"); } else if (pid > 0) { exit(0); /* parent from second fork == first child */ } sleep(2); printf("second child pid = %ld, parent pid = %ld\n", (long)getpid(), (long)getppid()); exit(0); } if (waitpid(pid, NULL, 0) != pid) /* wait for first child */ Perror("waitpid error"); printf("parent for first child exit\n"); exit(0); }
参考:《unix网络编程》·卷1、《unix环境高级编程》·第三版
相关文章推荐
- Linux socket 初步
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程
- Linux 下无损图片压缩小工具介绍