linux系统编程之进程(四):进程退出exit,_exit区别即atexit函数(转载)
一,进程终止有5种方式:
正常退出:
- 从main函数返回
- 调用exit
- 调用_exit
异常退出:
- 调用abort
- 由信号终止
二,exit和_exit区别:

关于_exit():
#include <unistd.h>
void _exit(int status);
#include <stdlib.h>
void _Exit(int status);
DESCRIPTION
The function _exit() terminates the calling process "immediately". Any
open file descriptors belonging to the process are closed; any children
of the process are inherited by process 1, init, and the process’s par-
ent is sent a SIGCHLD signal.
The value status is returned to the parent process as the process’s
exit status, and can be collected using one of the wait(2) family of
calls.
The function _Exit() is equivalent to _exit().
关于exit():
#include <stdlib.h>
void exit(int status);
DESCRIPTION
The exit() function causes normal process termination and the value of
status & 0377 is returned to the parent (see wait(2)).
All functions registered with atexit(3) and on_exit(3) are called, in
the reverse order of their registration. (It is possible for one of
these functions to use atexit(3) or on_exit(3) to register an addi-
tional function to be executed during exit processing; the new regis-
tration is added to the front of the list of functions that remain to
be called.) If one of these functions does not return (e.g., it calls
_exit(2), or kills itself with a signal), then none of the remaining
functions is called, and further exit processing (in particular, flush-
ing of stdio(3) streams) is abandoned. If a function has been regis-
tered multiple times using atexit(3) or on_exit(3), then it is called
as many times as it was registered.
All open stdio(3) streams are flushed and closed. Files created by
tmpfile(3) are removed.
The C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE,
that may be passed to exit() to indicate successful or unsuccessful
termination, respectively.
和exit比较一下,exit()函数定义在stdlib.h中,而_exit()定义在unistd.h中,
注:exit()就是退出,传入的参数是程序退出时的状态码,0表示正常退出,其他表示非正常退出,一般都用-1或者1,标准C里有EXIT_SUCCESS和EXIT_FAILURE两个宏,用exit(EXIT_SUCCESS);
_exit()函数的作用最为简单:直接使进程停止运行,清除其使用的内存空间,并销毁其在内核中的各种数据结构;exit() 函数则在这些基础上作了一些包装,在执行退出之前加了若干道工序。
exit()函数与_exit()函数最大的区别就在于exit()函数在调用exit系统调用之前要检查文件的打开情况,把文件缓冲区中的内容写回文件,就是"清理I/O缓冲"。
exit()在结束调用它的进程之前,要进行如下步骤:
1.调用atexit()注册的函数(出口函数);按ATEXIT注册时相反的顺序调用所有由它注册的函数,这使得我们可以指定在程序终止时执行自己的清理动作.例如,保存程序状态信息于某个文件,解开对共享数据库上的锁等.
2.cleanup();关闭所有打开的流,这将导致写所有被缓冲的输出,删除用TMPFILE函数建立的所有临时文件.
3.最后调用_exit()函数终止进程。
_exit做3件事(man):
1,Any open file descriptors belonging to the process are closed
2,any children of the process are inherited by process 1, init
3,the process's parent is sent a SIGCHLD signal
exit执行完清理工作后就调用_exit来终止进程。
三,atexit()
atexit可以注册终止处理程序,ANSI C规定最多可以注册32个终止处理程序。
终止处理程序的调用与注册次序相反
#include <stdlib.h>
int atexit(void (*function)(void));
DESCRIPTION
The atexit() function registers the given function to be called at nor-
mal process termination, either via exit(3) or via return from the pro-
gram’s main(). Functions so registered are called in the reverse order
of their registration; no arguments are passed.
The same function may be registered multiple times: it is called once
for each registration.
POSIX.1-2001 requires that an implementation allow at least ATEXIT_MAX
(32) such functions to be registered. The actual limit supported by an
implementation can be obtained using sysconf(3).
When a child process is created via fork(2), it inherits copies of its
parent’s registrations. Upon a successful call to one of the exec(3)
functions, all registrations are removed.
RETURN VALUE
The atexit() function returns the value 0 if successful; otherwise it
returns a non-zero value.
示例程序:
#include <unistd.h>
#include <stdlib.h>
void fun1() { printf("fun1 is called\n"); }
void fun2() { printf("fun2 is called\n"); }
int main(void) {
printf("main function\n");
atexit(fun1);
atexit(fun2);
atexit(fun1);
exit(EXIT_SUCCESS);
}
运行结果:

当调用fork时,子进程继承父进程注册的atexit:
示例程序:
#include <stdlib.h>
#define ERR_EXIT(m) \
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}\
while (0)\
void fun1(){
printf("fun1 is called\n");
}
void fun2(){
printf("fun2 is called\n");
}
int main(void)
{
pid_t pid;
pid = fork();
atexit(fun1);
atexit(fun2);
atexit(fun1);
if(pid == -1)
ERR_EXIT("fork error");
if(pid == 0){
printf("this is child process\n");
}
if(pid > 0){
printf("this is parent process\n");
}
return 0;
}

运行结果:

