当前位置:网站首页>Homework 8.1 Orphans and Zombies
Homework 8.1 Orphans and Zombies
2022-08-01 21:35:00 【Unknown college student 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("The child process is ready to exit\n");
_exit(1);//子进程退出
}
i++;
sleep(1);
}
printf("子进程退出\n" );
}
else
{
perror( "fork " );
return -1;
}
return 0;
}
运行结果
The parent process ID of the child process is 1677,That is, the process number is1的init进程
A child process is an orphan process,被init进程收养
2.Create a zombie process
代码
#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("The child process is ready to exit\n");
_exit(1);//子进程退出
}
i++;
sleep(1);
}
printf("子进程退出\n" );
}
else
{
perror( "fork " );
return -1;
}
return 0;
}
运行结果
The child process is not recycled after exiting,状态为Z+
Z:僵尸状态;进程退出,But the parent process did not collect the corpse for the process
+:运行在前端
边栏推荐
- C陷阱与缺陷 第7章 可移植性缺陷 7.8 随机数的大小
- LVS负载均衡群集
- C语言_枚举类型介绍
- ORI-GB-NP半乳糖介导冬凌草甲素/姜黄素牛血清白蛋白纳米粒的研究制备方法
- shell规范与变量
- 树莓派的信息显示小屏幕,显示时间、IP地址、CPU信息、内存信息(c语言),四线的i2c通信,0.96寸oled屏幕
- 分类接口,淘宝分类详情 API
- 软考 ----- UML设计与分析(上)
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.5 ANSI C Today
- C陷阱与缺陷 第7章 可移植性缺陷 7.10 首先释放,然后重新分配
猜你喜欢
Based on php online examination management system acquisition (php graduation design)
对C语言结构体内存对齐的理解
空间数据库开源路,超图+openGauss风起禹贡
property语法
How to choose Visibility, Display, and Opacity when interacting or animating
包含吲哚菁绿的多聚体白蛋白纳米球/载马钱子碱纳米粒的牛血清白蛋白微球的制备
ModuleNotFoundError: No module named ‘yaml‘
PyQt5 + MySQL5.8 【学生信息管理系统】【增删改查】
WEB渗透之SQL 注入
Based on php Xiangxi tourism website management system acquisition (php graduation design)
随机推荐
C pitfalls and pitfalls Chapter 7. Portability pitfalls 7.10 Free first, then realloc
Appendix A printf, varargs and stdarg A.1 printf family of functions
Day016 类和对象
The difference between groupByKey and reduceBykey
NFT的10种实际用途(NFT系统开发)
基于php影视资讯网站管理系统获取(php毕业设计)
位运算简介
Based on php Xiangxi tourism website management system acquisition (php graduation design)
一个关于操作数据库的建议—用户密码
分类接口,淘宝分类详情 API
Pytest: begin to use
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.2 Early Experience of C Language
图片识别商品接口 API:天猫淘宝
关键字搜索:“淘宝商品 API ”
在Cesium中实现与CAD的DWG图叠加显示分析
测试开发人均年薪30w+?软件测试工程师如何进阶拿到高薪?
淘宝获取收货地址列表的 API
可视化——Superset使用
上传markdown文档到博客园
如何优雅的性能调优,分享一线大佬性能调优的心路历程