进程等待(wait和wait函数)【Linux】

简介: 进程等待(wait和wait函数)【Linux】


进程等待的概念

  进程等待就是通过 wait/waitpid的方式,让父进程对子进程进行等待子进程退出并且将子进程进行资源回收的过程。

进程等待的必要性

 为什么要进行进程等待?

   1. 因为父子进程的执行顺序是不确定的,可能父进程会比子进程先一步退出,此时就会让子进程成为僵尸进程,子进程已经退出,但是依旧占用着内存空间,造成内存泄漏。

   2. 由于一般来说,子进程的出现是为了协助父进程完成一些任务,子进程就像是父进程的手下小兵一样,因此子进程完成父进程交给的任务,退出后也一定要给父进程一个答复(子进程是否完成,是否出现异常等)。

返回值:

成功返回被等待进程pid,失败返回-1。

参数:

输出型参数,获取子进程退出状态,不关心则可以设置成为NULL 3. 父进程通过进程等待的方式,回收退出的子进程的资源,获取子进程的退出信息(也就是2)。

进程等待的方法

wait函数

#include <sys/types.h>

#inlclude <sys/wait.h>

pid_t wait(int* status);

返回值:

成功返回被等待进程pid,失败返回-1。

参数:

输出型参数,获取子进程退出状态,不关心则可以设置成为NULL,之后和waitpid一起介绍。

 如何进行等待?

  1. 调用wait函数,进程等待子进程的退出。
  2. 当子进程退出后,会变成一个僵尸进程(短暂的存在不会造成什么影响),然后通过wait函数,进程状态从僵尸状态(Z)变成死亡状态(X)。
  3. 如果子进程没有退出,父进程必须阻塞等待,直到子进程变成Z,wait自动回收返回。
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <cstdlib>
int main()
{
    pid_t fd = fork();
    if (fd == 0)
    {
        sleep(5);
        std::cout << "child: " << getpid() << std::endl;
        exit(0);
    }
    if (fd > 0)
    {
        sleep(1);
        std::cout << "parent: " << getpid() << std::endl;
    }
    sleep(50);
    return 0;
}

子进程退出后由于父进程没有等待回收,子进程变成僵尸进程:

调用wait后,子进程就会回收释放了:

#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <cstdlib>
int main()
{
    pid_t fd = fork();
    if (fd == 0)
    {
        sleep(5);
        std::cout << "child: " << getpid() << std::endl;
        exit(0);
    }
    if (fd > 0)
    {
        sleep(1);
        std::cout << "parent: " << getpid() << std::endl;
        wait(NULL);
    }
    sleep(50);
    return 0;
}

waitpid函数

#include <sys/types.h>

#inlclude <sys/wait.h>

pid_ t waitpid(pid_t pid, int *status, int options);

返回值:

  当正常返回的时候waitpid返回收集到的子进程的进程ID;

如果设置了选项WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,则返回0;

  如果调用中出错,则返回-1,这时errno会被设置成相应的值以指示错误所在;

参数:

 pid:

  Pid=-1,等待任一个子进程。与wait等效。

  Pid>0.等待其进程ID与pid相等的子进程。

 status:

  WIFEXITED(status): 若为正常终止子进程返回的状态,则为真。(查看进程是否是正常退出)

  WEXITSTATUS(status): 若WIFEXITED非零,提取子进程退出码。(查看进程的退出码)

 options:

  WNOHANG: 若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若正常结束,则返回该子进

程的ID。

  status其实就是个int型的输出型参数,一共32bit,但是我们只需要关注低位的16bit就行。下图是16bit, status的示意图:

使用方法:

  • 判断是否正常终止,可以提取低7位,如果全0就是正常终止:
    (status & 0x7F)
  • 提取退出状态:( (status >> 8) & 0xFF)
  • 提取终止状态:(status & 0x7F)
  • 提取core dump: ( (status >> 7) & 0x1)

除了使用上面的方法,也可以:

  • 判断是否正常终止:WIFEXITED(status);若为正常终止子进程返回的状态,则为真.
  • 提取子进程退出码:WEXITSTATUS(status); 若WIFEXITED非零,提取子进程退出码。(查看进程的退出码)
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <cstdlib>
int main()
{
    pid_t fd = fork();
    if (fd == 0)
    {
        sleep(5);
        std::cout << "child: " << getpid() << std::endl;
        exit(8);
    }
    if (fd > 0)
    {
        int st;
        int ret = waitpid(fd, &st, 0);
        if (ret > 0 && (st & 0x7F) == 0)
        {
            std::cout << "退出状态:" << ((st >> 8) & 0xFF) << std::endl;
        }
        else if (ret > 0)
        {
            std::cout << "终止信号:" << (st & 0x7F) << std::endl;
        }
        std::cout << "parent: " << getpid() << std::endl;
    }
    return 0;
}