当atexit注册的函数中有一个没有正常返回或被kill,则后续的注册函数都不会被执行
示例程序:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h> void fun1()
{
printf("fun1 is called\n");
} void fun2()
{
printf("fun2 is called\n");
kill(getpid(),SIGINT);
} int main(void)
{
printf("main function\n");
if(signal(SIGINT,SIG_DFL) == SIG_ERR){
perror("signal error");
exit(EXIT_FAILURE);
}
atexit(fun1);
atexit(fun2);
atexit(fun1);
exit(EXIT_SUCCESS);
}
运行结果:

可见最后那个fun1没有执行
linux系统编程之进程(四):进程退出exit,_exit区别即atexit函数(转载)的更多相关文章
- Linux系统编程(8)—— 进程之进程控制函数fork
fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同的事. 一个进程调用fork()函数后,系统先 ...
- Linux系统编程(9)—— 进程之进程控制函数exec系列函数
在Linux中,并不存在exec()函数,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **environ; int exe ...
- Linux系统编程(7)—— 进程之进程概述
我们知道,每个进程在内核中都有一个进程控制块(PCB)来维护进程相关的信息,Linux内核的进程控制块是task_struct结构体.现在我们全面了解一下其中都有哪些信息. 进程id.系统中每个进程有 ...
- linux系统编程之信号(四)
今天继续探讨信号相关的东东,话不多说,正入正题: 信号在内核中的表示: 下面用图来进一步描述这种信号从产生到递达之间的状态(信号阻塞与未诀): 那是怎么来决定的呢?下面慢慢来举例分解: 所以,通过 ...
- Linux系统编程@进程通信(一)
进程间通信概述 需要进程通信的原因: 数据传输 资源共享 通知事件 进程控制 Linux进程间通信(IPC)发展由来 Unix进程间通信 基于System V进程间通信(System V:UNIX系统 ...
- linux系统编程之进程(一)
今天起,开始学习linux系统编程中的另一个新的知识点----进程,在学习进程之前,有很多关于进程的概念需要了解,但是,概念是很枯燥的,也是让人很容易迷糊的,所以,先抛开这些抽象的概念,以实际编码来熟 ...
- Linux系统编程@进程管理(二)
1.创建守护进程(Deamon) 守护进程的概念与作用 后台服务程序 – 系统服务,进程名字往往以’d’结尾,生存周期比较长(系统装入时启动,关闭时候终止.系统装入两种启动方式:1从启动脚本.etc/ ...
- LINUX系统编程 由REDIS的持久化机制联想到的子进程退出的相关问题
19:22:01 2014-08-27 引言: 以前对wait waitpid 以及exit这几个函数只是大致上了解,但是看REDIS的AOF和RDB 2种持久化时 均要处理子进程运行完成退出和父进程 ...
- 构建一个简单的Linux系统 MenuOs —— start_kernel到init进程(20135304刘世鹏)
构建一个简单的Linux系统 MenuOs —— start_kernel到init进程 作者:刘世鹏20135304 <Linux内核分析>MOOC课程http://mooc.study ...
随机推荐
- java中 SSL认证和keystore使用
java中 SSL认证和keystore使用 2013-10-12 11:08 10488人阅读 评论(0) 收藏 举报 目录(?)[+] 好久没用过SSL认证了,东西久不用,就有点生疏. ...
- appium 使用findElementByAndroidUIAutomator 定位元素示例
appium 使用findElementByAndroidUIAutomator 定位元素示例 import io.appium.java_client.remote.MobileCapability ...
- 听说每天都要写随笔,word哥~
今天主要学习了html的基本知识,进制的转换,无序列表,有序列表和表格,都是很基本的东西,然后自己自习了表单. <!DOCTYPE html PUBLIC "-//W3C//DTD X ...
- js中的函数,Date对象,Math对象和数组对象
函数就是完成某个功能的一组语句,js中的函数由关键字 function + 函数名 + 一组参数定义;函数在定义后可以被重复调用,通常将常用的功能写成一个函数,利用函数可以使代码的组织结构更多清晰. ...
- (转)OS X Mountain Lion 系统配置 Apache+Mysql+PHP 详细教程
如果你是一名 Web 开发者,很多时候都需要在本地搭建服务器测试环境,比如 Apache+Mysql+PHP 这样的环境.事实上 Mac OS X 中想要搭建这样的环境很简单,本文我们就会将详细的教程 ...
- nexus5 root教程
转载自: http://www.inexus.co/article-1280-1.html http://www.pc6.com/edu/71016.html https://download.cha ...
- 整型数组处理算法(八)插入(+、-、空格)完成的等式:1 2 3 4 5 6 7 8 9=N[华为面试题]
有一个未完成的等式:1 2 3 4 5 6 7 8 9=N 当给出整数N的具体值后,请你在2,3,4,5,6,7,8,9这8个数字的每一个前面,或插入运算符号“+”,或插入一个运算符号“-”,或不插入 ...
- linux防火墙开启-关闭
1.永久性生效,重启后不会复原 开启: chkconfig iptables on 关闭: chkconfig iptables off 2. 即时生效,重启后复原 开启: service iptab ...
- linux od命令
用户通常使用od命令查看特殊格式的文件内容.通过指定该命令的不同选项可以以十进制.八进制.十六进制和ASCII码来显示文件.od命令系统默认的显示方式是八进制,这也是该命令的名称由来(Octal Du ...
- vi & vim 基本指令(持续更新ing)
Abstract:1) 文本编辑模式: --INSERT--2)一般模式: --i.o.a.R--3)命令行命令模式 ...