当前位置:网站首页>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,接着下面的操作。
可以解决,子进程先于父进程结束,造成僵尸进程的问题。
边栏推荐
- [shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
- 《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
- UE4 游戏架构 学习笔记
- Market Research - current market situation and future development trend of aircraft front wheel steering system
- Oracle-PL/SQL编程
- [staff] Sibelius 7.5.1 score software installation (software download | software installation)
- Source code analysis - lightweight asynchronous crawler framework Ruia
- LightGBM原理及天文数据中的应用
- C语言,实现三子棋小游戏
- Attack and defense world PWN question: Echo
猜你喜欢

sql service 截取字符串
![[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)](/img/2b/f31b81cedf37ca187bcaa20dfe0b83.png)
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
![[shutter] shutter application theme (themedata | dynamic modification theme)](/img/77/6b0082368943aee7108ac550141f28.gif)
[shutter] shutter application theme (themedata | dynamic modification theme)

附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)

540. Single element in ordered array

20220702 how do programmers build knowledge systems?

【外刊】睡眠与减肥

Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?

The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"

SimpleITK使用——4. 奇怪的问题
随机推荐
服务器响应状态码
Using emqx cloud to realize one machine one secret verification of IOT devices
服务可见可观测性
附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
New feature of go1.18: introduce new netip Network Library
关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
[micro service sentinel] rewrite Sentinel's interface blockexceptionhandler
Market Research - current situation and future development trend of preclinical medical device testing service market
情感计算与理解研究发展概述
U++ 学习笔记 堆
Sql service intercepts string
100 important knowledge points that SQL must master: using cursors
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
使用 EMQX Cloud 实现物联网设备一机一密验证
佩服,竟然有人把高等数学这么晦涩难懂的科目,讲解得如此通俗易懂
SimpleITK使用——4. 奇怪的問題
【leetcode】1380. Lucky number in matrix
U++ 学习笔记 ----松弛
Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed