当前位置:网站首页>Process creation and recycling
Process creation and recycling
2022-06-12 11:29:00 【Investment paranoia】
Process creation and recycling
fork: Create child process
fork()Will return twice , Return... In child process 0, Returns the... Of the child process in the parent process pid, return -1 Indicates creation failed- Once created , Child processes and parent processes share data ( Only pcb Some of the data in are different , Such as pid,ppid). Share while reading , When writing copy .
Orphan process VS Zombie process
- Orphan process : End of parent process , Subprocess running --------> The parent process of the child process is set to
init(pid = 1) ---------------> At the end of the subprocessinitRecycling- Zombie process : End of subprocess , The parent process runs ---------> The parent process did not end and the child process was not recycled (wait)---------------> Zombie process
Subprocess recycle
- wait: Wait for any subprocess to finish , And recycle . Block waiting
- waitpid: Specify a child process to wait for completion and recycle , Blocking and non blocking can be set
/********************************************************* Copyright 2022 Shengkai Liu. All rights reserved. FileName: wait.c Author: Shengkai Liu Date: 2022-05-31 ***********************************************************/
#include <unistd.h> // fork, getpid
#include <stdio.h> // printf, perror
#include <wait.h> // wait
#include <stdlib.h> // exit
int main()
{
// create a sub-process
pid_t pid = fork();
// prints its own pid if the sub-process
if (pid == 0)
{
printf("pid of the child process: %d\n", getpid());
exit(0);
}
// Recycle sub-process resources by wait
int status;
pid = wait(&status);
if (pid == -1) perror("wait");
printf("pid of recycled sub-processes: %d\n", pid);
return 0;
}
/********************************************************* Copyright 2022 Shengkai Liu. All rights reserved. FileName: waitpid.c Author: Shengkai Liu Date: 2022-05-31 ***********************************************************/
#include <unistd.h> // fork, getpid
#include <stdio.h> // printf, perror
#include <wait.h> // waitpid
#include <stdlib.h> // exit
int main()
{
// create a sub-process
pid_t pid = fork();
if (pid == 0)
{
// prints its own pid if the sub-process
printf("pid of the child process: %d\n", getpid());
exit(0);
}
else if (pid > 0)
{
// Recycle sub-process resources by wait
int status;
pid = waitpid(pid, &status, 0);
if (pid == -1) perror("wait");
printf("pid of recycled sub-processes: %d\n", pid);
exit(0);
}
return -1;
}
边栏推荐
- go基于阿里云实现发送短信
- 如何查看glibc版本
- M-arch (fanwai 12) gd32l233 evaluation -cau encryption and decryption (tease Xiaobian)
- 元宇宙链游与传统游戏的区别
- LVS health state detection based on application layer
- Mcuxpresso develops NXP rt1060 (3) -- porting lvgl to NXP rt1060
- 【clickhouse专栏】基础数据类型说明
- Golang基础(7)
- selenium使用代理IP
- Redis keys in PHP
猜你喜欢

k52.第一章 基于kubeadm安装kubernetes v1.22 -- 集群部署

M-Arch(番外12)GD32L233评测-CAU加解密(捉弄下小编)

890. 查找和替换模式

Don't swallow rice with vinegar! Teach you 2 moves to make the fish bones "run out" safely

【藍橋杯單片機 國賽 第十一届】

Simple solution of regular expression

MATLAB中stairs函数使用

Network topology
![[the 11th national competition of Blue Bridge Cup single chip microcomputer]](/img/da/3c8a9efd5b28f67816f239531a0339.png)
[the 11th national competition of Blue Bridge Cup single chip microcomputer]

Mcuxpresso develops NXP rt1060 (3) -- porting lvgl to NXP rt1060
随机推荐
AcWing 1995. Meet and greet (simulation)
疫情居家办公体验 | 社区征文
M-arch (fanwai 13) gd32l233 evaluation - some music
mysql的悲观锁和乐观锁
MATLAB中stairs函数使用
k53.第二章 基于二进制包安装kubernetes v1.22 --集群部署
套接字实现 TCP 通信流程
Record the pits encountered when using JPA
AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
k58.第一章 基于kubeadm安装kubernetes v1.23 -- 集群部署
M-Arch(番外10)GD32L233评测-SPI驱动DS1302
Differences among various cross compiling tools of arm
人类想要拥有金钱、权力、美丽、永生、幸福……但海龟只想做一只海龟
Unity connect to Microsoft SQLSERVER database
M-arch (fanwai 12) gd32l233 evaluation -cau encryption and decryption (tease Xiaobian)
如何查看glibc版本
AcWing 1986. 镜子(模拟,环图)
InfoQ 极客传媒 15 周年庆征文|position:fixed 虚拟按键触发后无法生效问题分析及解决方案探究
多普勒效应的基本原理
UI自动化测试中比较少见的异常记录