Linux中创建多个进程姿势

2017-04-16  本文已影响62人  嘿嘿_小于同学

在Linux中一个父进程创建多个子进程姿势

# include <unistd.h>
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <sys/wait.h>

int main()
{
    pid_t status;
    int i;
    int isFather = 1;
    
    for(i=0; i<8; i++)
    {
        status= fork();
        
        if(status == -1)
        {
            printf("Create ChildProcess Errror!\n");
            exit(1);
        }
        else if(status == 0)
        {
            printf("I Am Child, My pid is %d\n", getpid());
            break;
        }
        else if(isFather==1 && status > 0)
        {
            printf("I Am Father, My pid is %d\n", getpid());
            isFather = 0;
        }
    }
    
    return 0;
}

上一篇下一篇

猜你喜欢

热点阅读