c 僵尸进程_演示僵尸进程的C程序

c 僵尸进程

僵尸进程 (Zombie process)

A process which has finished its execution but still has an entry in the process table to report to its parent process is known as a zombie process.

一个已经完成执行但仍在进程表中具有要报告给其父进程的条目的进程称为僵尸进程

In the following code, you can see that the parent will sleep for 20 sec, so it will complete its execution after 20 sec. But, Child will finish its execution using exit() system call while its parent process has gone for sleep.

在下面的代码中,您可以看到父级将睡眠20秒,因此它将在20秒后完成执行。 但是,Child将在其父进程进入睡眠状态时使用exit()系统调用完成其执行。

After execution the child must report to its parent, So the child process entry has to be in the process table to report to its parent even after it has finished execution.

执行后,子级必须向其父级报告。因此,即使子进程条目已完成执行,也必须位于进程表中才能向其父级报告。

Note: fork() is a UNIX system call so following program will work only on UNIX based operating systems.

注意: fork()是UNIX系统调用,因此以下程序仅适用于基于UNIX的操作系统。

The following code will not produce any output. It is just for demonstration purpose.

以下代码不会产生任何输出。 它仅用于演示目的。

用C语言编写僵尸程序 (Program for zombie process in C)

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

int main()
{
	// fork() creates child process identical to parent
	int pid = fork();

	// if pid is greater than 0 than it is parent process
	// if pid is 0 then it is child process
	// if pid is -ve , it means fork() failed to create child process

	// Parent process
	if (pid > 0)
		sleep(20);

	// Child process
	else
	{
		exit(0);
	}
	
	return 0;

}

Reference: Zombie and Orphan Processes in C

参考: C语言中的僵尸和孤立进程

翻译自: https://www.includehelp.com/c-programs/zombie-process.aspx

c 僵尸进程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值