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

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

一、进程退出(exit VS _exit)


1.1 _exit的执行流程


关闭进程打开的文件描述符、释放该进程持有的文件锁

关闭该进程打开的信号量、消息队列

取消该进程通过mmap()创建的内存映射

将该进程的所有子进程交给nit托管

给父进程发送一个SIGCHLD信号

没有释放资源


1.2 exit


1.2.1 exit函数


exit是对_exit进行封装


0a2653c851af460fa595bd959398a8f1.png


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


1.2.2 exit实际做的工作


2d65d23f6d4748949b924e4057485923.png


比_exit多一个刷stdio流缓存区


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
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.3 小结


正常退出

main调用return

异常退出

任意地方调用_exit函数

任意地方调用exit库函数

被信号杀死

调用abort函数

0a2653c851af460fa595bd959398a8f1.png

两者区别

小结


2d65d23f6d4748949b924e4057485923.png


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 <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
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.4.2 on_exit


#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
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;
}

0a2653c851af460fa595bd959398a8f1.png


二、进程等待


2.1 回收进程资源


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


2.1.1 查看进程资源


2d65d23f6d4748949b924e4057485923.png


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


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

父进程运行结束时,会负责回收子进程资源。


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


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


2.1.3.1 0号进程


做内部调度


0a2653c851af460fa595bd959398a8f1.png


2.1.3.2 1号进程


跟前端用户做调度

托孤进程

原始父进程


2d65d23f6d4748949b924e4057485923.png


2.1.3.3 2号进程


0a2653c851af460fa595bd959398a8f1.png


2.2 僵尸进程和孤儿进程


2.2.1 僵尸进程


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


2d65d23f6d4748949b924e4057485923.png


2.2.2 孤儿进程


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


2e9b90b2ca334476abebe75bafe6eeaa.png


2.3 wait和waitpid函数


2.3.1 wait函数


等待子进程运行终止


0a2653c851af460fa595bd959398a8f1.png


子进程返回状态


2d65d23f6d4748949b924e4057485923.png

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
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;
}


2.3.2 waitpid函数


0a2653c851af460fa595bd959398a8f1.png


三、总结


谈谈你对进程的理解


进程是对程序的统一抽象,因为任务调度,分配资源是按照进程分配的

拥有独立的地址空间,进程的消亡不会对其他进程产生影响

缺点:开销比较大

进程调度:三态、调度算法

进程创建


相关文章
|
5天前
|
存储 Linux Shell
Linux:进程等待 & 进程替换
Linux:进程等待 & 进程替换
29 9
|
5天前
|
存储 Linux C语言
Linux:进程创建 & 进程终止
Linux:进程创建 & 进程终止
24 6
|
3天前
|
Unix Linux 调度
linux线程与进程的区别及线程的优势
linux线程与进程的区别及线程的优势
|
4天前
|
存储 安全 Linux
【Linux】详解进程通信中信号量的本质&&同步和互斥的概念&&临界资源和临界区的概念
【Linux】详解进程通信中信号量的本质&&同步和互斥的概念&&临界资源和临界区的概念
|
4天前
|
Linux C++
【Linux】详解进程程序替换
【Linux】详解进程程序替换
|
4天前
|
API 开发工具 Windows
LabVIEW中编程更改进程的优先级
LabVIEW中编程更改进程的优先级
|
5天前
|
存储 Linux Shell
Linux:进程概念
Linux:进程概念
19 8
|
5天前
|
算法 Linux 调度
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程
xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程
10 1
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程
|
5天前
|
Linux 调度 数据库
|
5天前
|
存储 缓存 Linux
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(三)--实时与非实时数据交互
本文介绍了Xenomai中的XDDP(Xenomai Distributed Data Protocol)通信机制,XDDP用于实时和非实时进程之间的数据交换。XDDP在Xenomai内核中涉及的数据结构和管理方式,以及创建XDDP通道后的实时端和非实时端连接过程。
9 0
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(三)--实时与非实时数据交互