当前位置:网站首页>fork()函数的执行过程、孤儿进程和僵尸进程
fork()函数的执行过程、孤儿进程和僵尸进程
2022-07-27 19:25:00 【我不是萧海哇~~~~】
说起fork就不得不提COW(Copy On Write),就是“写时拷贝”。也就是当fork发生时,子进程根本不会去拷贝父进程的内存页面,而是与父进程共享。当子进程或父进程需要修改一个内存页面时,Linux就将这个内存页面复制一份给修改者,然后再去修改,这样从用户的角度看,父子进程根本就没有共享什么内存。COW也就是进程要写共享的内存页面,先复制再改写。
采用了COW技术后,fork时,子进程还需要拷贝父进程的页面表。这种拷贝的代价就非常小了,对于CPU来说用不了几个时钟周期。
1.孤儿进程
孤儿进程:就是没有父进程的进程。一个父进程退出,而它的一个或多个子进程还在运行,那么那些子进程将成为孤儿进程。孤儿进程将被init进程(进程号为1)所收养,并由init进程对它们完成状态收集工作。
2.僵尸进程
僵尸进程:一个子进程在其父进程还没有调用wait()或waitpid()的情况下退出。这个子进程就是僵尸进程。
3.危害
僵尸进程会占用系统资源,如果很多,则会严重影响服务器的性能
孤儿进程不会占用系统资源,最终是由init进程托管,由init进程来释放它。
处理流程:
只要老爹不等wait(sys/wait.h)儿子,儿子都将成为孤魂野鬼zombie(zombie),unix中默认老爹总是想看儿子死后的状态(以便报仇)
if 老爹比儿子先再见
儿子将被init(id = 1)收养,最后的结果是zombie儿子彻底再见,系统资源释放
else
{
儿子的zombie将一直存在,系统资源占用...
if 老爹dead
儿子将被init(id = 1)收养,最后的结果是zombie儿子彻底再见,系统资源释放
else 类似的儿子zombie越来越多,系统就等死了!!!
}
signal(SIGCHLD, SIG_IGN); //忽略SIGCHLD信号,这常用于并发服务器的性能的一个技巧
//因为并发服务器常常fork很多子进程,子进程终结之后需要
//服务器进程去wait清理资源。如果将此信号的处理方式设为
//忽略,可让内核把僵尸子进程转交给init进程去处理,省去了
//大量僵尸进程占用系统资源。(Linux Only)
4.如何防止僵尸进程
首先明白如何产生僵尸进程:
1、子进程结束后向父进程发出SIGCHLD信号,父进程默认忽略了它
2、父进程没有调用wait()或waitpid()函数来等待子进程的结束
第一种方法: 捕捉SIGCHLD信号,并在信号处理函数里面调用wait函数
转贴Richard Steven的Unix Network Programming代码
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;
}
边栏推荐
- Interview question: what are the functions of fail safe mechanism and fail fast mechanism
- Nano semiconductor 65W gallium nitride (GAN) scheme was adopted by Xiaomi 10 Pro charger
- 数组扩容、排序、嵌套语句应用
- 8000字讲透OBSA原理与应用实践
- 二维数组的基本用法
- 温度继电器
- It seems to be a bug of thread pool, but I think the source code design is unreasonable.
- [Marine Science] climate indices data set
- [question 22] dungeons and Warriors (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
- V2.X 同步异常,无法云端同步的帖子一大堆,同步又卡又慢
猜你喜欢

软件测试的就业前景到底怎么样?

8000字讲透OBSA原理与应用实践
![[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)](/img/dd/5ef46cff3988db57bfaf6fe925a0e0.jpg)
[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)

温度继电器

It seems to be a bug of thread pool, but I think the source code design is unreasonable.

MySQL execution process and order

舌簧继电器

每条你收藏的资讯背后,都离不开TA

对象在内存中存在形式&内存分配机制

固体继电器
随机推荐
Under the epidemic, the mobile phone supply chain and offline channels are blocked! Sales plummeted and inventory was serious!
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
Deepfake's face is hard to distinguish between true and false, and musk Fenke has disguised successfully
How can anyone ask how MySQL archives data?
Mimx8md6cvahzab i.MX 8mdual cortex-a53 - Microprocessor
ThreadLocal principle and source code analysis (click in step by step, don't recite, learn ideas)
为什么服务端程序都需要先 listen 一下
[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
vs2019 release模式调试:此表达式有副作用,将不予计算。
Massive data TOPK problem
基于简化的评分卡、Smote采样和随机森林的信贷违约预测
matlab 绘制三坐标(轴)图
Deploy dolphin scheduler high availability cluster based on rainbow
Leetcode 148. sorting linked list
高频继电器
Open source data quality solution -- Apache Griffin primer
JVM garbage collection garbage collector and common combination parameters
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
8000 word explanation of OBSA principle and application practice
An2021 software installation and basic operation (new file / export)