N个孩子向父母发送消息

时间:2016-07-06 14:08:25

标签: c++ linux pipe fork

我做的是创建了n个孩子,而且父母使用n个管道向他们发送了“start”消息。每个孩子都有一个烟斗。现在我正在努力做的就是把父母寄回给每个孩子的号码。

这是我的代码,直到现在:

UIGestureRecognizer

}

这是输出:

 int main()
{
int n=5;

int p[n-1][2];

int i;
for(i=0;i<n;i++){
    if(pipe(p[i])>0){
        perror("pipe error");
        exit(1);
    }
}

for(i=0;i<n;i++){
    pid_t pid=fork();
    if(pid<0){
        perror("fork error");
        exit(1);
    }
    if(pid==0){
        int j;
        for(j=0;j<n;j++){
            close(p[j][1]);
        }
        for(j=0;j<i;j++){
            close(p[j][0]);
        }
        char msg[256];
        int h;
        read(p[i][0],&h,sizeof(int));
        read(p[i][0],msg,h*sizeof(char));
        cout<<i<<"_"<<msg<<endl;
        close(p[i][0]);//here I would like to send the number i to the parent
        for(j=i+1;j<n;j++){
            close(p[j][0]);
        }
        exit(0);
    }
}

char ms[256];
strcpy(ms,"start");
int ho=strlen(ms);
for(i=0;i<n;i++){
    if(write(p[i][1],&ho,sizeof(int))==-1){
        perror("write error");
        exit(1);
    }
    if(write(p[i][1],ms,ho*sizeof(ms))==-1){
        perror("write error");
        exit(1);
    }
    close(p[i][1]);
}
for(int j=0;j<n;j++)
    close(p[j][0]);//then read the number of each child and print it
while(wait(NULL)>0){};
exit(0);

所以我成功地将消息开始发送给每个孩子。但是我无法弄清楚父母将如何接收孩子们发送的号码。

1 个答案:

答案 0 :(得分:0)

您可以执行类似的过程但是这里父级具有管道的读取端,而子级具有写入端。将上面的示例扩展为仅包含一个管道。每个孩子可以有一个多个管道。

int main()
{
int n=5;

int p[n-1][2];
int pw[2]; // pipe child writes into 

int i;
for(i=0;i<n;i++){
    if(pipe(p[i])>0){
        perror("pipe error");
        exit(1);
    }
}
    if(pipe(pw)>0){
        perror("pipe error");
        exit(1);
    }

for(i=0;i<n;i++){
    pid_t pid=fork();
    if(pid<0){
        perror("fork error");
        exit(1);
    }
    if(pid==0){
        int j;
        for(j=0;j<n;j++){
            close(p[j][1]);
        }
        close(pw[0]);// close read end - child
        for(j=0;j<n;j++){
            if ( i!= j ) close(p[j][0]);
        }
        char msg[256];
        int h;
        read(p[i][0],&h,sizeof(int));
        read(p[i][0],msg,h*sizeof(char));
        cout<<i<<"_"<<msg<<endl;
        close(p[i][0]);//here I would like to send the number i to the parent
        write(pw[1],&i,sizeof(int));  // send i 
        close(pw[1]);
        exit(0);
    }
}

char ms[256];
strcpy(ms,"start");
int ho=strlen(ms);
int value;
for(i=0;i<n;i++){
    if(write(p[i][1],&ho,sizeof(int))==-1){
        perror("write error");
        exit(1);
    }
    if(write(p[i][1],ms,ho*sizeof(ms))==-1){
        perror("write error");
        exit(1);
    }
    close(p[i][1]);
    close(pw[1]); //close write end 
    if(read(pw[0],&value,sizeof(int))==-1){ // read from child process
        perror("write error");
        exit(1);
    }
    cout << " in  main "<<value<<endl; // display number 
}
for(int j=0;j<n;j++)
    close(p[j][0]);//then read the number of each child and print it
while( wait(NULL) > 0 ){;}
exit(0);
}