Linux进程相关函数(二)—回收子进程

我们先来了解两个概念,什么是孤儿进程,以及什么是僵尸进程?

孤儿进程

        父进程先于子进程结束,此时子进程将将变成孤儿进程,孤儿进程会被init进程收留,init进程。

僵尸进程

        子进程结束,父进程尚未回收子进程的状态,此时子进程将变成僵尸进程,其PCB进程块会残留在内核当中。

目录

回收子进程退出信息

DEMO


回收子进程退出信息

  —wait函数

              pid_t wait(int *wstatus);

              pid_t waitpid(pid_t pid, int *wstatus, int options);

              wstatus—传入传输参数,其可以获取子进程退出码,但需要将其传入特定宏解析

              pid—

                      pid > 0  要回收子进程的ID

                      pid = -1 回收任意子进程

                      pid = 0  回收和当前调用同组的子进程

                      pid < -1 回收指定组内任意子进程

              options—waitpid函数行为选项

                     WNOHANG非阻塞,立即返回,没有子进程退出返回0。

              返回值

                     wait()

                               成功返回当前终止进程的PID

                               失败返回 -1 

                     waitpid()

                                返回值大于零——当前终止进程的PID

                                返回0——options指定了 WNOHANG,且当前没有子进程退出

                                失败返回 -1 并设置 error 

int *wastatus 保存子进程的推出信息,其需将其获取到的值传入对应的宏函数才能解析出推出信息。

  • WIFEXITED(wstatus) — 如果子进程正常退出,此宏函数为真,则可以获取子进程的退出状态
  • WEXITSTATUS(wstatus) — 获取子进程正常退出的退出状态
  • WIFSIGNALED(wstatus) — 如果子进程被信号杀死,此宏函数为真,则可以获取子进程的退出状态
  • WTERMSIG(wstatus) — 获取被信号终止的子进程退出状态

DEMO

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

int main(int argc,char **argv)
{

        pid_t pid;      //process ID
        int status;
        int wpid;
        printf("program exection!\n");  //the current program exection normally

        printf("==============================================\n");

        pid = fork();   //creat child process
        if(pid == -1){
                perror("fork error");   //creat failed
                exit(0);
        }else if(pid > 0){

                wpid =  wait(&status);  //reclaim child process exit informations
                //wpid =  waitpid(-1,&status,0);  //reclaim child process exit informations
                if(wpid == -1){

                        printf("wait functions execution error");//wait error
                        exit(0);
                }
                printf("This is father process!\n");    //father porcess

                if(WIFEXITED(status)){    //表示子进程正常退出

                        printf("The child exit normally! my child pid = %d exit status = %d\n",wpid,WEXITSTATUS(status)); //normally exit
                }
                if(WIFSIGNALED(status)){    //表示子进程被信号杀死

                        printf("The child is signal terminted! my child pid = %d singal status = %d\n",wpid,WTERMSIG(status)); //signal terminted       
                }

        }else if(pid == 0){

                printf("This is child process\n");  //child process
                printf("hello I will exit\n");  //
                sleep(10); //sleep 10 second
                exit(3);
        }

        return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值