当前位置:网站首页>The execution process, orphan process and zombie process of fork() function
The execution process, orphan process and zombie process of fork() function
2022-07-27 22:20:00 【I'm not Xiao Haiwa~~~~】
Speaking of fork I have to say COW(Copy On Write), Namely “ Copy while writing ”. That is to say fork occurs , The child process does not copy the memory page of the parent process at all , It's shared with the parent process . When a child or parent process needs to modify a memory page ,Linux Just copy this memory page to the modifier , Then I'll revise it , From the perspective of users , The parent-child processes do not share any memory at all .COW That is, the process needs to write a shared memory page , Copy first and then rewrite .
Adopted COW After the technology ,fork when , The child process also needs to copy the page table of the parent process . The cost of this kind of copy is very small , about CPU It doesn't take a few clock cycles .
1. Orphan process
Orphan process : A process without a parent process . A parent process exits , And one or more of its subprocesses is still running , Then those sub processes will become orphan processes . The orphan process will be init process ( The process number is 1) Adopted , And by the init Process completes status collection for them .
2. Zombie process
Zombie process : A child process has not been called by its parent process wait() or waitpid() Exit in case of . This sub process is the zombie process .
3. harm
Zombie processes will occupy system resources , If many , It will seriously affect the performance of the server
Orphan processes do not consume system resources , Ultimately it is init Process hosting , from init Process to release it .
Processing flow :
As long as dad doesn't wait wait(sys/wait.h) son , Sons will become ghosts zombie(zombie),unix By default, the father always wants to see his son's state after death ( For revenge )
if See you before your son
The son will be init(id = 1) Adoption , The end result is zombie Goodbye, son , System resource release
else
{
Son's zombie There will always be , System resource usage ...
if Daddy dead
The son will be init(id = 1) Adoption , The end result is zombie Goodbye, son , System resource release
else A similar son zombie More and more , The system will die !!!
}
signal(SIGCHLD, SIG_IGN); // Ignore SIGCHLD The signal , This is often used as a technique for the performance of concurrent servers
// Because concurrent servers often fork A lot of subprocesses , After the subprocess ends, you need
// The server process goes wait Cleaning up resources . If the processing method of this signal is set to
// Ignore , You can let the kernel transfer the zombie child process to init Process to deal with , No
// A large number of zombie processes occupy system resources .(Linux Only)
4. How to prevent zombie processes
First, understand how to generate zombie processes :
1、 When the child process ends, it sends a SIGCHLD The signal , The parent process ignores it by default
2、 Parent process did not call wait() or waitpid() Function to wait for the end of the child process
The first method : capture SIGCHLD The signal , And call in the signal processing function wait function
Repost Richard Steven Of Unix Network Programming Code
int
main(int argc, char **argv)
{
...
Signal(SIGCHLD, sig_chld);
for(;
}
...
}
void
sig_chld(int signo)
{
pid_t pid;
int stat;
while ( (pid = waitpid(-1, &stat, WNOHANG)) >; 0)
printf("child %d terminated/n", pid);
return;
}
边栏推荐
- 8000 word explanation of OBSA principle and application practice
- Leetcode-39-total number of combinations
- 8000字讲透OBSA原理与应用实践
- 【StoneDB故障诊断】MDL锁等待
- MySQL execution process and order
- Solid state relay
- 8000字讲透OBSA原理与应用实践
- [question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
- 固体继电器
- Mimx8md6cvahzab i.MX 8mdual cortex-a53 - Microprocessor
猜你喜欢

7行代码让B站崩溃3小时

Memo mode - unity

Simple use of enum

刚培训完的中级测试工程师如何快速度过试用期

Project analysis (from technology to project and product)

2022 2nd cyber edge cup cyber security competition Web

华能福建公司与华为数字能源深化战略合作,共建低碳智能县域

If demand splitting is as simple as cutting a cake | agile practice

Reed relay

Finish learning redis cluster solution at one go
随机推荐
Credit default prediction based on simplified scorecard, smote sampling and random forest
舌簧继电器
If demand splitting is as simple as cutting a cake | agile practice
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
An2021 software installation and basic operation (new file / export)
MySQL execution process and order
Simple use of enum
day 1 - day 4
基于简化的评分卡、Smote采样和随机森林的信贷违约预测
直播app系统源码,上下叠层样式的轮播图
刚培训完的中级测试工程师如何快速度过试用期
Interview question: talk about your understanding of AQS
Finish learning redis cluster solution at one go
The gratitude and resentment between the four swordsmen and code review: "abandon all chaos" to "prodigal son returns"
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
Matplotlib multi subgraph drawing
vs2019 release模式调试:此表达式有副作用,将不予计算。
软件测试的就业前景到底怎么样?
关系型数据库的设计思想,20张图给你看的明明白白
Can JVM tuning be done with single core CPU and 1G memory?