Linux提高:僵尸进程

题目

代码实现一个僵尸进程

代码

实现僵尸进程,只需让子进程先于父进程结束,并且父进程不调用 wait/ waitpid 函数回收子进程的退出状态。在父进程没有退出的转态下使用 ps 命令即可查看存在的僵尸进程信息。

下图运行结果,红框里为僵尸子进程
在这里插入图片描述

/*************************************************************************
    > File Name: main.c
    > Author: 杨永利
    > Mail: 1795018360@qq.com 
    > Created Time: 2021年07月14日 星期三 21时46分58秒
 ************************************************************************/


#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
	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;
}

知识回顾

详细内容请移驾另一篇博客:
https://yangyongli.blog.csdn.net/article/details/118738314

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呆萌宝儿姐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值