当前位置:网站首页>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
+:运行在前端
边栏推荐
猜你喜欢
随机推荐
Spark集群搭建
FusionGAN:A generative adversarial network for infrared and visible image fusion文章学习笔记
The difference between groupByKey and reduceBykey
LeetCode·32.最长有效括号·栈·动态规划
牛血清白蛋白刺槐豆胶壳聚糖缓释纳米微球/多西紫杉醇的纳米微球DTX-DHA-BSA-NPs
Appendix A printf, varargs and stdarg A.1 printf family of functions
Based on php online examination management system acquisition (php graduation design)
C陷阱与缺陷 第7章 可移植性缺陷 7.7 除法运算时发生的截断
C Expert Programming Preface
ImportError: `save_weights` requires h5py.问题解决
虚拟内存与物理内存之间的关系
方舟:生存进化PVE模式和PVP模式
”sed“ shell脚本三剑客
宝塔应用使用心得
牛血清白蛋白-葡聚糖-叶黄素纳米颗粒/半乳糖白蛋白磁性阿霉素纳米粒的制备
C Pitfalls and Defects Chapter 7 Portability Defects 7.7 Truncation During Division
C Pitfalls and Defects Chapter 5 Library Functions 5.5 Library Function Signal
C陷阱与缺陷 第8章 建议与答案 8.2 答案
with语句和上下文管理器
Pytest: begin to use