当前位置:网站首页>wait解决僵尸进程
wait解决僵尸进程
2022-07-02 22:06:00 【Htht111】
C0301—2022.0315
进程常规语句
在父进程里调用wait函数,查看效果。整个父进程做了三件事:两次打印、一次调用wait函数。
一、子进程做了死循环。
int pid = 0;
int status = 0;//状态
int res = 0;//wait返回值
pid = fork();
if (pid == 0)
{
cout << "进入子进程逻辑 pid=" << getpid() << endl;
for (int i = 0; i >-1; i++)
{
cout << "子进程 for i=" << i << endl;
sleep(2);//防止打印太快
}
}
else if (pid>0)
{
cout << "进入父进程逻辑 pid=" << getpid() << endl;
res = wait(&status);
cout << "wait后 父进程"<< endl;
}
因VS return打了断点结束不了,故用linux内核编译运行。
生成—重新生成解决方案
到Ubuntu里找到工程所在文件夹,右击 在终端打开
编译运行发现
(1)子进程一直在死循环
(2)父进程只做了第一步,且处于睡眠可唤醒状态。
wait等待子进程结束后再有返回值,此例子子进程是个死循环。
二、子进程循环可以结束,观察父进程调用wait函数后是什么情况
int pid = 0;
int status = 0;//状态
int res = 0;//wait返回值
pid = fork();
if (pid == 0)
{
cout << "进入子进程逻辑 pid=" << getpid() << endl;
for (int i = 0; i <10; i++)
{
cout << "子进程 for i=" << i << endl;
sleep(2);
}
}
else if (pid>0)
{
cout << "进入父进程逻辑 pid=" << getpid() << endl;
res = wait(&status);
cout << "wait后 父进程 res="<<res<< endl;
}
编译运行后,循环中
子进程循环结束后,可以看到,父进程打印了调用wait函数后的语句。说明子进程循环结束后,wait函数返回子进程的ID,然后再接着打印后面的语句。
由此可知,wait函数起阻塞的作用,子进程运行结束后,再返回子进程ID,接着下面的操作。
可以解决,子进程先于父进程结束,造成僵尸进程的问题。
边栏推荐
- 佩服,竟然有人把高等数学这么晦涩难懂的科目,讲解得如此通俗易懂
- Build your own website (22)
- [micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
- 服务器响应状态码
- 【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
- [staff] Sibelius 7.5.1 score software installation (software download | software installation)
- Task and privilege level protection
- U++ learning note pile
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
- U++ 原始内存 学习笔记
猜你喜欢
Oracle-游标
情感计算与理解研究发展概述
[shutter] shutter application theme (themedata | dynamic modification theme)
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
sql service 截取字符串
Sql service intercepts string
NC24325 [USACO 2012 Mar S]Flowerpot
UE4 游戏架构 学习笔记
Oracle-PL/SQL编程
Hanoi Tower problem
随机推荐
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
[autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
Infrastructure is code: a change is coming
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed
建立自己的网站(22)
[leetcode] sword finger offer 11 Rotate the minimum number of the array
Dynamic memory allocation (malloc calloc realloc free)
Oracle cursor
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
NC50965 Largest Rectangle in a Histogram
Market Research - current market situation and future development trend of aircraft front wheel steering system
Market Research - current market situation and future development trend of high tibial osteotomy plate
SimpleITK使用——4. 奇怪的问题
Official announcement! The golden decade of new programmers and developers was officially released
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
An overview of the development of affective computing and understanding research
NC24325 [USACO 2012 Mar S]Flowerpot
UE4 game architecture learning notes
Servicemesh mainly solves three pain points
Struct, bit segment, enumeration, union