zoukankan      html  css  js  c++  java
  • unix环境高级编程----进程控制wait()

    一、wait()函数

    当一个进程中调用wait()函数的时候

    (1)假设其全部的子程序都还在执行,则堵塞

    (2)假设一个子进程已终止。则等待父进程获取其终止状态。

    (3)假设没有子进程,则返回错误。


    以下的实例中。在父进程中调用wait(),假设子进程还没有执行完成,则将自己调入堵塞状态。

    等待子进程执行结束后,将子进程的资源回收后,自己再执行。

    #include <stdio.h>
    #include <unistd.h>
    #include <wait.h>
    #include <stdlib.h>
    
    int main()
    {
       int i=0;
       int j=0;
       int status;
       int count =0;
       int a = fork();
       if(a>0)
          {
            printf("this is parent ,pid = %d
    ",getpid());
            for(i= 0;i<=10;i++)
            {
                printf("parent is %d
    ",i);
                sleep(1);
                wait(&status);
            }
    //        wait(&status);
          }
       else
         {
    
               for(j=0;j<=10;j++)
               {
                  printf("child is %d
    ",j);
                  sleep(1);
               }
         }
    return 0;
    }
    



  • 相关阅读:
    JDBC连接MySQL并且查询操作。
    struts
    KMP 剪花布条hdoj2087
    线段树---敌兵布阵hdoj 1166
    设计模式----观察者模式
    线段树--hdoj1754
    ZOJ 2283 Challenge of Wisdom
    SGU 134 Centroid
    UVA 1637 Double Patience
    HDU 4389 X mod f(x)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5377861.html
Copyright © 2011-2022 走看看