Linux多进程07-wait和waitpid

进程回收

  • 在每个进程退出的时候,内核释放该进程所有的资源、包括打开的文件、占用的内存等。但是仍然为其保留一定的信息,这些信息主要主要指进程控制块PCB的信息(包括进程号、退出状态、运行时间等)。

  • 父进程可以通过调用wait或waitpid得到它的退出状态同时彻底清除掉这个进程。

  • wait() 和 waitpid() 函数的功能一样,区别在于,wait() 函数会阻塞,waitpid() 可以设置不阻塞,waitpid() 还可以指定等待哪个子进程结束。

  • 一次wait或waitpid调用只能清理一个子进程,清理多个子进程应使用循环。

wait

#include <sys/types.h>
#include <sys/wait.h>

pid_t wait(int *wstatus);
    功能: 等待任意一个子进程结束,如果任意一个子进程结束了,次函数会回收子进程的资源

    参数:
        wstatus :进程退出的状态信息, 传入的是一个int类型的地址,传出参数
    返回:
        - 成功: 返回被回收的子进程的ID
        - 失败: 返回-1 (所有的子进程都结束)

    调用wait函数的进程会被挂起(阻塞),
    直到它的一个子进程退出或者收到一个不能被忽略的信号时才被唤醒,相当于继续往下执行
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char const *argv[])
{
    //有个父进程,创建5个子进程
    pid_t pid;
    for (int i = 0; i < 5; i++)
    {
        pid = fork();
        if (pid == 0)
            break; //防止子进程产生孙子进程
    }

    if (pid > 0)
    {
        //父进程
        while (1)
        {
            printf("parent process, pid = %d\n", getpid());

            int ret = wait(NULL); //阻塞等待
            if(ret == -1){
                break;
            }
            printf("child process die, pid = %d\n", ret);

            sleep(1);
        }
    }
    else if (pid == 0)
    {
        //子进程
        while (1)
        {
            printf("child process, pid = %d\n", getpid());
            sleep(1);
        }
    }
    return 0;
}

waitpid

#include <sys/types.h>
#include <sys/wait.h>

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 错误,或者没有子进程了
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    pid_t pid;
    for (int i = 0; i < 5; i++)
    {
        pid = fork();
        if (pid == 0)
            break;
    }

    if (pid > 0)
    {
        while (1)
        {
            printf("parent, pid = %d\n", getpid());
            sleep(1);

            int st;
            // int ret = waitpid(-1,&st,0); //阻塞
            int ret = waitpid(-1, &st, WNOHANG); //非阻塞

            if (ret == -1)
                break;

            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 process die, pid = %d\n", ret);
            }
                }
    }
    else if (pid == 0)
    {
        while (1)
        {
            printf("child, pid = %d\n", getpid());
            sleep(1);
        }
        exit(0);
    }

    return 0;
}

退出信息相关宏函数

  • WIFEXITED(status) 非0,进程正常退出
  • WEXITSTATUS(status) 如果上宏为真,获取进程退出的状态(exit的参数)
  • WIFSIGNALED(status) 非0,进程异常终止
  • WTERMSIG(status) 如果上宏为真,获取使进程终止的信号编号
  • WIFSTOPPED(status) 非0,进程处于暂停状态
  • WSTOPSIG(status) 如果上宏为真,获取使进程暂停的信号的编号
  • WIFCONTINUED(status) 非0,进程暂停后已经继续运行
posted @ 2023-05-17 18:36  言叶以上  阅读(82)  评论(0编辑  收藏  举报