正常终止:

被kill杀死:上面的代码中子进程存在的时间延长

#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <cstdlib>
int main()
{
    pid_t fd = fork();
    if (fd == 0)
    {
        sleep(50);
        std::cout << "child: " << getpid() << std::endl;
        exit(8);
    }
    if (fd > 0)
    {
        int st;
        int ret = waitpid(fd, &st, 0);
        if (ret > 0 && (st & 0x7F) == 0)
        {
            std::cout << "退出状态:" << ((st >> 8) & 0xFF) << std::endl;
        }
        else if (ret > 0)
        {
            std::cout << "终止信号:" << (st & 0x7F) << std::endl;
        }
        std::cout << "parent: " << getpid() << std::endl;
    }
    return 0;
}

  验证时通过while :; do ps axj | head -1 && ps axj | grep test11 | grep -v "grep"; sleep 1; echo "###############"; done的指令来过去子进程的pid。

非阻塞等待和阻塞等待的对比

阻塞等待:

#include <iostream>
#include <unistd.h>
#include <sys/wait.h
> 这里是引用
#include <cstdlib>
int main()
{
    pid_t fd = fork();
    if (fd == 0)
    {
        sleep(10);
        std::cout << "child: " << getpid() << std::endl;
        exit(8);
    }
    if (fd > 0)
    {
        int ret;
        int st;
        do
        {
            ret = waitpid(fd, &st, 0);
            std::cout << "如果是非阻塞等待,则指向这个打印" << std::endl;
        } while (ret == 0);
        if (ret > 0 && (st & 0x7F) == 0)
        {
            std::cout << "退出状态:" << ((st >> 8) & 0xFF) << std::endl;
        }
        else if (ret > 0)
        {
            std::cout << "终止信号:" << (st & 0x7F) << std::endl;
        }
    }
    return 0;
}

  可以看到由于waitpid函数是这样的:waitpid(fd, &st, 0);option也就是第三个参数为0,表示阻塞等待,所以如果是非阻塞等待,则指向这个打印这句话只执行了一次。

非阻塞等待

  非阻塞等待也叫非阻塞轮询式等待,既然是轮询,就需要使用while循环的进行waitpid,否则如果waitpid没有等到子进程,就会执行下面的指令,直接执行完然后退出父进程。就不能回收子进程资源了。使用while时,需要注意这句话:如果设置了选项WNOHANG,而调用中waitpid发现没有已退出的子进程可收集,则返回0;

#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <cstdlib>
int main()
{
    pid_t fd = fork();
    if (fd == 0)
    {
        sleep(10);
        std::cout << "child: " << getpid() << std::endl;
        exit(8);
    }
    if (fd > 0)
    {
        int ret;
        int st;
        do
        {
            ret = waitpid(fd, &st, WNOHANG);
            std::cout << "如果是非阻塞等待,则指向这个打印" << std::endl;
        } while (ret == 0);
        if (ret > 0 && (st & 0x7F) == 0)
        {
            std::cout << "退出状态:" << ((st >> 8) & 0xFF) << std::endl;
        }
        else if (ret > 0)
        {
            std::cout << "终止信号:" << (st & 0x7F) << std::endl;
        }
    }
    return 0;
}

  可以看到由于waitpid函数是这样的:waitpid(fd, &st, WNOHANG);option也就是第三个参数为WNOHANG,表示非阻塞等待,所以如果是非阻塞等待,则指向这个打印这句话只执行了数次。


   😄 创作不易,你的点赞和关注都是对我莫大的鼓励,再次感谢您的观看😄

相关文章
|
21小时前
|
Linux 数据库
linux守护进程介绍 | Linux的热拔插UDEV机制
linux守护进程介绍 | Linux的热拔插UDEV机制
linux守护进程介绍 | Linux的热拔插UDEV机制
|
21小时前
|
Linux
linux线程创建等待及退出总结
linux线程创建等待及退出总结
|
21小时前
|
Unix Linux 调度
linux线程与进程的区别及线程的优势
linux线程与进程的区别及线程的优势
|
22小时前
|
Linux
linux中wait与waitpid函数使用场景及扩展
linux中wait与waitpid函数使用场景及扩展
|
22小时前
|
Linux
linux中fork函数与vfork函数的区别
linux中fork函数与vfork函数的区别
|
22小时前
|
Linux 调度 C语言
|
2天前
|
存储 安全 Linux
【Linux】详解进程通信中信号量的本质&&同步和互斥的概念&&临界资源和临界区的概念
【Linux】详解进程通信中信号量的本质&&同步和互斥的概念&&临界资源和临界区的概念
|
2天前
|
消息中间件 算法 Linux
【Linux】详解如何利用共享内存实现进程间通信
【Linux】详解如何利用共享内存实现进程间通信