一、程序的链接、加载和终止
- 链接器(给应用程序链接引导代码)
操作系统下的应用程序在main执行前也需要先执行段引导代码才能去执行main,但写应用程序时不用考虑引导代码的问题,编译连接时(准确说是链接时)由链接器将编译器中事先准备好的引导代码给链接进去,和应用程序一起构成最终的可执行程序。
- 加载器(把可执行程序加载到内存执行)
加载器是操作系统中的程序,当执行一个程序时(e.g., ./a.out,代码中用exec族函数来运行)加载器负责将这个程序加载到内存中去执行这个程序。
- 程序终止
- 正常终止
return、exit、_exit- 非正常终止
自已或他人发信号终止进程(e.g., ctrl + c )
- 进程终止处理注册函数
atexit()
- atexit()注册的终止处理函数会在运行return时运行。
#include <stdio.h>
#inlcude <stdlib.h>
void fun1(void)
{
printf("exit fun1!\n");
}
int main(int argc, char **argv)
{
printf("start!\n");
atexit(func1);
printf("end!");
return 0;
}
- 运行结果:
start!
end!
exit fun1!
- atexit注册多个进程终止函数时,先注册的后执行。(先进后出,和栈一样)
#include <stdio.h>
#include <stdlib.h>
//以下fun1,fun2都是进程终止函数
void fun1(void){
printf("exit fun1");
}
void fun2(void){
printf("exit fun2");
}
int main(int argc , char *args[]){
printf("start!");
atexit(fun1);
atexit(fun2);
return 0;
}
- 运行结果:
start!
exit fun2
exit fun1
- return和exit效果一样,都会执行进程终止函数;_exit不会执行进程终止函数,即刻终止进程。
#include <stdio.h>
#include <stdlib.h>
//以下fun1,fun2都是进程终止函数
void fun1(void){
printf("exit fun1");
}
void fun2(void){
printf("exit fun2");
}
int main(int argc , char *args[]){
printf("start!");
atexit(fun1);
atexit(fun2);
exit(0);
}
- 运行结果:
start!
exit fun2
exit fun1
#include <stdio.h>
#include <stdlib.h>
//以下fun1,fun2都是进程终止函数
void fun1(void){
printf("exit fun1");
}
void fun2(void){
printf("exit fun2");
}
int main(int argc , char *args[]){
printf("start!");
atexit(fun1);
atexit(fun2);
_exit(0);
}
- 运行结果:
start!
二、进程环境
- 环境变量
可以认为是操作系统的全局变量。
- 环境变量查看shell命令
export
- 进程环境表
每一个进程中都有一份所有环境变量构成的一个表格,也就是说当前进程中可以直接使用这些环境变量。进程环境表其实是一个字符串数组,用environ变量指向它。
写的程序可以无条件直接获得环境变量!
#include <stdio.h>
int main(int argc, char **argv)
{
extern char **environ