进程学习回顾---两次fork避免僵尸进程
来源:互联网 发布:怎么评价张学良 知乎 编辑:程序博客网 时间:2024/06/05 20:52
本文档的Copyleft归rosetta所有,使用GPL发布,可以自由拷贝、转载,转载时请保持文档的完整性。
参考资料:APUE2e
来源:http://blog.csdn.net/rosetta
一个进程如果终止后未被其父进程回收,它将仍然占用资源,这种进程称为僵尸进程(zombie),它将对系统产生危害。
可以通过ps -aux查看系统中是否存在僵尸进程(其状态显示为Z)。
为避免产生僵尸进程,可以通过两次fork()操作,第一次fork产生第一个子进程,第二次fork由第一个子进程产生第二个子进程,目前的状态变成:爷爷->父亲->孙子。爷爷回收父亲后退出,此时孙子的父进程自动托管给init进程。而init进程无论如何会回收子进程的,所以这样就不会产生僵尸进程了。
一、两个系统调用
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
创建一个新进程
父进程返回值是子进程的pid
子进程返回值为0
出错返回-1
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
回收子进程
如果pid==-1,与wait等效。
如果pid>0,等待回收指定pid的进程。
status指针指向的内存单元存放进程返回的状态,如果不关心其状态可以至NULL,返回状态可以由对应宏判断(APUE2eP180的宏)。
options选项如果是WNOHANG,将不阻塞。
#include "apue.h"
#include <sys/wait.h>
int
main(void)
{
pid_t pid;
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* first child */ //产生的第一个子进程
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid > 0)//如果进程id为第一个子进程则退出
exit(0); /* parent from second fork == first child */
//否则进入孙子进程代码片段执行
/*
* We're the second child; our parent becomes init as soon
* as our real parent calls exit() in the statement above.
* Here's where we'd continue executing, knowing that when
* we're done, init will reap our status.
*/
sleep(2);//孙子进程睡眠2秒以便其父进程被爷爷进程回收。
printf("second child, parent pid = %d\n", getppid());
exit(0);
}
if (waitpid(pid, NULL, 0) != pid) /* wait for first child *///实参pid是指定的第一个子进程的pid,如果执行成功其返回值也为其回收的子进程pid.
err_sys("waitpid error");
/*
* We're the parent (the original process); we continue executing,
* knowing that we're not the parent of the second child.
*/
exit(0);
}
参考资料:APUE2e
来源:http://blog.csdn.net/rosetta
一个进程如果终止后未被其父进程回收,它将仍然占用资源,这种进程称为僵尸进程(zombie),它将对系统产生危害。
可以通过ps -aux查看系统中是否存在僵尸进程(其状态显示为Z)。
为避免产生僵尸进程,可以通过两次fork()操作,第一次fork产生第一个子进程,第二次fork由第一个子进程产生第二个子进程,目前的状态变成:爷爷->父亲->孙子。爷爷回收父亲后退出,此时孙子的父进程自动托管给init进程。而init进程无论如何会回收子进程的,所以这样就不会产生僵尸进程了。
一、两个系统调用
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
创建一个新进程
父进程返回值是子进程的pid
子进程返回值为0
出错返回-1
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
回收子进程
如果pid==-1,与wait等效。
如果pid>0,等待回收指定pid的进程。
status指针指向的内存单元存放进程返回的状态,如果不关心其状态可以至NULL,返回状态可以由对应宏判断(APUE2eP180的宏)。
options选项如果是WNOHANG,将不阻塞。
二、源码实例
#include "apue.h"
#include <sys/wait.h>
int
main(void)
{
pid_t pid;
if ((pid = fork()) < 0) {
err_sys("fork error");
} else if (pid == 0) { /* first child */ //产生的第一个子进程
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid > 0)//如果进程id为第一个子进程则退出
exit(0); /* parent from second fork == first child */
//否则进入孙子进程代码片段执行
/*
* We're the second child; our parent becomes init as soon
* as our real parent calls exit() in the statement above.
* Here's where we'd continue executing, knowing that when
* we're done, init will reap our status.
*/
sleep(2);//孙子进程睡眠2秒以便其父进程被爷爷进程回收。
printf("second child, parent pid = %d\n", getppid());
exit(0);
}
if (waitpid(pid, NULL, 0) != pid) /* wait for first child *///实参pid是指定的第一个子进程的pid,如果执行成功其返回值也为其回收的子进程pid.
err_sys("waitpid error");
/*
* We're the parent (the original process); we continue executing,
* knowing that we're not the parent of the second child.
*/
exit(0);
}
- 进程学习回顾---两次fork避免僵尸进程
- fork两次如何避免僵尸进程
- fork两次如何避免僵尸进程
- fork两次如何避免僵尸进程收藏
- fork两次如何避免僵尸进程收藏
- fork两次如何避免僵尸进程
- fork两次如何避免僵尸进程收藏
- fork两次如何避免僵尸进程
- fork两次如何避免僵尸进程
- fork两次如何避免僵尸进程收藏
- 调用 fork 两次避免僵尸进程
- fork两次如何避免僵尸进程
- 【apue学习心得】两次fork避免僵尸进程
- Unix通过fork两次避免僵尸进程
- fork两次如何避免僵尸进程收藏
- 通过两次fork避免产生僵尸进程
- 为何要fork()两次来避免产生僵尸进程?
- 为何要fork()两次来避免产生僵尸进程?
- 傅里叶变换
- TabHost两种使用方法
- Android自动化测试初探(五): 再述模拟键盘鼠标事件(adb shell 实现)
- SPI抗干扰能力
- Android自动化测试初探(四): 模拟键盘鼠标事件(Socket+Instrumentation实现)
- 进程学习回顾---两次fork避免僵尸进程
- putty窗口解决中文乱码问题
- Tomcat域名虚拟主机配置
- ASP.NET Treeview控件中对Checkbox的联级选择
- BitVisor中外部中断的处理流程浅析
- 如何调查Windows Embedded CE上的Exception“凶手”(1)
- SQL STUFF函数 同一列值拼接 拼接字符串
- Jaxb2.0实现Java Object转换Xml转换Java Object.
- 命名空间