当前位置:网站首页>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,接着下面的操作。
可以解决,子进程先于父进程结束,造成僵尸进程的问题。
边栏推荐
- Market Research - current situation and future development trend of cell-based seafood market
- Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
- U++ 学习笔记 ----松弛
- [shutter] shutter page life cycle (initialization period | createstate | initstate | update period | build | destroy period | dispose)
- Leetcode theme [array] -169- most elements
- NC24325 [USACO 2012 Mar S]Flowerpot
- 【leetcode】1380. Lucky number in matrix
- Try to get property'num for PHP database data reading_ rows' of non-object?
- Service visibility and observability
- Source code analysis - lightweight asynchronous crawler framework Ruia
猜你喜欢

Sql service intercepts string

Dynamic memory allocation (malloc calloc realloc free)

Developers share | HLS and skillfully use Axi_ Customize the master bus interface instructions and improve the data bandwidth - area exchange speed

What is it that makes you tremble? Those without fans can learn

Promise optimized callback hell

情感计算与理解研究发展概述

Evolution of messaging and streaming systems under the native tide of open source cloud

New feature of go1.18: introduce new netip Network Library

NC50965 Largest Rectangle in a Histogram

LightGBM原理及天文数据中的应用
随机推荐
Methods of adding styles to native JS
Pointer array parameter passing, pointer parameter passing
[leetcode] sword finger offer 11 Rotate the minimum number of the array
20220702 how do programmers build knowledge systems?
Bridge emqx cloud data to AWS IOT through the public network
ServiceMesh主要解决的三大痛点
Leetcode theme [array] -169- most elements
From personal heroes to versatile developers, the era of programmer 3.0 is coming
任务和特权级保护
[001] [arm-cortex-m3/4] internal register
Based on asp Net (used mobile phone sales management system) +asp Net+c # language +vs2010+ database can be used for course design and post design learning
Market Research - current situation and future development trend of anterior cruciate ligament (ACL) reconstruction Market
Ransack combined condition search implementation
SimpleITK使用——4. 奇怪的问题
Official announcement! The golden decade of new programmers and developers was officially released
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
数学建模——图与网络模型及方法(一)
数据库系统概论第一章简答题-期末考得怎么样?
Unity3d learning notes 4 - create mesh advanced interface
LxC terminal login method