孤儿进程&僵尸进程
来源:互联网 发布:钢铁雄心4卡顿优化补丁 编辑:程序博客网 时间:2024/05/17 08:49
孤儿进程&僵尸进程
1. 孤儿进程
定义:
一个父进程退出,而其一个或多个子进程还在运行,则这些子2进程称为孤儿进程。这些孤儿进程被init进程(1号)所收养,并由init进程对它们完成状态收集工作。
实例:
代码如下:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> int main() { pid_t pid; //创建一个进程 pid = fork(); //创建失败 if (pid < 0) { perror("fork error:"); exit(1); } //子进程 if (pid == 0) { printf("I am the child process.\n"); //输出进程ID和父进程ID printf("pid: %d\tppid:%d\n",getpid(),getppid()); printf("I will sleep five seconds.\n"); //睡眠5s,保证父进程先退出 sleep(5); printf("pid: %d\tppid:%d\n",getpid(),getppid()); printf("child process is exited.\n"); } //父进程 else { printf("I am father process.\n"); //父进程睡眠1s,保证子进程输出进程id sleep(1); printf("father process is exited.\n"); } return 0; }运行结果:
2.僵尸进程
定义:
一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中,这种进程称之为僵尸进程。
实例1:
代码如下:
#include <stdio.h> #include <unistd.h> #include <errno.h> #include <stdlib.h> int main() { pid_t pid; pid = fork(); if (pid < 0) { perror("fork error:"); exit(1); } else if (pid == 0) { printf("I am child process.I am exiting.\n"); exit(0); } printf("I am father process.I will sleep two seconds\n"); //等待子进程先退出 sleep(2); //输出进程信息 system("ps -o pid,ppid,state,tty,command"); printf("father process is exiting.\n"); return 0; }运行结果:
实例2:
父进程循环创建子进程,子进程退出,造成多个僵尸进程。
代码如下:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h> int main() { pid_t pid; //循环创建子进程 while(1) { pid = fork(); if (pid < 0) { perror("fork error:"); exit(1); } else if (pid == 0) { printf("I am a child process.\nI am exiting.\n"); //子进程退出,成为僵尸进程 exit(0); } else { //父进程休眠20s继续创建子进程 sleep(20); continue; } } return 0; }
运行结果:
1 0
- 孤儿进程、僵尸进程
- 僵尸进程 孤儿进程
- 僵尸进程&孤儿进程
- 僵尸进程,孤儿进程
- 僵尸进程 孤儿进程
- 僵尸进程&孤儿进程
- 孤儿进程&僵尸进程
- 僵尸进程&孤儿进程
- 孤儿进程/僵尸进程
- 孤儿和僵尸进程
- 僵尸、孤儿、守护进程
- 孤儿进程和僵尸进程
- 僵尸进程和孤儿进程
- 孤儿进程和僵尸进程
- 孤儿进程和僵尸进程
- 僵尸进程与孤儿进程
- 僵尸进程和孤儿进程
- 孤儿进程和僵尸进程
- android_关于weex加载手机sdk js文件
- 第三方登录原理探究
- Linux调试C++
- MySQL之一道关于GROUP BY的经典面试题
- Linux trap介绍
- 孤儿进程&僵尸进程
- React-Native简单的漫画的客户端
- css那些事儿——读书笔记
- Jmeter安装和启动报错unable to access jarfile apachejmeter.jar【errorlevel=1】的解决
- HDU5974 A Simple Math Problem (韦达定理)
- 24
- Spring MVC 4.x 知识点
- 异常的深入研究与分析
- Problem H: 稀疏矩阵的表示和运算 SDUSTOJ