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
    -------------
    */
    ————————————————

  • 相关阅读:
    【NOIP2016】换教室
    【NOIP模拟赛】总结
    【Codeforces Round 418】An impassioned circulation of affection DP
    DP测试总结
    【NOIP2012】疫情控制
    【HNOI2016】序列 莫队+单调栈+RMQ
    【Luogu P2709 小B的询问】莫队
    【HNOI2017】影魔
    【HNOI2017】大佬
    阿里云MaxCompute 2019-7月刊
  • 原文地址:https://www.cnblogs.com/cqx6388/p/14892203.html
Copyright © 2011-2022 走看看