操作系统——Linux进程

安稳与你 提交于 2019-11-29 20:57:49

 

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main( )
{
    pid_t son_pid,daughter_pid;
    int count =1;
    son_pid =fork();            //父进程创建son子进程
    if(son_pid == 0)
    {
        count++;
        printf("I am son,count = %d\n",count);
    }
    else
    {
        daughter_pid = fork();  //父进程创建daughter子进程
        if(daughter_pid == 0)
        {
            count++;
            printf("I am daughter,count = %d\n",count);
        }
        else
        {
            count++;
            printf("I am father,count = %d\n",count);
            //父进程等待son及daughter进程退出后才结束
            waitpid(son_pid,NULL,0);
            waitpid(daughter_pid,NULL,0);
        }
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!