在Linux中简单实现回收子进程
学习到wait函数了,这个函数的作用是用来回收进程。一般来说,正常退出的进程是不需要我们来专门回收的。但是进程有这两种:孤儿进程和僵尸进程。
孤儿进程:
通俗点说就是父进程先于子进程死亡。此时子进程就成为孤儿进程。这时候子进程的父进程就是init进程了。这个过程(父进程死亡后子进程的父进程变为init)称为init进程领养孤儿进程。
僵尸进程
进程终止,但是父进程并未进行回收操作。子进程的残留资源(PCB)存在于内核中,成为僵尸进程。就是儿子死了,但是父亲不收尸。
这里要注重理解的是:僵尸进程实际上是结束的进程。所以,我们能用kill命令结束它么?不能!结束一个已经结束的进程。怎么能行呢?
请看下列代码:
#include
<cstdio>
#include
<stdlib.h>
#include
<unistd.h>
int p = 111;
//pid_t fork(void);//在父进程中返回子进程的id,在子进程返回0(没有子进程了嘛,因为)。
int main(int
argc, char*argv[])//有用户指定创建多少个子进程
{
printf("hello from Create_N_subprocesses!\n");
if (argc < 2)
{
printf("Too few parameters\n");
exit(1);
}
int i, n = atoi(argv[1]);
for (i = 0; i < n; i++)
{
if (0 == fork())//父进程中返回的不是0,继续执行for循环;在子进程中返回0,for循环结束。
{
break;
}
}
//基本上每个父子进程是同时进行。所以睡眠时间依次递增。
sleep(i);
if (i < n)//父进程不能算在里面
{
p++;//这里改变p的值
printf("I am the %d process.\n p = %d\n", i + 1, p);//这里的每个p都一样,哪怕每次p都会自增
}
else
{
printf("my father process \n p = %d\n", p);//但是这里还是会输出111
}
/*
p用来验证父子之间可以用静态变量传递消息么?
答案是不可以,因为date段是独立的。
父子之间遵循读时共享,写是独立的原则。
*/
return 0;
}
这是一段用于创建N个子进程的程序代码。并且验证父子进程之间能够进行资源共享的资源共享方式是:读时共享写时独立。这些都不是重点。重点是,这个程序执行后会产生N个僵尸进程。为什么?因为我们没有进行回收。怎么回收?用wait()函数。
wait函数:该函数有三个功能:
1.阻塞等待子进程退出 (如果该子进程没有死亡,父进程阻塞,不做其他的事。)
2.回收子进程残留资源
3.获取子进程结束状态(退出原因)。
原型:pid_t wait(int *status);
返回值:成功:清理掉的子进程ID;失败:-1 (没有子进程)
需要注意的是:回收进程的工作是由当前进程的父进程来做。调用一次wait函数只会回收一个进程,若是多个进程死亡,wait函数只会回收最先死亡的那个进程。
上代码:
#include
<stdlib.h>
#include
<stdio.h>
#include
<unistd.h>
#include
<sys/types.h>
#include
<sys/wait.h>
int main()
{
int status;
int i = 0;
pid_t pid_1, pid_2;
for (; i != 1; i++)
{
pid_1 = fork();
if (!pid_1)
{
break;
}
}
if (i < 1)
{
sleep(5);
printf("I am the %d child.my ID is %d .\n", i + 1, getpid());
}
else
{
pid_2 = wait(&status);
if (-1 == pid_2)
{
perror("wait precess ");
exit(1);
}
printf("Recovery of the %d process is successful.It is ID %d .\n", i, pid_2);
if (WIFEXITED(status))
{
printf("Normal exit of process, Exit status : %d .\n", WEXITSTATUS(status));
}
else
if (WIFSIGNALED(status))
{
printf("The program exits with abnormal signal, signal value : %d.\n", WTERMSIG(status));
}
printf("I am is father.\n");
}
//printf("hello from Recycle_child_process!\n");
return 0;
}
我fork了一个子进程,并让它休眠5s。注意我的wait函数是在父进程分支中调用的,这里涉及到几个宏函数:
WIFEXITED(status) 为非0 → 进程正常结束
WEXITSTATUS(status) 如上宏为真,使用此宏 → 获取进程退出状态 (exit的参数)
WIFSIGNALED(status) 为非0 → 进程异常终止
WTERMSIG(status) 如上宏为真,使用此宏 → 取得使进程终止的那个信号的编号。
这样,程序会输出:
I am the 1 child.my ID is 6024 .
Recovery of the 1 process is successful.It is ID 6024 .
Normal exit of process,Exit status:0 .
I am is father.
ID应该是不会一样的。
程序显示该子进程是正常退出的,返回值为0;我们可以改动这个返回值:用return 语句
if (i < 1)
{
sleep(5);
printf("I am the %d child.my ID is %d .\n", i + 1, getpid());
return 1314520;
}
那么程序的输出就应该是:
I am the 1 child.my ID is 6024 .
Recovery of the 1 process is successful.It is ID 6024 .
Normal exit of process,Exit status: .
I am is father.
那我们怎么让子进程异常退出?毕竟平常也很少遇到。我们可以人为的营造环境,段错误知道吧?浮点异常知道吧?不知道可以百度或谷歌。
我们来编写一个程序:
#include
<stdio.h>
int main(void)
{
int i = 5 / 0;//浮点异常
char *p = "love dandan";
p[2] = 69;
return 0;
}
编译并命名为a.out。然后我们修改程序为:
if (i < 1)
{
execl(" / home / lovedan / projects / Recycle_child_process / bin / x64 / Debug / a.out", "a.out", NULL);//第一个参数一定是绝对路径。相信你知道execl函数
/*sleep(5);
printf("I am the %d child.my ID is %d .\n", i + 1, getpid());*/
}
这时候,程序的输出。。。。输出结果我丢失了,也不想在去重新弄了。反正吧,他会输出子进程被成功回收,但是子进程是异常退出。提示浮点异常,value值是8(即:SIGFPE)。将in i=5/0;注释掉之后在编译它,重新运行另外一个程序输出结果又会不一样。会提示我们子进程因为段错误而退出。
大概就这些吧,后面还有一个waitpid函数。后面在来介绍吧。
来更新一下:循环回收多个子进程:
上面的示例是指创建回收一个子进程,若是我们创建多个,该如何回收呢?用循环!
for (; i != 10; i++)
{
pid_1 = fork();
if (!pid_1)
{
break;
}
}
if (i < 10)
{
sleep(i);
printf("I am the %d child.my ID is %d .\n", i + 1, getpid());
}
else
{
sleep(i);
while (pid_2 = wait(&status) != -1)//循环回收进程
{
printf("Recovery of the %d process is successful.It is ID %d .\n", i, pid_2);
if (WIFEXITED(status))
{
printf("Normal exit of process, Exit status : %d .\n", WEXITSTATUS(status));
}
else
if (WIFSIGNALED(status))
{
printf("The program exits with abnormal signal, signal value : %d.\n", WTERMSIG(status));
}
printf("I am is father.\n");
}
if (-1 == pid_2)
{
perror("wait precess ");
exit(1);
}
}
我就只是贴了我修改的代码。其实变动很少,就是加个循环控制而已,这个程序的输出是:
I am the 1 child.my ID is 1225 .
I am the 2 child.my ID is 1226 .
I am the 3 child.my ID is 1227 .
I am the 4 child.my ID is 1228 .
I am the 5 child.my ID is 1229 .
I am the 6 child.my ID is 1230 .
I am the 7 child.my ID is 1231 .
I am the 8 child.my ID is 1232 .
I am the 9 child.my ID is 1233 .
I am the 10 child.my ID is 1234 .
Recovery of the 10 process is successful.It is ID 1 .
Normal exit of process,Exit status:0 .
I am is father.
Recovery of the 10 process is successful.It is ID 1 .
Normal exit of process,Exit status:0 .
I am is father.
Recovery of the 10 process is successful.It is ID 1 .
Normal exit of process,Exit status:0 .
I am is father.
Recovery of the 10 process is successful.It is ID 1 .
Normal exit of process,Exit status:0 .
I am is father.
Recovery of the 10 process is successful.It is ID 1 .
Normal exit of process,Exit status:0 .
I am is father.
Recovery of the 10 process is successful.It is ID 1 .
Normal exit of process,Exit status:0 .
I am is father.
Recovery of the 10 process is successful.It is ID 1 .
Normal exit of process,Exit status:0 .
I am is father.
Recovery of the 10 process is successful.It is ID 1 .
Normal exit of process,Exit status:0 .
I am is father.
Recovery of the 10 process is successful.It is ID 1 .
Normal exit of process,Exit status:0 .
I am is father.
Recovery of the 10 process is successful.It is ID 1 .
Normal exit of process,Exit status:0 .
I am is father.
有没有发现回收的ID全为1?因为子进程先于父进程结束啊,是正常的结束,返回值为0;如果想要测试异常结束,请自行营造环境。
在Linux中简单实现回收子进程的更多相关文章
- LINUX中简单的字符命令
1. ls 查看目录中的内容 -a 查看隐藏文件 -l 显示文件的详细信息 -d 显示目录属性 -h 人性化显示文件大小 -i 显示ID号 2. 目录操作 创建目录 mkdir [-p](递归) di ...
- 用 set follow-fork-mode child即可。这是一个 gdb 命令,其目的是告诉 gdb 在目标应用调用fork之后接着调试子进程而不是父进程,因为在 Linux 中fork系统调用成功会返回两次,一次在父进程,一次在子进程
GDB的那些奇淫技巧 evilpan 收录于 Security 2020-09-13 约 5433 字 预计阅读 11 分钟 709 次阅读 gdb也用了好几年了,虽然称不上骨灰级玩家,但 ...
- Linux中vim的简单配置
本文主要分享Linux中vim的简单配置 ★配置文件的位置 在目录/etc.下面,有个名为vimrc的文件,这就是系统中公共的vim配置文件,对所有用户都开放.而在每个用户的主目录下,都可以自 ...
- Linux 中 Vi 编辑器的简单操作
Linux 中 Vi 编辑器的简单操作 Vi 编辑器一共有3种模式:命名模式(默认),尾行模式,编辑模式.3种模式彼此需要切换. 一.进入 Vi 编辑器的的命令 vi filename //打开或新 ...
- [软件测试]Linux环境中简单清爽的Google Test (GTest)测试环境搭建(初级使用)
本文将介绍单元测试工具google test(GTEST)在linux操作系统中测试环境的搭建方法.本文属于google test使用的基础教程.在linux中使用google test之前,需要对如 ...
- Linux中DHCP服务器的简单配置
我安装了两台linux系统,一个作为服务器,一个客户端 两个都有3个网卡, 后两个网卡聚合为zhi一个网卡:Linux 网卡聚合 两台电脑都一样. 那么如何为这个聚合网卡进行DHCP的分配呢? 1.由 ...
- Linux中DHCP服务器的简单配置(转)
我安装了两台linux系统,一个作为服务器,一个客户端 两个都有3个网卡, 后两个网卡聚合为zhi一个网卡:Linux 网卡聚合 两台电脑都一样. 那么如何为这个聚合网卡进行DHCP的分配呢? 1.由 ...
- Linux 的简单命令以及在idea中配置码云
Linux 的简单命令: ls(list)功能:列出目录内容 cd(change directory)功能:切换目录 touch 1.txt 在当前目录创建一个文件1.txt clear:清除屏幕 p ...
- linux中几个简单的系统命令(还有一些其他杂项命令)
linux中几个简单的系统命令,其他命令接触到了在补充. 1.ps命令:(process status),提供对进程的一次性查看.以及执行ps命令时那个时刻的进程信息 格式:ps[参数] -e 此参数 ...
随机推荐
- Dom操作的常用案例实现
本文介绍几个Dom操作的几个常用的案例.虽然现在各种web框架层出不穷,也很方便.但是了解最基本的实现方法对我们开发还是有很大的帮助的: 1.图片滚动案例 1.1 效果如下: 1.2 代码如下: ...
- [UE4][Canvas]用C++代码绘制血条(HealthBar)
转自:http://aigo.iteye.com/blog/2275110 参考自Epic官方项目StrategyGame 血条效果: StrategyHUD.h StrategyHUD.cpp
- MySQL产生随机字符
MySQL产生随机字符 UUID简介 UUID含义是通用唯一识别码 (Universally Unique Identifier),这是一个软件建构的标准,也是被开源软件基金会 (Open Softw ...
- win10间歇性的找不到usb设备
自从安装了win10,感觉掉了一个好大的坑. 比如win10经常找不到usb 设备,有时候过5-6分钟又有了.除了驱动的问题之外,有时候重启一下就好了. 今天又有一个小发现,笔记本为了省电,会把usb ...
- JavaBean转xml
JavaBean转xml的工具就是:XStream XStream的作用: XStream可以把JavaBean对象转换成XML!通常服务器向客户端响应的数据都是来自数据库的一组对象,当我们不能直接把 ...
- django,flask接口初试
-------------django 1.需要安装的环境 pip install django pip install djangorestframework ## restful组件 pip in ...
- 性能测试day01_性能基本概念
其实第一次接触性能是15年的时候,懵懵懂懂的被领导拉去做第一次做性能压测,如今有机会重新听一下云层大大讲解性能,于是打算以此博客记录下整个学习的过程,如若有不同意见者可以在下面留言指出,也欢迎大家一起 ...
- jquery前期总结,及实例
一.元素查找 1.选择器 id=d------------------------------------------>$("#d") class=c1----------- ...
- Linux中使用python测试主机存活 Linux系统CentOS Linux release 7.3.1611 (Core) py版本Python 2.7.5
下面是最初的情况 #/usr/bin/env python # -*- coding: utf-8 -*- import os import time import subprocess import ...
- 权重比较(id class 标签)
权重的取值: 按照 id class 标签 的顺序在其位置上标出1或者0 例如下面的例子 <!DOCTYPE html> <html lang="en"&g ...