创建2个子进程

时间:2016-07-04 10:20:38

标签: c++ linux process pipe fork

我必须在3个进程之间创建这种通信:

  • 1个进程(父进程):

    • 向孩子1发送偶数号码;
    • 向孩子2发送不均匀的数字;
    • 获取从孩子1和孩子2发送的号码
  • 2进程(子1):

    • 从父母那里得到偶数;
    • 向孩子2发送偶数号码;
    • 向父母发送2 * value_of_number
  • 3流程(孩子2):

    • 来自父母的数字不均匀;
    • 向父母发送2 * value_of_number

这是我的代码:

int main()
{
    int p12[2],p13[2],p23[2];
    int p21[2],p31[2];

    if(pipe(p12)<0){
        perror("pipe 12 error\n");
        exit(1);
    }

    if(pipe(p13)<0){
        perror("pipe 13 error\n");
        exit(1);
    }

    if(pipe(p23)<0){
        perror("pipe 23 error\n");
        exit(1);
    }

    if(pipe(p21)<0){
        perror("pipe 21 error\n");
        exit(1);
    }

    if(pipe(p31)<0){
        perror("pipe 31 error\n");
        exit(1);
    }

    switch(fork()){
        case -1:{
            perror("fork 1 error\n");
            exit(1);
        }
        case 0:{//1 child
            close(p12[1]);
            close(p13[1]);
            close(p13[0]);
            close(p23[0]);
            close(p21[0]);
            close(p31[1]);
            close(p31[0]);
            int paros;
            int ket;
            while(read(p12[0],&paros,sizeof(int))>0){
                cout<<"2: "<<paros<<endl;
                if(write(p23[1],&paros,sizeof(int))==-1){
                    perror("write 23 error\n");
                    exit(1);
                }
                ket=2*paros;
                if(write(p21[1],&ket,sizeof(int))==-1){
                    perror("write 21 error\n");
                    exit(1);
                }
            }
            close(p21[1]);
            close(p12[0]);
            close(p23[1]);
            exit(0);
        }
        default:{
            switch(fork()){
                case -1:{
                    perror("fork 2 error\n");
                    exit(1);
                }
                case 0:{//2 child
                    close(p13[1]);
                    close(p12[1]);
                    close(p12[0]);
                    close(p23[1]);
                    close(p31[0]);
                    close(p21[1]);
                    close(p21[0]);
                    int szamok;
                    int ket;
                    while(read(p13[0],&szamok,sizeof(int))>0){
                        cout<<"3: "<<szamok<<endl;
                        ket=2*szamok;
                        if(write(p31[1],&ket,sizeof(int))==-1){
                            perror("write 31 error\n");
                            exit(1);
                        }
                    }
                    while(read(p23[0],&szamok,sizeof(int))>0){
                        cout<<"3: "<<szamok<<endl;
                    }
                    close(p31[1]);
                    close(p13[0]);
                    close(p23[0]);
                    exit(0);
                }
                default:{
                    close(p12[0]);
                    close(p13[0]);
                    close(p23[0]);
                    close(p23[1]);
                    close(p21[1]);
                    close(p31[1]);
                }
            }
        }
    }
    int i=1;
    while(i<=10){
        if(i%2==0){
            if(write(p12[1],&i,sizeof(int))==-1){
                perror("write 12 error\n");
                exit(1);
            }
        }
        else{
            if(write(p13[1],&i,sizeof(int))==-1){
                perror("write 13 error\n");
                exit(1);
            }
        }
        i++;
    }
    int szam;
    while(read(p21[0],&szam,sizeof(int))>0){
        cout<<"1: "<<szam<<endl;
    }
    while(read(p31[0],&szam,sizeof(int))>0){
        cout<<"1: "<<szam<<endl;
    }
    close(p12[1]);
    close(p13[1]);
    close(p31[0]);
    close(p21[0]);
    while(wait(NULL)>0){};
    exit(0);
}

但由于某种原因,它不起作用......

1 个答案:

答案 0 :(得分:0)

您的父母在每个管道中写入5个数字,然后等待孩子1的所有回复,然后再转到孩子2.在给孩子们写数字之后,你不要关闭那些管道,所以那些孩子不知道父母已经停止了。在孩子1的情况下,它仍在等待从父母那里接收更多号码。因此,它不会关闭自己的管道并退出。因此,父母一直停留在孩子1的第一个阅读循环中,而且永远不会到达孩子2。

写入p12 [1]和p13 [1]后,添加

close(p12[1]);
close(p13[1]);

并且该过程将完成