exit 中文man頁面
NAME
exit - 使程序正常中止
SYNOPSIS 總覽
#include <stdlib.h> void exit(int status);
DESCRIPTION 描述
函數 exit() 使得程序正常中止,status & 0377 的值被返回給父進程 (參見 wait(2)) 。所有用 atexit() 和 on_exit() 注冊的函數都以與注冊時相反的順序被依次執行。使用 tmpfile() 創建的文件被刪除。
C 標準定義了兩個值 EXIT_SUCCESS 和 EXIT_FAILURE,可以作為 exit() 的參數,來分別指示是否為成功退出。
RETURN VALUE 返回值
函數 exit() 不會返回。
CONFORMING TO 標準參考
SVID 3, POSIX, BSD 4.3, ISO 9899 (``ANSI C'')
NOTES 要點
在 exit 處理過程中,可能會使用 atexit() 和 on_exit() 注冊其他的函數。通常,***注冊的函數被從已注冊函數鏈中摘下來,然后執行。如果在處理過程中,又調用了 exit() 或 longjmp(),那么發生的行為是未定義的。
相對于使用 0 和非零值 1 或 -1,使用 EXIT_SUCCESS 和 EXIT_FAILURE 可以稍微增加一些可移植性 (對非 Unix 環境)。特別的,VMS 使用一種不同的約定。
BSD 試圖標準化退出代碼 - 參見文件 <sysexits.h> 。
exit() 之后,退出狀態必須傳遞給父進程。這里有三種情況。如果父進程已設置了 SA_NOCLDWAIT,或者已將 SIGCHLD 的處理句柄設置成了 SIG_IGN,這個狀態將被忽略。這時要退出的進程立即消亡。如果父進程沒有表示它對退出狀態不感興趣,僅僅是不再等待,那么要退出的程序變成一個僵尸進程 (``zombie'',除了包含一個字節的退出狀態外,什么也不是)。這樣在父進程后來調用 wait() 函數族之一時,可以得到退出狀態。
如果所用實現支持 SIGCHLD 信號,信號將被發送到父進程。如果父進程已設置了 SA_NOCLDWAIT,它被取消定義。(?)
如果進程是一個 session leader,它的控制終端是會話的控制終端,那么這個終端的前臺進程組的每個進程都將收到 SIGHUP 信號;終端將與這個會話斷開,可以再被一個新的控制進程獲得。
如果進程的退出使得一個進程組成為孤兒,并且這個新近成為孤兒的進程組中任何的進程被中止,進程組中所有的進程將依次收到 SIGHUP 和 SIGCONT 信號。
SEE ALSO 參見
_exit(2), wait(2), atexit(3), on_exit(3), tmpfile(3)
#p#
NAME
exit - cause normal program termination
SYNOPSIS
#include <stdlib.h> void exit(int status);
DESCRIPTION
The exit() function causes normal program termination and the the value of status & 0377 is returned to the parent (see wait(2)). All functions registered with atexit() and on_exit() are called in the reverse order of their registration, and all open streams are flushed and closed. Files created by tmpfile() are removed.
The C standard specifies two defines EXIT_SUCCESS and EXIT_FAILURE that may be passed to exit() to indicate successful or unsuccessful termination, respectively.
RETURN VALUE
The exit() function does not return.
CONFORMING TO
SVID 3, POSIX, BSD 4.3, ISO 9899 (``ANSI C'')
NOTES
During the exit processing, it is possible to register additional functions with atexit() and on_exit(). Always the last-registered function is removed from the chain of registered functions, and invoked. It is undefined what happens if during this processing either exit() or longjmp() is called.
The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to non-Unix environments) than that of 0 and some nonzero value like 1 or -1. In particular, VMS uses a different convention.
BSD has attempted to standardize exit codes - see the file <sysexits.h>.
After exit(), the exit status must be transmitted to the parent process. There are three cases. If the parent has set SA_NOCLDWAIT, or has set the SIGCHLD handler to SIG_IGN, the status is discarded. If the parent was waiting on the child it is notified of the exit status. In both cases the exiting process dies immediately. If the parent has not indicated that it is not interested in the exit status, but is not waiting, the exiting process turns into a "zombie" process (which is nothing but a container for the single byte representing the exit status) so that the parent can learn the exit status when it later calls one of the wait() functions.
If the implementation supports the SIGCHLD signal, this signal is sent to the parent. If the parent has set SA_NOCLDWAIT, it is undefined whether a SIGCHLD signal is sent.
If the process is a session leader and its controlling terminal the controlling terminal of the session, then each process in the foreground process group of this controlling terminal is sent a SIGHUP signal, and the terminal is disassociated from this session, allowing it to be acquired by a new controlling process.
If the exit of the process causes a process group to become orphaned, and if any member of the newly-orphaned process group is stopped, then a SIGHUP signal followed by a SIGCONT signal will be sent to each process in this process group.
SEE ALSO
_exit(2), wait(2), atexit(3), on_exit(3), tmpfile(3)