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