【问题标题】:N children send message to parentN 个孩子向父母发送消息
【发布时间】:2016-07-06 14:08:24
【问题描述】:

我所做的是我创建了 n 个孩子,然后父母使用 n 个管道向他们发送消息“开始”。每个孩子一个管道。现在我正在努力做的是向父母发回每个孩子的数量。

这是我到目前为止的代码:

 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);

}

这是输出:

  0_start
  2_start
  1_start
  4_start
  3_start

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

【问题讨论】:

  • 数组 p 的大小为 n。所以你应该写信给p[0]p[n-1]。您正在写信至p[1]p[n]
  • 将 for 循环更改为 for(int j=0; j &lt; n; j++)
  • 好的,我来编辑一下

标签: c++ linux pipe fork


【解决方案1】:

你可以做一个类似的过程但是这里父有管道的读端,子有写端。将上面的示例扩展为仅包含一根管道。您可以为每个孩子设置多个管道。

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2022-08-03
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多