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()。

  • 相关阅读:
    Oracle 不走索引
    Oracle不等值链接
    查看统计信息是否过期
    JavaScript利用append添加元素报错
    Subversion Native Library Not Available & Incompatible JavaHL library loaded
    Oracle并行查询出错
    Oracle连接出错(一)
    Linux下Subclipse的JavaHL
    Java生成文件夹
    Java生成文件
  • 原文地址:https://www.cnblogs.com/wangshuo/p/2038601.html
Copyright © 2011-2022 走看看