Linux系统编程之回收子进程

孤儿进程

在这里插入图片描述

/*
 *  @file           orphan.c
 *  @brief          父进程比子进程先结束,子进程变成了孤儿进程,由init进程回收
 *  @version 1.1    无
 *  @author         北豼
 *  @date           2022年5月9日
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

int main(void)
{
        pid_t pid;

        pid = fork();
        if (pid == 0)           //子进程
        {
                while(1)        //循环打印让父进程先结束,子进程变成孤儿进程
                {
                        printf("I am child,my parent pid = %d\n", getppid());
                        sleep(1);
                }
        }
        else if (pid > 0)       //父进程
        {
                printf("I am parent,my pid is %d\n", getpid());
                sleep(5);
                printf("-------parent going to die---------\n");
        }
        else if (pid == -1)     //创建子进程失败
        {
                perror("fork error");
                exit(1);
        }

        return 0;
}


刚开始的父进程是11872,后来父进程11872先结束了,子进程就变成孤儿进程了,父进程变成1了,即是init进程,由这个init进程进行子进程的回收
在这里插入图片描述

僵尸进程

在这里插入图片描述

/*
 *  @file           zoom.c
 *  @brief          子进程结束了,父进程也没有回收回收残留的资源,就变成僵尸进程了
 *  @version 1.1    无
 *  @author         北豼
 *  @date           2022年5月9日
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void)
{
        pid_t pid;

        pid = fork();
        if (pid == 0)           //子进程
        {
                printf("I am child, my parent %d, going to sleep 10s\n", getppid());
                sleep(10);
                printf("-----------child die-------------\n");
        }
        else if (pid > 0)       //父进程
        {
                while(1)        //子进程结束后,父进程并没有对子进程进行回收,导致子进程变成僵尸进程
                {
                        printf("I am parent, pid = %d,myson = %d\n", getpid(), pid);
                        sleep(1);
                }
        }
        else if (pid == -1)     //子进程创建失败
        {
                perror("fork error");
                exit(1);
        }

        return 0;
}


子进程结束了,父进程也没有回收回收残留的资源,就变成僵尸进程了在这里插入图片描述
这里看到的括号括起来的zoom就是僵尸态了在这里插入图片描述

wait函数

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

在这里插入图片描述
wait函数

在这里插入图片描述

wait函数阻塞等待子进程结束,再由父进程粗略回收,但不知道子进程结束的状态

/*
 *  @file           zoom_wait.c
 *  @brief          用wait函数阻塞等待子进程结束,再由父进程粗略回收,但不知道子进程结束的状态
 *  @version 1.1    无
 *  @author         北豼
 *  @date           2022年5月9日
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void)
{
        pid_t pid;
        pid_t wpid;

        //创建一个子进程
        pid = fork();
        if (pid == 0)           //子进程
        {
                printf("I am child, my parent %d, going to sleep 10s\n", getppid());
                sleep(10);
                printf("-----------child die-------------\n");
        }
        else if (pid > 0)       //父进程
        {
                wpid = wait(NULL);              //阻塞等待子进程结束,回收子进程
                if(wpid == -1)                  //回收子进程失败
                {
                        perror("wait error");
                        exit(1);
                }
                while(1)                        //这里子进程已经被回收,弄一个while,方便去查看是否还有僵尸进程
                {
                        printf("I am parent, pid = %d,myson = %d\n", getpid(), pid);
                        sleep(1);
                }
        }
        else if (pid == -1)     //创建子进程失败
        {
                perror("fork error");
                exit(1);
        }

        return 0;
}

在这里插入图片描述
这样子父进程就可以回收到子进程了,就不会留下子进程的残留了
在这里插入图片描述

wait函数阻塞等待子进程结束,再执行父进程,这里精确回收,得到了子进程结束的状态,子进程正常结束

/*
 *  @file           wait1.c
 *  @brief          用wait函数阻塞等待子进程结束,再执行父进程,这里精确回收,得到了子进程结束的状态,子进程正常结束
 *  @version 1.1    无
 *  @author         北豼
 *  @date           2022年5月9日
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void)
{
        pid_t pid;
        pid_t wpid;
        int status;

        //创建一个子进程
        pid = fork();
        if (pid == 0)           //子进程
        {
                printf("I am child, my parent %d, going to sleep 10s\n", getppid());
                sleep(10);
                printf("-----------child die-------------\n");
                exit(76);       //进程正常结束
        }
        else if (pid > 0)       //父进程
        {
                wpid = wait(&status);           //阻塞等待子进程结束,回收子进程
                if (wpid == -1)                 //回收子进程失败
                {
                        perror("wait error");
                        exit(1);
                }
                if (WIFEXITED(status))          //子进程正常结束
                {
                        printf("child exit with %d\n", WEXITSTATUS(status));            //打印输出exit的参数
                }
                while(1)        //这里子进程已经被回收,弄一个while,方便去查看是否还有僵尸进程
                {
                        printf("I am parent, pid = %d,myson = %d\n", getpid(), pid);
                        sleep(1);
                }
        }
        else if (pid == -1)     //创建子进程失败
        {
                perror("fork error");
                exit(1);
        }

        return 0;
}

在这里插入图片描述
wait函数阻塞等待子进程结束,再执行父进程,这里精确回收,得到了子进程结束的状态,子进程异常结束

/*
 *  @file           wait2.c
 *  @brief          用wait函数阻塞等待子进程结束,再执行父进程,这里精确回收,得到了子进程结束的状态,子进程异常结束
 *  @version 1.1    无
 *  @author         北豼
 *  @date           2022年5月9日
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void)
{
        pid_t pid;
        pid_t wpid;
        int status;

      	//创建一个子进程
        pid = fork();
        if (pid == 0)           //子进程
        {
                printf("I am child, my parent %d, going to sleep 10s\n", getppid());
                sleep(60);              //这里阻塞60s,留着去kill子进程
                printf("-----------child die-------------\n");
                exit(76);
        }
        else if (pid > 0)       //父进程
        {
                wpid = wait(&status);           //阻塞等待子进程结束,回收子进程
                if (wpid == -1)                 //回收子进程失败
                {
                        perror("wait error");
                        exit(1);
                }
                if (WIFEXITED(status))          //子进程正常结束
                {
                        printf("child exit with %d\n", WEXITSTATUS(status));
                }
                if (WIFSIGNALED(status))        //子进程异常结束
                {
                        printf("child killed by %d\n", WTERMSIG(status));
                }
                while (1)
                {
                        printf("I am parent, pid = %d,myson = %d\n", getpid(), pid);
                        sleep(1);
                }
        }
        else if (pid == -1)     //创建子进程失败
        {
                perror("fork error");
                exit(1);
        }

        return 0;
}


这里用kill杀死子进程
在这里插入图片描述
wait函数得到了子进程结束的状态
在这里插入图片描述
在这里插入图片描述
回收多个子进程
上面已经用过wait函数来回收一个子进程,现在用wait函数来回收多个子进程

/*
 *  @file           wait3.c
 *  @brief          用wait函数来回收多个子进程
 *  @version 1.1    无
 *  @author         北豼
 *  @date           2022年5月9日
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(void)
{
        int i;
        pid_t pid;

        //创建5个子进程
        for (i = 0; i < 5; i++)
        {
                pid = fork();
                if (pid == 0)           //父进程继续循环,直到循环结束,子进程退出循环
                {
                        break;
                }
        }

        //判断是父进程还是子进程
        if(i < 5)
        {
                sleep(i);
                printf("I am %d child, pid = %u\n", i+1, getpid());
        }
        else
        {
                sleep(i);
                printf("I am parent\n");
                while (wait(NULL));             //循环回收多个子进程
        }

        return 0;
}

在这里插入图片描述

waitpid函数

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

waitpid函数回收指定子进程
waitpid函数的第三个参数为0是阻塞回收,跟wait函数一样

/*
 *  @file           waitpid1.c
 *  @brief          用waitpid函数回收指定子进程
 *  @version 1.1    无
 *  @author         北豼
 *  @date           2022年5月9日
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(void)
{
        int i;
        pid_t pid;
        pid_t p;

        //循环创建5个子进程
        for (i = 0; i < 5; i++)
        {
                pid = fork();
                if (pid == 0)
                {
                        break;
                }
                else if (i == 3)
                {
                        p = pid;
                }
        }

        if (i < 5)
        {
                sleep(i);
                printf("I am %d child, pid = %u\n", i+1, getpid());
        }
        else
        {
                sleep(i);
                printf("I am parent\n");
                waitpid(p, NULL, 0);            //阻塞等待回收指定的子进程
                while (1);
        }

        return 0;
}

可以看出指定回收的第四个子进程已经被回收了
在这里插入图片描述
waitpid函数回收多个子进程

/*
 *  @file           waitpid2.c
 *  @brief          用waitpid函数回收多个子进程
 *  @version 1.1    无
 *  @author         北豼
 *  @date           2022年5月9日
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(void)
{
        int i;
        pid_t pid;

        //循环创建5个子进程
        for (i = 0; i < 5; i++)
        {
                pid = fork();
                if (pid == 0)
                {
                        break;
                }
        }

        if (i < 5)
        {
                sleep(i);
                printf("I am %d child, pid = %u\n", i+1, getpid());
        }
        else
        {
                sleep(i);
                printf("I am parent\n");
                while (waitpid(-1, NULL, 0));           //等价于while(wait(NULL));
        }

        return 0;
}

可以看到所有子进程都被回收了
在这里插入图片描述
waitpid函数的第三个参数为WNOHANG,为轮询回收子进程,子进程正在运行的时候,返回值为0
轮询回收多个子进程

/*
 *  @file           waitpid3.c
 *  @brief          用waitpid函数轮询回收多个子进程
 *  @version 1.1    无
 *  @author         北豼
 *  @date           2022年5月9日
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(void)
{
        int i;
        int n = 5;
        pid_t pid;
        pid_t wpid;

        //循环创建5个子进程
        for (i = 0; i < 5; i++)
        {
                pid = fork();
                if (pid == 0)
                {
                        break;
                }
        }

        if (i < 5)
        {
                sleep(i);
                printf("I am %d child, pid = %u\n", i+1, getpid());
        }
        else
        {
                sleep(i);
                printf("I am parent\n");
                do
                {
                        wpid = waitpid(-1, NULL, WNOHANG);              //轮询回收子进程
                        if (wpid > 0)
                        {
                                n--;
                        }
                        sleep(1);
                }while (n > 0);

                while (1);
        }

        return 0;
}

子进程已全部回收
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
线程概念 什么是线程 LWP:light weight process 轻量级的进程,本质仍是进程(在Linux环境下) 进程:独立地址空间,拥有PCB 线程:也有PCB,但没有独立的地址空间(共享) 区别:在于是否共享地址空间。 独居(进程);合租(线程)。 Linux下: 线程:最小的执行单位 进程:最小分配资源单位,可看成是只有一个线程的进程。 Linux内核线程实现原理 类Unix系统中,早期是没有“线程”概念的,80年代才引入,借助进程机制实现出了线程的概念。因此在这类系统中,进程和线程关系密切。 1. 轻量级进程(light-weight process),也有PCB,创建线程使用的底层函数和进程一样,都是clone 2. 从内核里看进程和线程是一样的,都有各自不同的PCB,但是PCB中指向内存资源的三级页表是相同的 3. 进程可以蜕变成线程 4. 线程可看做寄存器和栈的集合 5. 在linux下,线程最是小的执行单位;进程是最小的分配资源单位 察看LWP号:ps –Lf pid 查看指定线程的lwp号。 三级映射:进程PCB --> 页目录(可看成数组,首地址位于PCB中) --> 页表 --> 物理页面 --> 内存单元 参考:《Linux内核源代码情景分析》 ----毛德操 对于进程来说,相同的地址(同一个虚拟地址)在不同的进程中,反复使用而不冲突。原因是他们虽虚拟址一样,但,页目录、页表、物理页面各不相同。相同的虚拟址,映射到不同的物理页面内存单元,最终访问不同的物理页面。 但!线程不同!两个线程具有各自独立的PCB,但共享同一个页目录,也就共享同一个页表和物理页面。所以两个PCB共享一个地址空间。 实际上,无论是创建进程的fork,还是创建线程的pthread_create,底层实现都是调用同一个内核函数clone。 如果复制对方的地址空间,那么就产出一个“进程”;如果共享对方的地址空间,就产生一个“线程”。 因此:Linux内核是不区分进程和线程的。只在用户层面上进行区分。所以,线程所有操作函数 pthread_* 是库函数,而非系统调用。 线程共享资源 1.文件描述符表 2.每种信号的处理方式 3.当前工作目录 4.用户ID和组ID 5.内存地址空间 (.text/.data/.bss/heap/共享库) 线程非共享资源 1.线程id 2.处理器现场和栈指针(内核栈) 3.独立的栈空间(用户空间栈) 4.errno变量 5.信号屏蔽字 6.调度优先级 线程优、缺点 优点: 1. 提高程序并发性 2. 开销小 3. 数据通信、共享数据方便 缺点: 1. 库函数,不稳定 2. 调试、编写困难、gdb不支持 3. 对信号支持不好 优点相对突出,缺点均不是硬伤。Linux下由于实现方法导致进程、线程差别不是很大。 线程控制原语 pthread_self函数 获取线程ID。其作用对应进程中 getpid() 函数。 pthread_t pthread_self(void); 返回值:成功:0; 失败:无! 线程ID:pthread_t类型,本质:在Linux下为无符号整数(%lu),其他系统中可能是结构体实现 线程ID是进程内部,识别标志。(两个进程间,线程ID允许相同) 注意:不应使用全局变量 pthread_t tid,在子线程中通过pthread_create传出参数来获取线程ID,而应使用pthread_self。 pthread_create函数 创建一个新线程。 其作用,对应进程中fork() 函数。 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); 返回值:成功:0; 失败:错误号 -----Linux环境下,所有线程特点,失败均直接返回错误号。 参数: pthread_t:当前Linux中可理解为:typedef unsigned long int pthread_t; 参数1:传出参数,保存系统为我们分配好的线程ID 参数2:通常传NULL,表示使用线程默认属性。若想使用具体属性也可以修改该参数。 参数3:函数指针,指向线程主函数(线程体),该函数运行结束,则线程结束。 参数4:线程主函数执行期间所使用的参数。 在一个线程中调用pthread_create()创建新的线程后,当前线程从pthread_create()返回继续往下执行,而新的线程所执行的代码由我们传给pthread_create的函数指针start_routine决定。star

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是北豼不太皮吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值