当前位置:网站首页>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
+:运行在前端


边栏推荐
猜你喜欢

方舟:生存进化PVE模式和PVP模式

FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes

【接口测试】JMeter调用JS文件实现RSA加密

MySQL相关知识

小程序--独立分包&分包预下载

基于php旅游网站管理系统获取(php毕业设计)

WEB渗透之SQL 注入

Unity Shader 常规光照模型代码整理

C语言_联合体共用体引入

Based on php online music website management system acquisition (php graduation design)
随机推荐
365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号
scikit-learn no moudule named six
基于php酒店在线预定管理系统获取(php毕业设计)
Spark shuffle调优
C Pitfalls and Defects Chapter 5 Library Functions 5.5 Library Function Signal
Based on php film and television information website management system acquisition (php graduation design)
Based on php animation peripheral mall management system (php graduation design)
Based on php tourism website management system acquisition (php graduation design)
0DFS中等 LeetCode6134. 找到离给定两个节点最近的节点
宝塔应用使用心得
Appendix A printf, varargs and stdarg A.1 printf family of functions
Shell编程条件语句
AI应用第一课:支付宝刷脸登录
C Pitfalls and Defects Chapter 7 Portability Defects 7.11 An Example of a Portability Problem
Scala练习题+答案
LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟
测试开发人均年薪30w+?软件测试工程师如何进阶拿到高薪?
微软校园大使喊你来秋招啦!
JS hoisting: how to break the chain of Promise calls
基于php湘西旅游网站管理系统获取(php毕业设计)