当前位置:网站首页>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.11 可移植性问题的一个例子
- 回收租凭系统100%开源无加密 商城+回收+租赁
- 数据库练习
- 小程序--独立分包&分包预下载
- Transformer学习
- How to make the timer not execute when the page is minimized?
- Based on php tourism website management system acquisition (php graduation design)
- 磷酸化甘露糖苷修饰白蛋白纳米粒/卵白蛋白-葡聚糖纳米凝胶的
- LeetCode·32.最长有效括号·栈·动态规划
- 用户量大,Redis没法缓存响应,数据库宕机?如何排查解决?
猜你喜欢

JVM内存结构详解

Based on php online learning platform management system acquisition (php graduation design)

磷酸化甘露糖苷修饰白蛋白纳米粒/卵白蛋白-葡聚糖纳米凝胶的

51.【结构体初始化的两种方法】

property语法

线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~

Based on php online music website management system acquisition (php graduation design)

ImportError: `save_weights` requires h5py.问题解决

Port protocol for WEB penetration

MySQL相关知识
随机推荐
PyQt5 + MySQL5.8 【学生信息管理系统】【增删改查】
NFT的10种实际用途(NFT系统开发)
Spark practice questions + answers
(七)《数电》——CMOS与TTL门电路
方舟开服需要知道的那些事
Review Set/Map basics with these two hooks
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.2 Early Experience of C Language
WEB 渗透之端口协议
C陷阱与缺陷 第7章 可移植性缺陷 7.9 大小写转换
关于npm的那些事儿
PX4模块设计之十五:PX4 Log设计
测试开发人均年薪30w+?软件测试工程师如何进阶拿到高薪?
Shell编程条件语句
Upload markdown documents to blog garden
回收租凭系统100%开源无加密 商城+回收+租赁
Spark练习题+答案
property语法
LVS负载均衡群集
树莓派的信息显示小屏幕,显示时间、IP地址、CPU信息、内存信息(c语言),四线的i2c通信,0.96寸oled屏幕
2022牛客多校联赛第五场 题解