当前位置:网站首页>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;
}
边栏推荐
- Starrocks community structure comes out, waiting for you to upgrade!
- How to customize logging of.Net6.0
- Lvs+kept highly available cluster
- ApacheSpark-命令执行(CVE-2022-33891) 漏洞复现
- Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track
- It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years
- JVM garbage collection garbage collector and common combination parameters
- High frequency relay
- 华能福建公司与华为数字能源深化战略合作,共建低碳智能县域
- Are Transformers Effective for Time Series Forecasting?|填坑
猜你喜欢

Is log4j vulnerability still widespread?

An2021软件安装及基本操作(新建文件/导出)

一种比读写锁更快的锁,还不赶紧认识一下

Learn the use principle and core idea of thread pool from the source code

The gratitude and resentment between the four swordsmen and code review: "abandon all chaos" to "prodigal son returns"

Pytoch distributed training

LVS+Keepalived高可用群集

Log4j 漏洞仍普遍存在,并持续造成影响

电磁继电器

@Can component be used on the same class as @bean?
随机推荐
Small change project (two versions) with detailed ideas
Log4j vulnerability is still widespread and continues to cause impact
Deepfake 换脸真假难辨,马斯克分克已伪装成功
Yyds dry goods inventory # solve the real problem of famous enterprises: cycle number comparison
Deepfake's face is hard to distinguish between true and false, and musk Fenke has disguised successfully
8000 word explanation of OBSA principle and application practice
【StoneDB故障诊断】MDL锁等待
Simple use of enum
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
2021-11-05 understand main method syntax, code block and final keyword
Leetcode-155-minimum stack
Enumeration and annotation
Vs2019 release mode debugging: this expression has side effects and will not be evaluated.
Live broadcast software app development, uniapp scroll view hidden scroll bar
Finish learning redis cluster solution at one go
Encapsulate an array into a class
Project analysis (from technology to project and product)
matlab 绘制三坐标(轴)图
CMOS开关(二)_参数提取
leetcode383赎金信