C与汇编混合;C程序是怎么开始和结束的
2011-05-06 10:32
344 查看
副标题:
在汇编中使用Standard C Library。
main函数的执行过程。
main函数与Standard C Library的交互。
C程序是怎么开始和结束的:
在这里,一个C程序就是一个process
Note: The only way a program is executed by the kernel is when one of the exec functions is called.
The only way a process voluntarily terminates is when _exit or _Exit is called, either explicitly or implecitly.
A process can also be involuntarily terminated by a signal (not shown in the previous figure).
A simple example:
执行结果:
atexit(void (* func)(void)) 这个函数设定一个process的exit handler,一般来说最多可设定32个(根据具体系统有所不同)。exit() function执行全部的exit handler,按照设定时的反序执行。
注意:函数之前用static修饰只表示这个函数只能在这个文件中被引用,不能在其他文件中引用。没有其他的意思。
我们在main()函数中调用的是return,它返回到C start-up routine, 由它执行exit()。
在汇编中使用Standard C Library。
main函数的执行过程。
main函数与Standard C Library的交互。
C程序是怎么开始和结束的:
在这里,一个C程序就是一个process
Note: The only way a program is executed by the kernel is when one of the exec functions is called.
The only way a process voluntarily terminates is when _exit or _Exit is called, either explicitly or implecitly.
A process can also be involuntarily terminated by a signal (not shown in the previous figure).
A simple example:
#include "apue.h" static void my_exit1(void); static void my_exit2(void); int main(void) { if (atexit(my_exit2) != 0) err_sys("can't register my_exit2"); if (atexit(my_exit1) != 0) err_sys("can't register my_exit1"); if (atexit(my_exit1) != 0) err_sys("can't register my_exit1"); printf("main is done\n"); return 0; /* the result is same as exit(0) */ /* but _exit(0) or _Exit(0) do not deal with exit handlers */ } static void my_exit1(void) { printf("fist exit handler\n"); } static void my_exit2(void) { printf("second exit handler\n"); }
执行结果:
main is done fist exit handler fist exit handler second exit handler |
注意:函数之前用static修饰只表示这个函数只能在这个文件中被引用,不能在其他文件中引用。没有其他的意思。
我们在main()函数中调用的是return,它返回到C start-up routine, 由它执行exit()。
相关文章推荐
- [VB.NET]父窗口怎么向子窗口传值?(刚开始做winform的程序)
- [编译环境][gcc]16位程序中汇编代码与C语言代码的混合编译
- win10周年更新后程序各种卡死,进程无法结束怎么破?
- 汇编语言: 从键盘上输入一串字符(用回车键结束,使用 10 号功能调用。)放在 STRING 中,试 编制一个程序测试字符串中是否存在数字。如有,则把 CL 的第 5 位置 1,否则将该位置置 0。
- python计算程序开始到程序结束的运行时间和程序运行的CPU时间
- 程序人生 - 结束,是另外一个阶段的开始
- Cocos2d-x 程序是如何开始运行与结束的
- Win32汇编--开始了解窗口程序
- 程序的开始和结束
- 汇编语言 编写程序,如果输入的是大写字母,则输出对应的小写字母;如果输入的是小写字母,则输出对应的大写字母;如果输入的是数字,原样输出;按回车结束
- 怎么把PID算法的汇编程序与温度控制系统的程序结合起来??
- Cocos2d-x 程序是如何开始运行与结束的
- python计算程序开始到程序结束的运行时间和程序运行的CPU时间
- 怎么开始写一个程序
- C程序与汇编程序混合编程
- 程序还没开始运行就马上结束
- cocos2d-x学习日志(18) --程序是如何开始运行与结束?
- 开始看汇编程序
- C语言里的EOF(结束程序)怎么用?
- [转]Cocos2d-X 程序是如何开始运行与结束的