zoukankan      html  css  js  c++  java
  • waitpid WNOHANG

    waitpid WNOHANG

    原文链接:https://blog.csdn.net/weixin_37787043/article/details/78714621

    #include<stdio.h>
    #include<stdlib.h>
    #include <unistd.h>
    //waitpid WNOHANG
    int main(void)
    {
      printf("main process pid = %d ",getpid());
      int status;
      pid_t pid;
      pid = fork();//创建子进程
      if(pid < 0)
      {
        perror("fork error");
        exit(1);//结束进程
      }

      if(pid == 0)
      {
        printf("子进程pid = %d ",getpid());
        sleep(3);
        printf("******** ");
        exit(2);
      }

      if(pid > 0)
      {
        printf("父进程pid = %d ",getpid());

        //waitpid(pid,&status,0);//等同wait(&status);
        //WNOHANG,waitpid()不阻塞而且立即返回,返回值为0
        while(waitpid(pid,&status,WNOHANG) == 0)
        {
          printf("-----子进程运行中----- ");
          sleep(1);
        }

        //判断子进程结束状态
        if(WIFEXITED(status))//进程正常结束
        {
          printf("normal exit status = %d ",WIFEXITED(status));
        }

        if(WIFSIGNALED(status))//进程异常终止
        {
          printf("recv signal exit ");
        }
        printf("------------- ");
        exit(1);
      }

      return 0;
    }
    /*
    $ ./a.out
    main process pid = 24977
    父进程pid = 24977
    -----子进程运行中-----
    子进程pid = 24978
    -----子进程运行中-----
    -----子进程运行中-----
    ********
    -----子进程运行中-----
    normal exit status = 1
    -------------
    */
    ————————————————

  • 相关阅读:
    类的内部成员之五-----内部类
    接口的使用
    java中abstract关键字的使用
    Redis主从复制原理——哨兵模式(Sentinel)
    Redis主从复制原理——薪火相传
    Redis主从复制原理——一主二仆
    Git---使用Github实现团队内协作操作步骤
    Final知识点总结
    代码块知识点总结
    Linux学习计划
  • 原文地址:https://www.cnblogs.com/cqx6388/p/14892203.html
Copyright © 2011-2022 走看看