孤儿进程与僵尸进程

父进程与子进程

进程是操作系统中资源调度的最小单位。在Linux中,为我们提供了fork()函数来进行进程的创建,在Linux的官方手册上解释了fork()函数创建进程的方式,即子进程被创建是通过对父进程的复制得来的。子进程对父进程的复制实际上复制了父进程的整个地址空间,需要注意的是,虽然子进程是复制了父进程所使用的的地址空间。但子进程创建成功后。子进程所访问的虚拟空间一定是属于自己的,而非与父进程共享此空间(这里,注意与线程区分开)。其本质是父子进程分别映射到不同的物理空间地址。
那么,如何区分父进程与子进程呢?
其实,是通过fork()函数的返回值来区分的,fork()函数如果执行失败,会返回-1,而如果执行成功,则在子进程中获得一个0的返回值,在父进程中获得子进程的进程ID(一定大于0 的整数),使用这个进程ID可以区分当前的进程,从而分别控制父进程与子进程应该执行的代码段,如下:

// this program will show you how to use get process id

#include <iostream>
using namespace std;

#include <sys/types.h>
#include <unistd.h>

int main(int argc, const char** argv)
{
	pid_t pid;
	
	pid = fork();

	if(pid < 0 )
	{
		cout << "fork error" << endl;
		return -1;
	}
	else if(pid == 0)
	{
		cout << "The child process" << endl;
		decltype(getpid()) pid = getpid();
		decltype(getppid()) ppid = getppid();
		cout << "now process id is " << (long)pid << endl;
		cout << "it's father process id is " << (long)ppid << endl;
	}
	else
	{
		sleep(1);
		cout << "The father process" << endl;
		decltype(getpid()) pid = getpid();
		cout << "it's process id is " << (long)pid <<endl;
	}
	return 0;
}

执行结果为:
在这里插入图片描述

由此可以看出,在子进程的执行代码区中打印的父进程ID为10520,而子进程的进程ID为10521,在子进程中使用getppid()函数获得的父进程ID也与在父进程的执行代码区中使用getpid()获得本身进程ID是一样的。

孤儿进程

什么是孤儿进程呢?从字面上来看,就是父进程被杀死了,但是子进程还未被杀死,这种进程,也是我们编写守护进程的基础,在代码层面,我们可以考虑这样来产生孤儿进程

#include <iostream>
using namespace std;

#include <sys/types.h>
#include <unistd.h>

int main(int argc, const char** argv)
{
	pid_t pid;
	
	pid = fork();

	if(pid < 0 )
	{
		cout << "fork error" << endl;
		return -1;
	}
	else if(pid == 0)
	{
		cout << "The child process" << endl;
		decltype(getpid()) pid = getpid();
		decltype(getppid()) ppid = getppid();
		cout << "now process id is " << (long)pid << endl;
		cout << "it's father process id is " << (long)ppid << endl;
		while(1){}
	}
	else
	{
		sleep(1);
		cout << "The father process" << endl;
		decltype(getpid()) pid = getpid();
		cout << "it's process id is " << (long)pid <<endl;
	}
	return 0;
}

僵尸进程

僵尸进程其实是进程的一种状态,即僵尸态。僵尸态与死亡态很相似,唯一不同的是死亡进程会释放所有资源,而僵尸进程,即进程退出但却不释放资源,所以在平时的编程中,我们应该避免僵尸态进程的产生,因为这种状态的进程不执行任务但却占有系统资源,会导致系统资源的浪费。如下:

#include <iostream>
using namespace std;

#include <sys/types.h>
#include <unistd.h>

int main(int argc, const char** argv)
{
	pid_t pid;
	
	pid = fork();

	if(pid < 0 )
	{
		cout << "fork error" << endl;
		return -1;
	}
	else if(pid == 0)
	{
		cout << "The child process" << endl;
		decltype(getpid()) pid = getpid();
		decltype(getppid()) ppid = getppid();
		cout << "now process id is " << (long)pid << endl;
		cout << "it's father process id is " << (long)ppid << endl;
		
	}
	else
	{
		cout << "The father process" << endl;
		decltype(getpid()) pid = getpid();
		cout << "it's process id is " << (long)pid <<endl;
		while(1){}
	}
	return 0;
}

如上述代码所示,当父进程不退出而子进程却已经退出的时候,此时父进程不会回收子进程的资源,这个时候的子进程就会成为僵尸进程。
在终端输入ps axj即可查看父子进程的信息。
在这里插入图片描述
此时子进程的状态为Z,即僵尸态。
这就警示我们为了避免这样的情况产生,程序需要在子进程选择退出的时候,由父进程进行资源的回收处理。
我们不可能为了回收子进程资源而将父进程也退出,这将导致父进程不能拥有自由的生命周期,于是乎,Linux中也为我们提供了wait()函数以及waitpid()函数来完成对僵尸进程的资源的回收。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值