当前位置:网站首页>作业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:僵尸状态;进程退出,但是父进程没有给该进程收尸
+:运行在前端
边栏推荐
猜你喜欢
随机推荐
LeetCode
Realize the superposition display analysis of DWG drawing with CAD in Cesium
磷酸化甘露糖苷修饰白蛋白纳米粒/卵白蛋白-葡聚糖纳米凝胶的
C Pitfalls and Defects Chapter 7 Portability Defects 7.11 An Example of a Portability Problem
如何优雅的性能调优,分享一线大佬性能调优的心路历程
ISC2022 HackingClub白帽峰会倒计时1天!最全议程正式公布!元宇宙集结,精彩绝伦!
C Pitfalls and Defects Chapter 7 Portability Defects 7.8 Size of Random Numbers
漏洞分析丨HEVD-0x6.UninitializedStackVariable[win7x86]
Review Set/Map basics with these two hooks
Pytorch框架学习记录8——最大池化的使用
R语言 线性回归的有关方法
附录A printf、varargs与stdarg A.1 printf函数族
LVS负载均衡群集
AIDL通信
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.2 Early Experience of C Language
sizeof的详细解说和与strlen的区别
JSD-2204-Knife4j框架-处理响应结果-Day07
Classification interface, Taobao classification details API
包含吲哚菁绿的多聚体白蛋白纳米球/载马钱子碱纳米粒的牛血清白蛋白微球的制备
网红驼背矫正产品真的管用吗?如何预防驼背?医生说要这样做