C程序運行流程:
1.內核執行C startup routine。在該startup routine中,內核運行exec函數族中一個來運行C程序。
2.被exec運行的C程序 從main函數開始執行. ISO C規定的main函數原型爲如下形式:
int main(int argc, char *argv[]);
3.C程序運行完畢。以下爲程序正常退出的五種方式:
a. return from main
b. Calling exit
c. Calling _exit or _Exit
d. return of the last thread from its start routine
e. Calling pthread_exit from the last thread.
程序非正常退出的三種方式:
a. Calling abort
b. Receipt of a signal
c. Response of the last thread to a cancellation request.
Exit系列函數:
1.
void exit(int status);
程序調用該函數退出時,在退出之前會先做一些清理工作,比如關閉打開的文件等等。
2.
void _Exit(int status)
該函數是ISO C標準定義的函數。程序調用該函數退出時,程序直接返回內核。
3.
void _exit(int status)
該函數是POSIX.1.1標準定義的函數。程序調用該函數退出時,程序直接返回內核。
4. 在ISO C標準中,進程可以最多向exit函數註冊32個函數。即當程序調用exit時,該函數會去調用向它註冊的函數。完成這種註冊的函數如下:
int atexit(void (*func)(void))
程序的環境變量:
每個C程序在運行時,會被傳入一個全局變量environ。該變量申明如下:
extern char **environ