当前位置:网站首页>作业8.1 孤儿进程与僵尸进程
作业8.1 孤儿进程与僵尸进程
2022-08-01 21:17:00 【不知名大学生M】
1.创建一个孤儿进程
代码
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
pid_t pid = fork( );
if(pid > 0)
{
printf( "this is parent: %d child :%d\n" , getpid(),pid);
}
else if(0==pid)
{
int i = 0;
while(1)
{
//子进程
printf( "this is child ppid: %d child :%d\n", getppid(),getpid());
if(i > 3)
{
printf("子进程准备退出\n");
_exit(1);//子进程退出
}
i++;
sleep(1);
}
printf("子进程退出\n" );
}
else
{
perror( "fork " );
return -1;
}
return 0;
}
运行结果
子进程的的父进程号为1677,即进程号为1的init进程
子进程是孤儿进程,被init进程收养
2.创建一个僵尸进程
代码
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
pid_t pid = fork( );
if(pid > 0)
{
while(1)
{
printf( "this is parent: %d child :%d\n" , getpid(),pid);
sleep(1);
}
}
else if(0==pid)
{
int i = 0;
while(1)
{
//子进程
printf( "this is child ppid: %d child :%d\n", getppid(),getpid());
if(i > 3)
{
printf("子进程准备退出\n");
_exit(1);//子进程退出
}
i++;
sleep(1);
}
printf("子进程退出\n" );
}
else
{
perror( "fork " );
return -1;
}
return 0;
}
运行结果
子进程退出后未被回收,状态为Z+
Z:僵尸状态;进程退出,但是父进程没有给该进程收尸
+:运行在前端
边栏推荐
猜你喜欢
随机推荐
Hiking, cured my mental internal friction
ahooks 是怎么处理 DOM 的?
空间数据库开源路,超图+openGauss风起禹贡
C陷阱与缺陷 第8章 建议与答案 8.2 答案
C陷阱与缺陷 第7章 可移植性缺陷 7.11 可移植性问题的一个例子
【微信小程序】【AR】threejs-miniprogram 安装(76/100)
property语法
C pitfalls and pitfalls Chapter 7. Portability pitfalls 7.10 Free first, then realloc
TP5-NPs负载噻吩类化合物TP5白蛋白纳米粒/阿魏酸钠新糖牛血清蛋白纳米粒
2022-08-01 第五小组 顾祥全 学习笔记 day25-枚举与泛型
和我一起写一个音乐播放器,听一首最伟大的作品
Nacos 配置中心
Internet使用的网络协议是什么
R语言进行相关的操作
方舟开服需要知道的那些事
JVM内存结构详解
How to make the timer not execute when the page is minimized?
Pytorch框架学习记录9——非线性激活
wps excel 插入公式 整列
15 分钟带你入门 Grafana