当前位置:网站首页>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;
}
边栏推荐
- Leetcode-155-minimum stack
- 2021-11-05 understanding of class variables and class methods
- Enumeration and annotation
- [Marine Science] climate indices data set
- Monitor the running of server jar and restart script
- 时间继电器
- [numerical analysis exercise] Jacobi iteration method of third-order matrix
- How to deal with high concurrency deadlock?
- 九天后我们一起,聚焦音视频、探秘技术新发展
- Is log4j vulnerability still widespread?
猜你喜欢

Are Transformers Effective for Time Series Forecasting?|填坑

CMOS传输门原理及应用

Seven lines of code crashed station B for three hours

Interview question: what are the functions of fail safe mechanism and fail fast mechanism

High frequency relay

Polarization relay

Behind every piece of information you collect, you can't live without TA
![[Marine Science] climate indices data set](/img/0f/f63b946b80fc2cf1d39a975c318abc.png)
[Marine Science] climate indices data set

Reed relay

Reentranlock and source code analysis (learn ideas and click the source code step by step)
随机推荐
How to deal with high concurrency deadlock?
舌簧继电器
项目分析(从技术到项目、产品)
Deepfake's face is hard to distinguish between true and false, and musk Fenke has disguised successfully
[Marine Science] climate indices data set
[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
Import word document pictures blocking and non blocking IO operations
Excel only wants to visualize charts and make data move? Yes, come and watch (with a large number of templates to download)
JVM memory model interview summary
cocos:ccpDistance函数的简单运用以及实现眼球随着手指在眼眶中转动的功能
华能福建公司与华为数字能源深化战略合作,共建低碳智能县域
Inertial navigation principle (VII) -imu error classification (II) -allan variance analysis method +imu test + calibration introduction
【StoneDB故障诊断】系统资源瓶颈诊断
Matlab draws the statistical rose chart of wind speed and direction
刚培训完的中级测试工程师如何快速度过试用期
matlab 绘制三坐标(轴)图
Interview question: talk about your understanding of AQS
2021-11-05 understanding of class variables and class methods
【二叉树】统计二叉树中好节点的数目
Matplotlib multi subgraph drawing