APUE 学习笔记(五) 进程环境
1.main函数
C程序总是从main函数开始执行,当内核执行C程序时,在调用main函数之前先调用exec函数从内核获取命令行参数和环境变量值
2.进程终止
正常终止:
(1)在main函数内执行return语句
(2)调用exit
(3)最后一个线程从其启动例程返回
(4)最后一个线程调用pthread_exit
异常中止:
(1)调用abort
(2)接收到一个信号并终止(内存越界或除0)
(3)最后一个线程对取消请求作出响应
内核使程序执行的唯一方法就是调用exec函数,进程自愿终止的唯一方法就是调用exit
3.C程序的存储空间布局
a.out中还有其他类型的段,比如 符号表段、调试信息段、动态共享库段
size命令查看 正文段、数据段和bss(未初始化数据段)的长度。 如 size /usr/bin/cc
4. setjmp longjmp
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <setjmp.h> static jmp_buf jmpbuf; static int globval = 1; static void func2(void) { longjmp(jmpbuf, 1); } static void func1(int i, int j, int k, int l) { fprintf(stdout, "in func1:\n"); fprintf(stdout, "globval = %d," "autoval = %d," "regival = %d," "volaval = %d," "statval = %d\n", globval, i, j, k , l); func2(); } int main(int argc, char* argv[]) { int autoval = 2; register int regival = 3; volatile int volaval = 4; static int statval = 5; if (setjmp(jmpbuf) != 0) { fprintf(stdout, "after longjmp:\n"); fprintf(stdout, "globval = %d," "autoval = %d," "regival = %d," "volaval = %d," "statval = %d\n", globval, autoval, regival, volaval, statval); exit(0); } globval = 95; autoval = 96; regival = 97; volaval = 98; statval = 99; func1(autoval, regival, volaval, statval); exit(0); }
编译时不进行优化,结果如下:
in func1: globval = 95,autoval = 96,regival = 97,volaval = 98,statval = 99 after longjmp: globval = 95,autoval = 96,regival = 97,volaval = 98,statval = 99
编译时进行全部优化,结果如下:
in func1: globval = 95,autoval = 96,regival = 97,volaval = 98,statval = 99 after longjmp: globval = 95,autoval = 2,regival = 3,volaval = 98,statval = 99
我们可以看到:全局、静态、易失变量不受优化的影响,在调用longjmp之后,他们的值是最近所呈现的值
未优化时,所有5个变量都存放在存储器中,而进行优化之后,autoval 和 regival 都存放在寄存器中,而 volatile变量仍存放在 存储器中
对于 volatile 修饰符用法,这里有一篇很好的文章:http://hedengcheng.com/?p=725