当前位置:网站首页>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,接着下面的操作。
可以解决,子进程先于父进程结束,造成僵尸进程的问题。
边栏推荐
- U++ 原始内存 学习笔记
- #include errors detected. Please update your includePath.
- SimpleITK使用——3. 常见操作
- Utilisation de simpletk - 4. Question étrange
- 建立自己的网站(22)
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
- kubernetes资源对象介绍及常用命令(四)
- Market Research - current market situation and future development trend of total nutrition products
- NC50965 Largest Rectangle in a Histogram
- 附加:【登录信息存储】与【登录状态校验】;(包括:总结了到目前为止,有关【登录信息存储】与【登录状态校验】的所有内容;)
猜你喜欢
Oracle-PL/SQL编程
Basic concepts of image and deep understanding of yuv/rgb
#include errors detected. Please update your includePath.
Kubernetes resource object introduction and common commands (4)
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
[shutter] shutter resource file use (import resource pictures | use image resources)
Reading experience of just because
20220702 how do programmers build knowledge systems?
Pointer and string
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
随机推荐
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
[leetcode] sword finger offer 11 Rotate the minimum number of the array
Regular expression (2)
Micro service gateway selection, please accept my knees!
Market Research - current situation and future development trend of herringbone gear Market
New feature of go1.18: introduce new netip Network Library
JS solution for obtaining the width and height of hidden elements whose display is none
[QT] QT multithreading development - four methods to realize multithreading design
Lightgbm principle and its application in astronomical data
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
数据库系统概论第一章简答题-期末考得怎么样?
[C question set] of V
Market Research - current market situation and future development trend of third-party data platform
Market Research - current market situation and future development trend of aircraft audio control panel system
U++ learning notes - relaxation
Evolution of messaging and streaming systems under the native tide of open source cloud
对象与对象变量
Promise optimized callback hell
《乔布斯传》英文原著重点词汇笔记(十一)【 chapter nine】
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?