exit和wait一起可以彻底清除子进程的资源
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
#include <errno.h>
int main()
{
pid_t p1,p2,pr;
int i;
for(i=;i<=;i++)
{
if((p1=fork())==)
{
printf("parent process%d child process%d\n",getppid(),getpid());
exit(); //我发现把return 0替换为exit(0)也可以的!,不过编译器这两种的处理区别很大,以后再做解释
}
else{
pr=wait(NULL);
//如果成功,wait会返回被收集的子进程的进程ID,
//如果调用进程没有子进程,调用就会失败,此时wait返回-1,同时errno被置为ECHILD。
if( pr>)
printf("I catched a child process with pid of %d\n", pr);
else
printf("error: %s\n.\n", strerror(errno));
}
}
return ;
}
输出结果如下:
parent process4595 child process4596
I catched a child process with pid of
parent process4595 child process4597
I catched a child process with pid of
parent process4595 child process4598
I catched a child process with pid of
当把exit注释后输出结果如下:
parent process4642 child process4643
parent process4643 child process4644
parent process4644 child process4645
I catched a child process with pid of
I catched a child process with pid of
parent process4643 child process4646
I catched a child process with pid of
I catched a child process with pid of
parent process4642 child process4647
parent process4647 child process4648
I catched a child process with pid of
I catched a child process with pid of
parent process4642 child process4649
I catched a child process with pid of
//重新执行一边的结果
parent process4657 child process4658
parent process4658 child process4659
parent process4659 child process4660
I catched a child process with pid of
I catched a child process with pid of
parent process4658 child process4661
I catched a child process with pid of
I catched a child process with pid of
parent process4657 child process4662
parent process4662 child process4663
I catched a child process with pid of
I catched a child process with pid of
parent process4657 child process4664
I catched a child process with pid of
从上面的输出结果可以得出:exit可以用来释放进程的资源,必须加上,当注释掉时,可能因为子进程的资源没有
及时清理掉,所以导致wait阻塞住,不能及时清理掉子进程!
总结:当进程发出exit该调用时,内核会释放进程占有的资源,释放进程上下文所占的内存空间,保留
进程表项,将进程表项中记录进程状态的关键字设为僵死状态。内核在进程收到不可扑捉的信号时,会从内核内部调用exit
,使得进程退出。父进程通过wait,并释放进程表项。
exit和wait一起可以彻底清除子进程的资源的更多相关文章
- 清除nginx静态资源缓存
之前写过一篇如何配置nginx缓存及手动清除缓存的文章: http://www.cnblogs.com/Eivll0m/p/4921829.html 但如果有大量缓存需要清理,手动一条条清理就比较慢了 ...
- 子进程回收资源两种方式,僵尸进程与孤儿进程,守护进程,进程间数据隔离,进程互斥锁,队列,IPC机制,线程,守护线程,线程池,回调函数add_done_callback,TCP服务端实现并发
子进程回收资源两种方式 - 1) join让主进程等待子进程结束,并回收子进程资源,主进程再结束并回收资源. - 2) 主进程 “正常结束” ,子进程与主进程一并被回收资源. from multipr ...
- [Docker基础]如何清除不用的资源
Docker - How to cleanup resources 有时你可能需要清理Docker中不用的资源,特别是在学习Docker过程中创建的镜像.容器.网络.存储卷等. delete volu ...
- Linux C _exit函数与exit函数的联系与区别
一.联系 1.功能上,_exit和exit函数都是让进程正常退出,即关闭进程所打开的文件描述符,释放已占用内存和其他资源. 二.区别 1._exit函数在头文件unistd.h中声明,而exit在头文 ...
- 回收进程用户空间资源 exit()函数 _exit()函数 atexit()函数 on_exit()函数
摘要:本文主要讲述进程的终止方式,以及怎样使用exit()函数来终止进程.回收进程用户空间资源:分析了exit()函数与_exit()函数,returnkeyword的差异.同一时候具体解读了怎样使用 ...
- Android 与Java 进程退出 killProcess与System.exit
android所有activity都在主进程中,在清单文件Androidmanifest.xml中可以设置启动不同进程,Service需要指定运行在单独进程?主进程中的主线程?还是主进程中的其他线程? ...
- 在Linux中简单实现回收子进程
学习到wait函数了,这个函数的作用是用来回收进程.一般来说,正常退出的进程是不需要我们来专门回收的.但是进程有这两种:孤儿进程和僵尸进程. 孤儿进程: 通俗点说就是父进程先于子进程死亡.此时子进程就 ...
- 进程退出exit、_exit、abort
分为正常退出,异常退出 正常退出的方法: 1.在main函数中执行return 2.调用exit函数 3.调用_exit 函数 ----------------------------------- ...
- 关于进程exit后,内存释放释放的实践
最近碰到一个问题,或许也是小猿们都会碰到的问题:内存泄露. 都知道malloc后需要free才能释放内存,shmat后需要shmdt才能断掉内存区并使用IPC_RMID命令删除共享内存.那么如果是当前 ...
随机推荐
- pdo,更高的sql安全性
介绍 PHP 数据对象 (PDO) 扩展为PHP访问数据库定义了一个轻量级的一致接口.实现 PDO 接口的每个数据库驱动可以公开具体数据库的特性作为标准扩展功能. 注意利用 PDO 扩展自身并不能实现 ...
- 【LeetCode】12. Integer to Roman (2 solutions)
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【LeetCode】41. First Missing Positive (3 solutions)
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- android语音识别方法
http://www.apkbus.com/forum.php?mod=viewthread&tid=3473 android语音识别方法一:使用intent调用语音识别程序 1. 说明 以下 ...
- Python lower() 方法
描述 Python lower() 方法转换字符串中所有大写字符为小写,其效果和 casefold() 方法非常相似. 两者的区别是:lower() 方法只对ASCII编码,也就是‘A-Z’有效,对于 ...
- C++拷贝函数的小结,关于变量的作用域等---ShinePans
#include <iostream> using namespace std; class circle { private: double r0; public: circle(dou ...
- ubuntu中pip安装redis-py及pip的使用
安装redis-py的前提是已经将redis成功安装,redis安装过程请看博文 ubuntu14安装redis 1.安装pip sudo apt-get install python-pip 2.使 ...
- django中cookies和session
django中cookies和session是两个经常使用的用户认证工具.都是类似于字典的数据类型,都是request的内部属性 cookies的读写方法 cookies读,比如username us ...
- 利用PHPExcel实现数据保存到excel文件
include(dirname(__FILE__) .'/phpexcel-1.7.7/Classes/PHPExcel.php'); include(dirname(__FILE__) .'/php ...
- SGU 114. Telecasting station 三分or找中位数
题目链接点这儿 一開始想都没想...直接上了三分...结果...sample的答案不一样...可是过了...然后又看了看. . . 发现这不就是高中或者初中出过的求中位数的题么. . .直接找到这些的 ...