linux多进/线程编程(3)——wait、waitpid函数和孤儿、僵尸进程
当使用fork创建多个进程后,需要解决子进程回收的问题。wait和waitpid函数就是做这个工作的。
假设子进程没有合理的回收,可能会带来两个问题:
1.孤儿进程(父进程挂了,子进程活着),孤儿进程会被init进程回收,可以理解其没有危害,不会占用资源。
2.僵尸进程(子进程挂了,父进程活着),僵尸进程是当父进程活着时,子进程没有其他进程帮忙回收后产生的“有害进程”,所以僵尸必须要回收,否则僵尸进程太多了会占用系统资源。此时就需要用到wait或waitpid函数。
wait和waitpid的区别在于:
wait函数是阻塞的,必须等到子进程被回收才会执行wait之后的代码;
waitpid可以设置为非阻塞的,不过非阻塞可能带来新的问题,子进程还没有回收waitpid这句代码就走完了(一般会写循环,通过判断返回值解决)。
wait:
WAIT(2) Linux Programmer's Manual WAIT(2) NAME
wait, waitpid, waitid - wait for process to change state SYNOPSIS
#include <sys/types.h>
#include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options);
返回值:
RETURN VALUE
wait(): on success, returns the process ID of the terminated child; on error, -1 is returned. waitpid(): on success, returns the process ID of the child whose state has changed; if WNOHANG was specified and one or more child(ren) specified by pid exist, but have not yet changed state,
then 0 is returned. On error, -1 is returned.
waitpid的参数:
The child state changes to wait for are specified by ORing one or more of the following flags in options:
WEXITED Wait for children that have terminated.
WSTOPPED Wait for children that have been stopped by delivery of a signal.
WCONTINUED Wait for (previously stopped) children that have been resumed by delivery of SIGCONT.
The following flags may additionally be ORed in options:
WNOHANG As for waitpid().
WNOWAIT Leave the child in a waitable state; a later wait call can be used to again retrieve the child status information.
wait用法示例:
pid_t pid = fork();
if (pid == 0) {
//while (true) {
//sleep(1);
//}
printf("i am child, i will die\n");
sleep(2);
//return 101;
exit(2);
} else if (pid > 0) {
printf("i am parent, wait for a child die\n");
int status { 0 };
pid_t wait_pid = wait(&status);//1,阻塞等待(子进程死亡);2,回收子进程资源(避免僵尸进程);3,查看状态(死亡原因)
printf("wait ok, wait_pid=%d, pid=%d\n", wait_pid, pid);
if (WIFEXITED(status)) {
printf("child exit with %d\n", WEXITSTATUS(status));
}
if (WIFSIGNALED(status)) {
printf("child killed by %d\n", WTERMSIG(status));
}
while (true) {
sleep(1);
}
}
waitpid用法示例:
pid_t pid = fork();
int ret { 0 }; if (pid == 0) {
printf("i am child, pid=%d\n", getpid());
sleep(2); exit(101);
} else if (pid > 0) {
printf("i am parent, self=%d, child_pid=%d\n", getpid(), pid); //不阻塞可能会带来一个问题,子进程还没回收,这句代码就走完了,所以要加循环(让代码阻塞在这里。。这。。。那还不如直接用wait????)
//WNOHANG: 不阻塞
int status { 0 }; while ((ret = waitpid(-1, &status, WNOHANG)) == 0) {
sleep(1);
}
printf("ret=%d\n", ret); if (WIFEXITED(status)) {
printf("child exit with %d\n", WEXITSTATUS(status));
} if (WIFSIGNALED(status)) {
printf("child killed by %d\n", WTERMSIG(status));
} ret = waitpid(-1, nullptr, WNOHANG); if (ret < 0) {
perror("wait err");
} while (true) {
sleep(1);
}
}
waitpid回收多个子进程(且忽略子进程状态即死亡原因)
int i { 0 };
int n { 5 };
int ret { 0 };
pid_t pid { 0 };
for (i = 0; i < n; ++i) {
pid = fork();
if (pid == 0) {
printf("i am child, pid=%d\n", getpid());
sleep(2);
break;
}
}
if (pid > 0) {
while ((ret = waitpid(-1, nullptr, WNOHANG)) != -1) {
sleep(1);
printf("ret = %d\n", ret);
}
printf("ret = %d\n", ret);
while (true) {
sleep(1);
printf("i am parent, self=%d, child_pid=%d\n", getpid(), pid);
}
}
linux多进/线程编程(3)——wait、waitpid函数和孤儿、僵尸进程的更多相关文章
- linux多进/线程编程(7)——多线程1(线程的创建,回收,分离,设置线程属性等)
参考资料: 1.博客1:https://blog.csdn.net/zhou1021jian/article/details/71531699 2.博客2:https://blog.csdn.net/ ...
- linux多进/线程编程(4)——进程间通信之pipe和fifo
前言: Linux环境下,进程地址空间相互独立,每个进程各自有不同的用户地址空间.任何一个进程的全局变量在另一个进程中都看不到,所以进程和进程之间不能相互访问,要交换数据必须通过内核,在内核中开辟一块 ...
- linux多进/线程编程(5)——进程间通信之mmap
参考资料: 1.博客1:https://www.jianshu.com/p/755338d11865 mmap:一种内存映射文件的方法 memory map 父子进程和无亲缘关系的进程,都可以将自身用 ...
- linux多进/线程编程(2)—— fork函数和进程间“共享”数据
参考: 1.博客1:https://www.pianshen.com/article/4305691855/ fork:在原进程的基础上"分叉"出一个子进程,即创建一个子进程. N ...
- linux多进/线程编程(1)—— 基础概念(PCB、MMU、进程状态)
学习大概就是不断迭代.重构的过程,不复习的学习是不负责任的,亦是无用的. 本系列博客主要作为个人记录,主要是贴图和代码,不做详细解释,以后有时间可能会重写:从下一篇开始上代码,代码可以运行是对自己的最 ...
- linux无锁化编程--__sync_fetch_and_add系列原子操作函数
linux支持的哪些操作是具有原子特性的?知道这些东西是理解和设计无锁化编程算法的基础. 下面的东西整理自网络.先感谢大家的分享! __sync_fetch_and_add系列的命令,发现这个系列命令 ...
- Linux 网络编程详解六(多进程服务器僵尸进程解决方案)
小结:在点对点p2p程序中,服务器端子程序退出,子进程会主动发送信号,关闭父进程,但是这种模式导致服务器只能支持一个客户端连接,本章节中使用新的框架,子进程退出,不主动发送信号关闭父进程,而是父进程安 ...
- UNIX环境编程学习笔记(21)——进程管理之获取进程终止状态的 wait 和 waitpid 函数
lienhua342014-10-12 当一个进程正常或者异常终止时,内核就向其父进程发送 SIGCHLD信号.父进程可以选择忽略该信号,或者提供一个该信号发生时即被调用的函数(信号处理程序).对于这 ...
- 【Linux】僵尸进程,孤儿进程以及wait函数,waitpid函数(有样例,分析很详细)
本文内容: 1.僵尸进程,孤儿进程的定义,区别,产生原因,处理方法 2.wait函数,waitpid函数的分析,以及比较 背景:由于子进程的结束和父进程的运行是一个异步的过程,即父进程永远无法预测子进 ...
随机推荐
- python字符串系列--2
#!/usr/bin/python #coding=utf-8 first_name='tiger' last_name='gao' full_name= f"{first_name} {l ...
- zabbix-mongodb监控脚本(高性能、低占用)
Zabbix调用脚本以实现对MongoDB的监控! 本脚本支持对服务存活状态.副本集.性能指标共计25个监控项! 使用mongostat和"echo rs.status()["me ...
- 【故障公告】数据库服务器 CPU 100% 引发全站故障
今天 11:12-12:03 期间,园子使用的阿里云 RDS 实例(SQL Server2016 标准版,16核CPU)出现 CPU 100% 问题,引发全站故障,由此给您带来麻烦,请您谅解. 发现故 ...
- MapperScan注解 放在启动器上?
package com.aaa.zxf; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boo ...
- X000001
一些相互无关联的题目的集合 都是码量不大,略有思维难度的题 做起来还是很舒适的 P6312 [PA2018]Palindrom 空间限制很小,不足以存下整个字符串,故暴力判断不可行. 考虑使用字符串哈 ...
- git merge -ff --no-ff --squash 的区别
感谢原文作者:futureme 原文链接:https://www.cnblogs.com/taylorluo/articles/10810762.html git merge #没有参数(默认为–ff ...
- iOS 性能优化系列
Objective-C 高性能的循环 使用 Swift 和 Objective-C 执行 iOS 内存管理的 7 个简单技巧 @autoreleasepool-内存的分配与释放
- Sping高质量博文链接集合
1. Spring事务传播行为详解 https://segmentfault.com/a/1190000013341344
- nvidia-smi
内容转自:https://blog.csdn.net/handsome_bear/article/details/80903477 nvidia-smi 显示 说明 Fan 风扇转速(0%--100% ...
- springCloud项目解决跨域问题
通过 spring cloud gateway 实现, 方式一:选择在主启动类中注册 CorsWebFilter 类: /** * 1.允许cookies跨域 * 2.允许向该服务器提交请求的URI, ...