zoukankan      html  css  js  c++  java
  • wait,waitpid学习测试

    用man wait学习wait waitpid的使用

    • wait()函数功能:wait()函数使父进程暂停执行,直到它的一个子进程结束为止,该函数的返回值是终止运行的子进程的PID. 参数status所指向的变量存放子进程的退出码,即从子进程的main函数返回的值或子进程中exit()函数的参数。如果status不是一个空指针,状态信息将被写入它指向的变量。
    • waitpid函数功能:waitpid()的作用和wait()一样,但它并不一定要等待第一个终止的子进程,它还有若干选项,如可提供一个非阻塞版本的wait()功能等。实际上wait()函数只是waitpid()函数的一个特例。
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    main()
    {
           int status;
           pid_t pc,pr;
            
            pc=fork();
            if(pc<0) /* 如果出错 */
                   printf("error ocurred!
    ");
            else if(pc==0){ /* 子进程 */
                   printf("This is child process with pid of %d.
    ",getpid());
               
            }
            else{ /* 父进程 */
                   pr=wait(&status);
                   if(WIFEXITED(status)){ /* 如果WIFEXITED返回非零值 */
                           printf("the child process %d exit normally.
    ",pr);
                         
                   }else /* 如果WIFEXITED返回零 */
                       printf("the child process %d exit abnormally.
    ",pr);
            }
    }
    

    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    main()
    {
           int status;
           pid_t pc,pr;
            
            pc=fork();
            if(pc<0) /* 如果出错 */
                   {printf("error ocurred!
    ");
                   return -1;}
            else if(pc==0){ /* 子进程 */
                   printf("This is child process with pid of %d.
    ",getpid());
                   
            }
            else{ /* 父进程 */
                   pr=wait(&status);
                   if(WIFEXITED(status)){ /* 如果WIFEXITED返回非零值 */
                           printf("the child process %d exit normally.
    ",pr);
                           printf("the return code is %d.
    ",WEXITSTATUS(status));
                   }else /* 如果WIFEXITED返回零 */
                       printf("the child process %d exit abnormally.
    ",pr);
            }
    }
    

  • 相关阅读:
    pyqt-QGrapicsView类
    pyqt5.0 GraphicsView框架
    STM32(三)- GPIO输入输出之按键检测点亮LED
    STM32(二)- 位带操作、启动文件讲解
    STM32(一)- 基于固件库的工程模板
    C语言(四)- C预处理和C库
    C语言(三)- 结构体、结构体指针、位运算
    C语言(二)- 函数、指针、数组
    C语言(一)- 基础知识
    [caffe笔记]:杀死caffe多个进程中的某个(发生 leveldb lock 解决方法)
  • 原文地址:https://www.cnblogs.com/banpingcu/p/11830647.html
Copyright © 2011-2022 走看看