• 嵌入式Linux C多进程编程(五)——进程退出和进程的等待


    一、进程退出(exit VS _exit)

    1.1 _exit的执行流程

    • 关闭进程打开的文件描述符、释放该进程持有的文件锁
    • 关闭该进程打开的信号量、消息队列
    • 取消该进程通过mmap()创建的内存映射
    • 将该进程的所有子进程交给nit托管
    • 给父进程发送一个SIGCHLD信号

    没有释放资源

    1.2 exit

    1.2.1 exit函数

    exit是对_exit进行封装
    在这里插入图片描述

    命令:echo $?,是对当前进程的返回值

    1.2.2 exit实际做的工作

    在这里插入图片描述
    比_exit多一个刷stdio流缓存区

    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char const *argv[], char **env)
    {
        printf("hello world");
        _exit(1);  //打印不出hello world
        //exit(1); //可以打印出hello world
        while(1);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1.3 小结

    正常退出
    main调用return
    异常退出
    任意地方调用_exit函数
    任意地方调用exit库函数
    被信号杀死
    调用abort函数

    两者区别在这里插入图片描述
    小结
    在这里插入图片描述

    1.4 atexit/on_exit

    退出处理程序
    在exit退出后可以自动执行用户注册的退出处理程序
    执行顺序与注册顺序相反
    函数原型: int atexit (void (funtion)void);
    函数原型: int on_exit (vold (*function)int,void *), void *arg);

    1.4.1 atexit

    #include 
    #include 
    #include 
    #include 
    
    void my_exit(void)
    {
        printf("my_exit\n");
    }
    
    int main(int argc, char const *argv[], char **env)
    {
        atexit(my_exit); //会在程序退出前调用my_exit函数
        printf("hello world");
        //_exit(1);  //打印不出hello world
        exit(1); //可以打印出hello world
        while(1);
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    1.4.2 on_exit

    #include 
    #include 
    #include 
    #include 
    
    void my_exit(void)
    {
        printf("my_exit\n");
    }
    
    void my_exit2(int ret, void *p)
    {
        printf("ret = %d\n", ret);
        printf("p = %s\n", (char *)p);
    }
    
    int main(int argc, char const *argv[], char **env)
    {
        //atexit(my_exit);
        on_exit(my_exit2, "hello");
        printf("hello world");
        //_exit(1);  //打印不出hello world
        exit(1); //可以打印出hello world
        while(1);
        return 0;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    二、进程等待

    2.1 回收进程资源

    进程运行终止后,不管进程是正常终止还是异常终止的,必须回收进程所古用的资源

    2.1.1 查看进程资源

    在这里插入图片描述

    2.1.2 为什么要回收进程的资源?

    当一个进程退出之后,进程能够回收自己的用户区的资源,但是不能回收内核空间的PCB资源,必须由父进程调用wait或者waitpid函数完成对子进程的回收,避免造成资源浪费。
    父进程运行结束时,会负责回收子进程资源。

    2.1.3 ./a.out进程的父进程是谁?

    0、1、 2三个进程:OS启动之后一 直默默运行的进程,直到关机OS结束运行

    2.1.3.1 0号进程

    做内部调度
    在这里插入图片描述

    2.1.3.2 1号进程

    1. 跟前端用户做调度
    2. 托孤进程
    3. 原始父进程
      在这里插入图片描述
      在这里插入图片描述

    2.1.3.3 2号进程

    在这里插入图片描述

    2.2 僵尸进程和孤儿进程

    2.2.1 僵尸进程

    父进程还没结束,子进程结束,子进程资源没被回收,变成僵尸进程,占用资源

    在这里插入图片描述

    2.2.2 孤儿进程

    父进程结束,子进程还没结束,会变成孤儿进程,会被托管到PID == 1的下面

    在这里插入图片描述

    2.3 wait和waitpid函数

    2.3.1 wait函数

    等待子进程运行终止
    在这里插入图片描述

    子进程返回状态
    在这里插入图片描述

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char const *argv[])
    {
        pid_t pid;
    
        pid = fork();
    
        if (pid < 0)
        {
            perror("fork error!\n");
            exit(1);
        }
        if (pid == 0)    //子进程
        {
            for (size_t i = 0; i < 3; i++)
            {
                printf("hello \n");
                sleep(1);
            }
            exit(3);
        }
        else if (pid > 0) //父进程
        {
            printf("father hello\n");
    
            int ret;
            wait(&ret);
    
            int num = WEXITSTATUS(ret);
    
            printf("子进程全部结束 = %d\n",num);
        }
    
        return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    2.3.2 waitpid函数

    在这里插入图片描述
    在这里插入图片描述

    三、总结

    谈谈你对进程的理解

    进程是对程序的统一抽象,因为任务调度,分配资源是按照进程分配的
    拥有独立的地址空间,进程的消亡不会对其他进程产生影响
    缺点:开销比较大
    进程调度:三态、调度算法
    进程创建

  • 相关阅读:
    排查SQLSERVER数据库慢-查询SQLSERVER数据库占用资源SQL语句
    【7 同步原语】
    深入浅出Java多线程(十一):AQS
    早在多久MySQL 啥时候用表锁,啥时候用行锁?
    数据治理建设管理方案(参考)(一)
    electron 起步
    中勒索病毒怎么处理想恢复数据
    HashSet和HashMap
    gRPC(一)入门:什么是RPC?
    知识图谱从入门到应用——知识图谱的获取与构建:知识工程与知识获取
  • 原文地址:https://blog.csdn.net/m0_52592798/article/details/125819230