【问题标题】:Create 2 child processes创建 2 个子进程
【发布时间】:2016-07-04 10:20:37
【问题描述】:

我必须在 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);
}

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

【问题讨论】:

  • 你遇到了什么问题:你期望什么行为以及你真正得到什么行为?

标签: c++ linux process pipe fork


【解决方案1】:

您的父母在每个管道中写入 5 个数字,然后等待孩子 1 的所有响应,然后再转到孩子 2。在将数字写入孩子之后,您不会关闭这些管道,所以这些孩子不知道父已停止。在孩子 1 的情况下,它仍在等待从父母那里接收更多的数字。因此,它永远不会关闭自己的管道并退出。因此,父母卡在从孩子 1 开始的第一个读取循环中,永远不会到达孩子 2。

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

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

该过程将运行完成

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-10
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    相关资源
    最近更新 更多