wait函数() waitpid函数()

wait函数

复制代码
/*
    #include <sys/types.h>
    #include <sys/wait.h>
    pid_t wait(int *wstatus);
    功能:等待任意一个子进程结束,如果子进程结束或者中断了,那么就回收他的资源
    参数: wstatus
        进程退出时的状态信息,传入的是一个int类型的地址,传出参数
    返回值:
        成功:返回了被回收的子进程的ID
        失败:-1(所有的子进程结束了,调用函数失败了)

    调用wait函数的进程会被挂起(阻塞),知道他的一个子进程退出或者收到一个不能被忽略的信号才被唤醒
    如果没有子进程,函数立刻返回-1,如果子进程全部结束,也返回-1
*/
#include<sys/wait.h>
#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
int main() {
    pid_t pid;
    for(int i = 0; i < 5; i++) {
        pid = fork();
        if(pid == 0) {
            break;
        }
    }

    if(pid > 0) {
        while(1) {
            printf("父, pid = %d\n", getpid());
            int ret = wait(NULL);
            printf("child die, pid = %d", ret);
            sleep(1); 
        }            
    } 
    else if(pid == 0) {
        while(1) {
            printf("儿, pid = %d \n", getpid());
            sleep(1);
        }
        
    }
}
bash 复制代码
#include<sys/wait.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main() {
    pid_t pid;
    for(int i = 0; i < 5; i++) {
        pid = fork();
        if(pid == 0) {
            break;
        }
    }

    if(pid > 0) {
        while(1) {
            printf("父, pid = %d\n", getpid());
            // int ret = wait(NULL);
            int st;
            int ret = wait(&st);
            if(ret == -1) break;
            if(WIFEXITED(st)) {
                //是不是正常退出
                printf("退出的状态码:%d\n", WEXITSTATUS(st));
            }
            if(WIFSIGNALED(st)) {
                //是不是正常退出
                printf("被那个信号干掉了:%d\n", WTERMSIG(st));
            }
            
            printf("child die, pid = %d", ret);
            sleep(1); 
        }            
    } 
    else if(pid == 0) {
        //while(1) {
            printf("儿, pid = %d \n", getpid());
            sleep(1);
        //}
        exit(0);
    }
}

因为exit(0)所以退出的状态码0 如果exit(2)那么就是2

如果通过kill -9 杀死进程 那么宏WIFSIGNALED就生效 输出那句话

waitpid函数

cpp 复制代码
/*
    #include <sys/types.h>
    #include <sys/wait.h>
    pid_t wait(int *wstatus);
    pid_t waitpid(pid_t pid, int *wstatus, int options);
    功能:等待任意一个子进程结束,如果子进程结束或者中断了,那么就回收他的资源
    参数: 
        pid:
            pid > 0:某个子进程的pid
            pid = 0:回收当前进程组的所有子进程
            pid = -1:回收所有的子进程 相当于wait()
            pid < -1: 某个进程组的组id的绝对值,回收指定进程组中的子进程
        wstatus
        进程退出时的状态信息,传入的是一个int类型的地址,传出参数
        options:
            0:表示阻塞的
            WNOHANG:非阻塞
    返回值:
        > 0:返回了子进程的ID
        = 0:options = WNOHANG, 表示还有子进程活着
        = -1;表示错误或者没有子进程

    调用wait函数的进程会被挂起(阻塞),知道他的一个子进程退出或者收到一个不能被忽略的信号才被唤醒
    如果没有子进程,函数立刻返回-1,如果子进程全部结束,也返回-1
*/
#include<sys/wait.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main() {
    pid_t pid;
    for(int i = 0; i < 5; i++) {
        pid = fork();
        if(pid == 0) {
            break;
        }
    }

    if(pid > 0) {
        while(1) {
            sleep(1); 
            printf("父, pid = %d\n", getpid());
            // int ret = wait(NULL);
            int st;
            int ret = waitpid(-1, &st, WNOHANG);
            if(ret == -1) break;
            else if(ret == 0) {continue;}
            else if(ret > 0){
                if(WIFEXITED(st)) {
                    //是不是正常退出
                    printf("退出的状态码:%d\n", WEXITSTATUS(st));
                }
                if(WIFSIGNALED(st)) {
                    //是不是正常退出
                    printf("被那个信号干掉了:%d\n", WTERMSIG(st));
                }
            
                printf("child die, pid = %d", ret);
            }     
        }            
    } 
    else if(pid == 0) {
        while(1) {
            printf("儿, pid = %d \n", getpid());
            sleep(1);
        }
        exit(0);
    }
}
相关推荐
侯小啾33 分钟前
【03】C语言 强制类型转换 与 进制转换
c语言·数据结构·算法
Sylvia@8881 小时前
18.SELInux安全性
linux·运维·服务器
Xの哲學1 小时前
Linux NAPI 架构详解
linux·网络·算法·架构·边缘计算
碼不停提1 小时前
linux 内核架构
linux
gplitems1232 小时前
Technox – IT Solutions & Services WordPress Theme: A Practical
linux·服务器·数据库
Deamon Tree3 小时前
后端开发常用Linux命令
linux·运维·python
key_Go4 小时前
0.基于Centos-Stream8 搭建Openstack环境
linux·运维·centos·openstack
wacpguo4 小时前
centos 配置网络
linux·网络·centos
京东零售技术4 小时前
扛起技术大梁的零售校招生们 | 1024技术人特别篇
算法
feng_blog66885 小时前
Linux桌面X11服务-XRecord方案捕获鼠标点击的应用窗口
linux