linux程序退出的時exit,_exit區別和聯絡
一,程序終止有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.
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 <stdio.h> #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 <stdio.h> #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沒有執行
四、abort意義
abort 產生 SIGABRT 訊號。非正常退出,即在程式碰到災難性錯誤時強制退出。由於是非正常退出,因此不會做其它任何操作。
五、程序和函式結束的總結
1、atexit 是在執行 exit 函式時設定要作的工作,實際就是呼叫要在程序退出想讓程式執行的函式。
其引數是要呼叫的函式地址。引數函式是一個無引數無返回值的函式。atexit可以登記32個函式,這些函式由 exit 函式自動呼叫,登記的順序和呼叫的順序相反,即最後登記的先執行。同一函式登記多次也會呼叫多次。
2、exit :是一個C庫標準函式。此函式執行會首先呼叫由 atexit註冊的函式,然後執行關閉所有標準IO流,重新整理流緩衝區等操作。
3、_exit : 是一個系統呼叫。此函式不會呼叫 atexit 註冊的函式,也不會執行訊號處理程式。對標準IO流的緩衝區是否進行重新整理取決於該函式在系統中的實現。一般UNIX下不會重新整理。 exit函式會呼叫此函式。(在windows下的_exit函式會對標準IO流的緩衝進行重新整理)
_Exit : 是一個C庫標準函式。其動作類似 _exit 。
4、非main函式在使用return返回的時候表示函式呼叫棧的返回,return是語言級別的。而exit是系統呼叫級別的,它表示了一個程序的結束。exit函式是退出應用程式,並將應用程式的一個狀態返回給OS,這個狀態標識了應用程式的一些執行資訊。在main函式裡面return(0)和exit(0)是一樣的,子函式用return返回;而子程序用exit退出,呼叫exit時要呼叫一段終止處理程式,然後關閉所有I/O流。
在程序操作中exit是結束當前程序或程式並把控制權返回給呼叫該程式或者程序的程序即父程序並告訴父程序該當前程序的執行狀態,而return是從當前函式返回,如果是在main函式中,main函式結束時隱式地呼叫exit函式,自然也就結束了當前程序。
六、總結
exit和_exit函式用於正常終止一個程式: _exit立即進入核心, exit則先執行一些清除處理(包括呼叫執行各終止處理程式,關閉所有標準I / O流等),然後進入核心。函式的返回的程序的退出之間有很大的不同,因此我們在使用的時候需要區分和深刻的理解。