系统编程之进程管理 wait()

来源:互联网 发布:红警2网络找不到人 编辑:程序博客网 时间:2024/05/02 06:19
#include<unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
int main()
{
    pid_t child;
    /*创建子进程*/
    if((child=fork())==-1)
    {
        printf("Fork Error:%s\n",strerror(errno));
        exit(1);
    }
    else
        if (child == 0) //child
        {
            printf("the child process is running \n");
            sleep(1);
            printf("I am the chil: %d\n",getpid());
            exit(0);
        }
        else //parament
        {
            wait(NULL);//等到任一子进程退出,父进程才会运行
          //  waitpid(child,NULL,0); //等待特定的进程
            printf("the father process starts run\n");
            printf("I am the father: %d\n",getpid());
            return 0;
        }
}