一,进程终止有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 <unistd.h>
#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函数(转载)的更多相关文章

  1. Linux系统编程(8)—— 进程之进程控制函数fork

    fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同的事. 一个进程调用fork()函数后,系统先 ...

  2. Linux系统编程(9)—— 进程之进程控制函数exec系列函数

    在Linux中,并不存在exec()函数,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **environ; int exe ...

  3. Linux系统编程(7)—— 进程之进程概述

    我们知道,每个进程在内核中都有一个进程控制块(PCB)来维护进程相关的信息,Linux内核的进程控制块是task_struct结构体.现在我们全面了解一下其中都有哪些信息. 进程id.系统中每个进程有 ...

  4. linux系统编程之信号(四)

    今天继续探讨信号相关的东东,话不多说,正入正题: 信号在内核中的表示: 下面用图来进一步描述这种信号从产生到递达之间的状态(信号阻塞与未诀):   那是怎么来决定的呢?下面慢慢来举例分解: 所以,通过 ...

  5. Linux系统编程@进程通信(一)

    进程间通信概述 需要进程通信的原因: 数据传输 资源共享 通知事件 进程控制 Linux进程间通信(IPC)发展由来 Unix进程间通信 基于System V进程间通信(System V:UNIX系统 ...

  6. linux系统编程之进程(一)

    今天起,开始学习linux系统编程中的另一个新的知识点----进程,在学习进程之前,有很多关于进程的概念需要了解,但是,概念是很枯燥的,也是让人很容易迷糊的,所以,先抛开这些抽象的概念,以实际编码来熟 ...

  7. Linux系统编程@进程管理(二)

    1.创建守护进程(Deamon) 守护进程的概念与作用 后台服务程序 – 系统服务,进程名字往往以’d’结尾,生存周期比较长(系统装入时启动,关闭时候终止.系统装入两种启动方式:1从启动脚本.etc/ ...

  8. LINUX系统编程 由REDIS的持久化机制联想到的子进程退出的相关问题

    19:22:01 2014-08-27 引言: 以前对wait waitpid 以及exit这几个函数只是大致上了解,但是看REDIS的AOF和RDB 2种持久化时 均要处理子进程运行完成退出和父进程 ...

  9. 构建一个简单的Linux系统 MenuOs —— start_kernel到init进程(20135304刘世鹏)

    构建一个简单的Linux系统 MenuOs —— start_kernel到init进程 作者:刘世鹏20135304 <Linux内核分析>MOOC课程http://mooc.study ...

随机推荐

  1. 鼠标点击DIV后,DIV的背景变色(js)

    <!DOCTYPE html> <html> <head> <script> window.onload = function(){ var divs ...

  2. linux部署mongodb及基本操作

    原文:http://blog.csdn.net/jinzhencs/article/details/50930877 一.安装部署mongo 1.创建文件夹 /opt/mongodb/single / ...

  3. Android笔记(二):从savedIndstanceState发散

    savedIndstanceState savedIndstanceState位于ActivityonCreate(Bundle savedInstanceState)方法的参数中.对这个参数的理解要 ...

  4. C语言——文件

    需要理解的知识点:数据流.缓冲区.文件类型.文件存取方式   C语言可以将相关定义的数据按照内存的原样写入文件,这对于大规模数据来说比较方便,因为文件的尺寸比单纯的ASCII存储要小很多. 一.文件 ...

  5. Android 使用新浪微博SSO授权

    新浪微博SSO授权,很早就做好了,只是一直没有时间整理博客,今天加班,晚上闲暇之时便想到整理一下.由于整个七月份很忙,加班很多.前段时间把腾讯微博的SSO认证整理好了.想在七月份翻篇之前再写点东西.好 ...

  6. Linux文件的查找

    一直以来,总是记不住文件的查找命令,今天记在博客里,希望可以记得更牢! 1.脚本文件名的查询 which命令(寻找执行文件) #which ifconfig 2.文件名的查找 whereis 命令 # ...

  7. PLSQL Developer安装(Oracle11g+win7_64bit)

    1)安装Oracle 11g 64位 2)安装32位的Oracle客户端( instantclient-basic-win32-11.2.0.1.0)下载地址:http://www.oracle.co ...

  8. JS高级程序设计学习笔记之第三章基本概念(语法,数据类型,流控制语句,函数)——查漏补缺

    一.语法: 区分大小写; 2.标识符:就是指变量.函数.属性的名字,或者函数的参数 a.标志符的规则:①第一个字符必须是一个字母.下划线(_)或一个美元符号($).                   ...

  9. QT-Demo-Colck-01

    QT += widgets QT += core HEADERS += \ mainwindow.h SOURCES += \ mainwindow.cpp \ main.cpp #ifndef MA ...

  10. 【转】c/c++各种字符、字符串类型转换

    itoa   功 能:把一整数转换为字符串   用 法:char *itoa(int value, char *string, int radix);   详细解释:itoa是英文integer to ...