美文网首页
Linux中创建多个进程姿势

Linux中创建多个进程姿势

作者: 嘿嘿_小于同学 | 来源:发表于2017-04-16 21:47 被阅读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;
    }
    
    

    相关文章

      网友评论

          本文标题:Linux中创建多个进程姿势

          本文链接:https://www.haomeiwen.com/subject/rkhzattx.html