一,进程终止有5种方式:

正常退出:

从main函数返回

调用exit

调用_exit

异常退出:

调用abort

由信号终止

二,exit和_exit区别:

关于_exit():

#include

void _exit(int status);

#include

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

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

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

#include

#include

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

#include

#include

#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

#include

#include

#include

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是怎么回事,linux 进程退出exit,_exit区别即atexit函数相关推荐

  1. linux内存显示3.54g,为什么WDCP/linux服务器内存一直显示几乎用完了

    1.在WDCP的首页显示系统信息处,可以看到4G内存使用几乎用完了. 2.进入到内存管理中,看下实际的内存使用情况.同样显示内存几乎被占完. 3.接下来我们点下"强制释放内存",可 ...

  2. linux打印显示etc中的文件,Linux命令之文件内容查看(cat、tac、nl、more、less、head、tail)...

    本文将学到的命令 cat tac nl more less head tail cat(从第一行开始显示文件内容) 首先来看一下官方文档 man cat CAT(1) GNU CAT(1) NAME ...

  3. linux不能显示文本文件内容的命令,Linux之文本文件查看命令

    Linux中,常用的文本文件查看命令介绍如下: 1. cat 用法: cat [options] filename options: -A: 显示全部. -E: 在每一行的后面加上"$&qu ...

  4. linux内存显示buff,能不能让Linux不要对内存buff/cache辣么饥渴?

    20 个回复 iwannabe I wanna be 2019-02-13 http://www.newsmth.net/nForum/article/KernelTech/72788?s=72788 ...

  5. linux命令显示进度,【命令】Linux下查看dd命令写入进度

    [命令]Linux下查看dd命令写入进度 12个月前 (05-14)    作者:Jiaozn    分类:Linux    阅读次数:433 评论(0) Linux下有一个强力工具,dd,用来操作镜 ...

  6. linux一直显示探测文件,如何在Linux中检测文件访问?

    一种选择是使用strace: strace -o logfile -eopen yourapp 这将记录所有文件打开事件,但会造成性能上的重大下降.但是,它具有易于使用的优点. 另一种选择是使用LD_ ...

  7. p2000显卡LINUX无显示,petalinux 2020.01工程 linux系统启动不完全

    重新修改了vivado工程  可以启动到这了  但是又停了!!!!啊!!!! [ 5.644960] hub 2-0:1.0: 1 port detected [ 5.707005] mmc0: ne ...

  8. Linux实验三父子进程每隔3秒,实验三进程的创建和简单控制(学生分析.doc

    实验三进程的创建和简单控制(学生分析 实验 进程的创建和简单控制 实验目的: 掌握进程的概念和进程的状态,对进程有感性的认识: 掌握进程创建方法: 认识进程的并发执行,了解进程族之间各种标识及其存在的 ...

  9. Linux命令任务管理器,linux top显示命令详解(任务管理器)

    linux top显示命令详解(任务管理器) linux中top命令能够实时显示系统中各个进程的资源占用状况(比如cpu.内存的使用),默认5秒刷新一下进程列表,所以类似于 Windows 的任务管理 ...

  10. 安利一款Python开发的仿Linux树形显示目录tree命令

    大家好,我是小小明,今天我要给大家分享一个用python实现的仿Linux的tree命令. 详见:https://pypi.org/project/filestools/ 通过以下命令安装即可直接使用 ...

最新文章

  1. 输入vue ui没反应
  2. 切糕[HNOI2013]
  3. “非自回归”也不差:基于MLM的阅读理解问答
  4. 前端学习(2853):简单秒杀系统学习之页面事件
  5. 步进电机编写单4拍或4-8拍方式的汇编或c语言控制程序.,基于SCM和PLC的两种步进电机控制方法...
  6. 可以进行单元测试么_为什么程序员都讨厌写单元测试?有一个词叫“相爱相杀”!...
  7. 用canvas给自己的博客园加背景(二)
  8. 小米12系列有望于双12当天发布:贴合命名 争取骁龙8G1首发
  9. 腾讯广告算法大赛 | 第一周周冠军心得分享
  10. MySQL 基数的定义
  11. php 远程文件是否存在,如何通过php判断本地及远程文件是否存在
  12. mysql高性能学习笔记03_《高性能MySQL》学习笔记——第三章 服务器性能剖析
  13. 2021亚太杯数学建模C题全网成品论文+代码+详细思路+数据+参考文献
  14. TFT屏(OCM320240T350)调试总结
  15. vmware使用显卡
  16. 备份计算机软件,如何备份电脑里面的软件
  17. Android 仿微信回复评论点赞效果
  18. Webm如何转换mp4? 傻瓜式的操作方法来了
  19. OSCHINA开源中国
  20. 基于PHP的学生食堂管理系统

热门文章

  1. c# 操作FTP文件类
  2. javascript的一些各浏览器不兼容的地方
  3. 【转载】ARX程序再VS2002中的调试初探
  4. 使用Sharepoint Services 3.0构建基本网站
  5. html 伪元素原理,CSS伪类伪元素详解
  6. 类的加载过程详解之过程三:Initialization(初始化)阶段
  7. Spring Cloud Zuul网关集成JWT身份验证学习总结
  8. 排序的概念及分类实现
  9. Pandas python
  10. spring 注入 list和map及enum映射