zoukankan      html  css  js  c++  java
  • C与汇编混合;C程序是怎么开始和结束的

    副标题:

         在汇编中使用Standard C Library。

         main函数的执行过程。

         main函数与Standard C Library的交互。

    image

    C程序是怎么开始和结束的:

    image

         在这里,一个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

         atexit(void (* func)(void)) 这个函数设定一个process的exit handler,一般来说最多可设定32个(根据具体系统有所不同)。exit() function执行全部的exit handler,按照设定时的反序执行。

         注意:函数之前用static修饰只表示这个函数只能在这个文件中被引用,不能在其他文件中引用。没有其他的意思。

                  我们在main()函数中调用的是return,它返回到C start-up routine, 由它执行exit()。

  • 相关阅读:
    Java—异常处理总结
    CSS white-space 属性
    兼容性问题的总结
    学习利用vertical-align:middle实现在整个页面居中
    多口USB HUB信号延长器 USBX-M200(针对于A客户使用时很棒吧)
    凌华运动控制卡调试记录
    记录:EPALN Electric P8 2.4.4.8366 安装记录
    一步一步创建一个动态库
    (转)VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径说明
    凌华运动控制卡的函数。
  • 原文地址:https://www.cnblogs.com/wangshuo/p/2038601.html
Copyright © 2011-2022 走看看