zoukankan      html  css  js  c++  java
  • 系统编程-进程-进程终止(或称为退出或结束)

     

    1. atexit函数: 用于登记用户自定义的进程终止函数,这样会覆盖系统默认提供的标准终止函数

    我们自定义的进程终止函数并非在所有进程终止的情况下都会被调用,是否会被调用与进程的终止方式有关。

    下图展示了不同的进程终止方式的区别:

    _exit或者_EXIT不会负责调用进程终止函数,也不会刷新缓存。而exit和关键字return会调用进程终止函数,也会刷新缓存。

    2. 进程的启动和退出方式 框图详解

    上图中虽然没有提return, 补充下: 使用return和exit的效果是一样的。

    3. 实践出真知

    实验1

    #include <stdio.h>
    #include <stdlib.h>
    
    void term_func1(void){
        printf("term_func1 
    ");
    }
    void term_func2(void){
        printf("term_func2 
    ");
    }
    void term_func3(void){
        printf("term_func3 
    ");
    }
    
    int main(){
      
       atexit(term_func1);
       atexit(term_func2);
       atexit(term_func3);
    
      return 0;
    }

    编译运行 : 

    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/fork/进程终止# gcc process_end.c
    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/fork/进程终止# ./a.out term_func3
    term_func2
    term_func1
    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/fork/进程终止#

    结论:   登记的多个进程终止函数执行顺序是以栈的方式执行,先登记的后执行。

     

    实验2

    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    void term_func1(void){
        printf("term_func1 
    ");
    }
    void term_func2(void){
        printf("term_func2 
    ");
    }
    void term_func3(void){
        printf("term_func3 
    ");
    }
    
    int main(int argc, char**argv){
      
       atexit(term_func1);
       atexit(term_func2);
       atexit(term_func3);
    
       FILE* fp = fopen("file.txt", "wb+");
       // 对于wb+,如果文件不存在则会创建,如果文件存在则会覆盖写入。
       fprintf(fp, "hello this is a test");
    
       if(!strcmp(argv[1], "exit"))
        exit(0);
       else if(!strcmp(argv[1], "_exit"))
        _exit(0);
       else if(!strcmp(argv[1], "_Exit"))
        _Exit(0);
       else if(!strcmp(argv[1], "return"))
        return 0;
       else
        printf("your input Error! 
    ");
      return 0;
    }

    编译运行 : 

    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止# ./a.out exit
    term_func3
    term_func2
    term_func1
    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止# cat file.txt
    hello this is a testroot@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止#
    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止#


    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止# ./a.out return
    term_func3
    term_func2
    term_func1
    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止#
    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止# cat file.txt
    hello this is a testroot@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止#


    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止# ./a.out _exit
    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止#
    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止# cat file.txt
    root@lmw-virtual-machine:/home/lmw/MINE/Linux_C_exercise/for
    k/进程终止#

    结论:  本实验使用到的fprintf是标准C库里的进行IO操作的函数,是带缓存的,这里不是写标准输入或输出(行缓存),也不是标准错误(无缓存),而是写普通的文件,是全缓存方式。

    全缓存方式,刷新缓存的时机是:

    1,调用fclose    2. 调用刷新缓存函数fflush sync等     3. 缓存满了 

    4. 程序退出, 但是对程序退出的方式有要求, 即:

    程序退出时使用_exit或者_EXIT不会负责调用进程终止函数,也不会刷新缓存。而使用exit和关键字return会调用进程终止函数,同时也会刷新缓存。

    .

    /************* 社会的有色眼光是:博士生、研究生、本科生、车间工人; 重点大学高材生、普通院校、二流院校、野鸡大学; 年薪百万、五十万、五万; 这些都只是帽子,可以失败千百次,但我和社会都觉得,人只要成功一次,就能换一顶帽子,只是社会看不见你之前的失败的帽子。 当然,换帽子决不是最终目的,走好自己的路就行。 杭州.大话西游 *******/
  • 相关阅读:
    CentOS 7配置LNMP开发环境及配置文件管理
    Virtual Box配置CentOS7网络(图文教程)
    HTTP常用状态码分析
    MySQL数据库和InnoDB存储引擎文件
    InnoDB关键特性学习笔记
    InnoDB体系结构学习笔记
    Laravel 5.x 请求的生命周期(附源码)
    PyQt5 signal and slot
    x01.gamelab: An Tank 3D Model
    x01.AntWorld: An Python AI Game
  • 原文地址:https://www.cnblogs.com/happybirthdaytoyou/p/14301052.html
Copyright © 2011-2022 走看看