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 ...
随机推荐
- linux mysql默认安装在哪个目录
MySQL安装完成后不象SQL Server默认安装在一个目录,它的数据库文件.配置文件和命令文件分别在不同的目录,了解这些目录非常重要,尤其对于Linux的初学者,因为 Linux本身的目录结构就比 ...
- 今天在写powershell脚本中犯的两个错误
可能是因为牙痛没睡好,今天老是犯错,还是脚本写错,特别难调. 第一个错误: powershell脚本里面,函数与函数互相调用的传参.其实就像调用普通的cmdlet一样的写法,应该这么写: Add-Sc ...
- 转载: C#: Left outer joins with LINQ
I always considered Left Outer Join in LINQ to be complex until today when I had to use it in my app ...
- C语言文件详解
1.C语言FILE类,在stdio.h头文件中,FILE类是一个结构体:定义如下: 通过typedef定义了 文件类型 的别名: “FILE”,这样以后需要读写文件的时候直接定义FILE就行了. ...
- SMO启发式选择
%% % svm 简单算法设计 --启发式选择 %% clc clear close all % step=0.05;error=1.2; % [data, label]=generate_sampl ...
- [置顶] SQL注入问题
我们做系统,有没有想过,自己的容量很大的一个数据库就被很轻易的进入,并删除,是不是很恐怖的一件事.这就是sql注入. 一.SQL注入的概念 SQL注入攻击指的是通过构建特殊的输入作为参 ...
- bash:command not found
在linux下执行某一常用命令时,提示类似错误信息:bash:bash:command not found 原因是所执行的命令在当前系统环境变量里找不到路径. 例如:刚安装了openOffice时,执 ...
- Linux 数据 CD 刻录
http://www.cyberciti.biz/tips/linux-burning-multi-session-cds-on-linux.html #mkisofs -dvd-video -inp ...
- 使Asp.net WebApi支持JSONP和Cors跨域访问
1.服务端处理 同源策略(Same Origin Policy)的存在导致了“源”自A的脚本只能操作“同源”页面的DOM,“跨源”操作来源于B的页面将会被拒绝.同源策略以及跨域资源共享在大部分情况下针 ...
- Oracle数据库中的blob类型解析
Oracle的Blob字段比较特殊,他比long字段的性能要好很多,可以用来保存例如图片之类的二进制数据. 写入Blob字段和写入其它类型字段的方式非常不同,因为Blob自身有一个cursor,你必须 ...