系統(tǒng)之家 - 系統(tǒng)光盤下載網(wǎng)站!

當前位置:系統(tǒng)之家 > 系統(tǒng)教程 > Linux下exit命令

Linux下exit命令和_exit命令區(qū)別盤點(2)

時間:2014-12-09 10:04:48 作者:qipeng 來源:系統(tǒng)之家 1. 掃描二維碼隨時看資訊 2. 請使用手機瀏覽器訪問: https://m.xitongzhijia.net/xtjc/20141208/32064.html 手機查看 評論

  更詳細的介紹:

  Calling exit() The exit() function causes normal program termination.

  The exit() function performs the following functions:

  1. All functions registered by the Standard C atexit() function are called in the reverse order of registration. If any of these functions calls exit(), the results are not portable. 2. All open output streams are flushed (data written out) and the streams are closed.

  3. All files created by tmpfile() are deleted.

  4. The _exit() function is called. Calling _exit() The _exit() function performs operating system-specific program termination functions. These include: 1. All open file descriptors and directory streams are closed.

  2. If the parent process is executing a wait() or waitpid(), the parent wakes up and status is made available.

  3. If the parent is not executing a wait() or waitpid(), the status is saved for return to the parent on a subsequent wait() or waitpid()。 4. Children of the terminated process are assigned a new parent process ID. Note: the termination of a parent does not directly terminate its children. 5. If the implementation supports the SIGCHLD signal, a SIGCHLD is sent to the parent. 6. Several job control signals are sent.

  為何在一個fork的子進程分支中使用_exit函數(shù)而不使用exit函數(shù)? ‘exit()’與‘_exit()’有不少區(qū)別在使用‘fork()’,特別是‘vfork()’時變得很 突出。

  ‘exit()’與‘_exit()’的基本區(qū)別在于前一個調(diào)用實施與調(diào)用庫里用戶狀態(tài)結(jié)構(user-mode constructs)有關的清除工作(clean-up),而且調(diào)用用戶自定義的清除程序 (自定義清除程序由atexit函數(shù)定義,可定義多次,并以倒序執(zhí)行),相對應,_exit函數(shù)只為進程實施內(nèi)核清除工作。 在由‘fork()’創(chuàng)建的子進程分支里,正常情況下使用‘exit()’是不正確的,這是 因為使用它會導致標準輸入輸出(stdio: Standard Input Output)的緩沖區(qū)被清空兩次,而且臨時文件被出乎意料的刪除(臨時文件由tmpfile函數(shù)創(chuàng)建在系統(tǒng)臨時目錄下,文件名由系統(tǒng)隨機生成)。在C++程序中情況會更糟,因為靜態(tài)目標(static objects)的析構函數(shù)(destructors)可以被錯誤地執(zhí)行。(還有一些特殊情況,比如守護程序,它們的父進程需要調(diào)用‘_exit()’而不是子進程;適用于絕大多數(shù)情況的基本規(guī)則是,‘exit()’在每一次進入‘main’函數(shù)后只調(diào)用一次。) 在由‘vfork()’創(chuàng)建的子進程分支里,‘exit()’的使用將更加危險,因為它將影響父進程的狀態(tài)。

  #include 《sys/types.h》; #include 《stdio.h》 int glob = 6; /* external variable in initialized data */ int main(void) { int var; /* automatic variable on the stack */ pid_t pid; var = 88; printf(“before vfork\n”; /* we don‘t flush stdio */ if ( (pid = vfork()) 《 0) printf(“vfork error\n”; else if (pid == 0) { /* child */ glob++; /* modify parent‘s variables */ var++; exit(0); /* child terminates */ //子進程中最好還是用_exit(0)比較安全。 } /* parent */ printf(“pid = %d, glob = %d, var = %d\n”, getpid(), glob, var); exit(0); } 在Linux系統(tǒng)上運行,父進程printf的內(nèi)容輸出:pid = 29650, glob = 7, var = 89

  子進程 關閉的是自己的, 雖然他們共享標準輸入、標準輸出、標準出錯等 “打開的文件”, 子進程exit時,也不過是遞減一個引用計數(shù),不可能關閉父進程的,所以父進程還是有輸出的。

  但在其它UNIX系統(tǒng)上,父進程可能沒有輸出,原 因是子進程調(diào)用了e x i t,它刷新關閉了所有標準I / O流,這包括標準輸出。雖然這是由子進程執(zhí)行的,但卻是在父進程的地址空間中進行的,所以所有受到影響的標準I/O FILE對象都是在父進程中的。當父進程調(diào)用p r i n t f時,標準輸出已被關閉了,于是p r i n t f返回- 1。

  在Linux的標準函數(shù)庫中,有一套稱作“高級I/O”的函數(shù),我們熟知的printf()、fopen()、fread()、fwrite()都在此 列,它們也被稱作“緩沖I/O(buffered I/O)”,其特征是對應每一個打開的文件,在內(nèi)存中都有一片緩沖區(qū),每次讀文件時,會多讀出若干條記錄,這樣下次讀文件時就可以直接從內(nèi)存的緩沖區(qū)中讀取,每次寫文件的時候,也僅僅是寫入內(nèi)存中的緩沖區(qū),等滿足了一定的條件(達到一定數(shù)量,或遇到特定字符,如換行符和文件結(jié)束符EOF), 再將緩沖區(qū)中的 內(nèi)容一次性寫入文件,這樣就大大增加了文件讀寫的速度,但也為我們編程帶來了一點點麻煩。如果有一些數(shù)據(jù),我們認為已經(jīng)寫入了文件,實際上因為沒有滿足特 定的條件,它們還只是保存在緩沖區(qū)內(nèi),這時我們用_exit()函數(shù)直接將進程關閉,緩沖區(qū)中的數(shù)據(jù)就會丟失,反之,如果想保證數(shù)據(jù)的完整性,就一定要使用exit()函數(shù)。

  Exit的函數(shù)聲明在stdlib.h頭文件中。

  _exit的函數(shù)聲明在unistd.h頭文件當中。

  下面的實例比較了這兩個函數(shù)的區(qū)別。printf函數(shù)就是使用緩沖I/O的方式,該函數(shù)在遇到“\n”換行符時自動的從緩沖區(qū)中將記錄讀出。實例就是利用這個性質(zhì)進行比較的。

  exit.c源碼

  #include 《stdlib.h》 #include 《stdio.h》 int main(void) { printf(“Using exit.。。\n”); printf(“This is the content in buffer”); exit(0); }

  輸出信息:

  Using exit.。。

  This is the content in buffer

  #include 《unistd.h》 #include 《stdio.h》 int main(void) { printf(“Using exit.。。\n”); //如果此處不加“\n”的話,這條信息有可能也不會顯示在終端上。 printf(“This is the content in buffer”); _exit(0); }

  則只輸出:

  Using exit.。。

  說明:在一個進程調(diào)用了exit之后,該進程并不會馬上完全消失,而是留下一個稱為僵尸進程(Zombie)的數(shù)據(jù)結(jié)構。僵尸進程是一種非常特殊的進程,它幾乎已經(jīng)放棄了所有的內(nèi)存空間,沒有任何可執(zhí)行代碼,也不能被調(diào)度,僅僅在進程列表中保留一個位置,記載該進程的退出狀態(tài)等信息供其它進程收集,除此之外,僵尸進程不再占有任何內(nèi)存空間。

  #include 《stdio.h》;

  int main() { printf(“%c”, ‘c‘); _exit(0); }

  上面就是Linux系統(tǒng)中exit與_exit的區(qū)別介紹了,它們的區(qū)別主要體現(xiàn)在函數(shù)庫中的定義,大部分情況還是相同的,你了解了嗎?

發(fā)表評論

0

沒有更多評論了

評論就這些咯,讓大家也知道你的獨特見解

立即評論

以上留言僅代表用戶個人觀點,不代表系統(tǒng)之家立場

其他版本軟件

熱門教程

人氣教程排行

Linux系統(tǒng)推薦

官方交流群 軟件收錄