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

  • 相关阅读:
    Office办公 如何设置WPS的默认背景大小
    百科知识 已知三角形三条边长,如何求解三角形的面积
    Office 如何添加Adobe Acrobat虚拟PDF打印机
    电脑技巧 如何保存网页为PDF
    JAVA Eclipse打开报错failed to load the jni shared library怎么办
    JAVA Eclipse如何导入已有的项目
    easy UI获取数据,打开毕弹窗
    easyUi 的DataGrid的绑定
    MVC异步分页
    MVC分页
  • 原文地址:https://www.cnblogs.com/cqx6388/p/14892203.html
Copyright © 2011-2022 走看看