N 个子项向父级发送消息

N children send message to parent

本文关键字:消息      更新时间:2023-10-16

我所做的是我创建了n个孩子,然后父级使用n个管道向他们发送消息"start"。每个孩子一根烟斗。现在我正在努力做的是把每个孩子的号码发回给父母。

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

 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

所以我成功地向每个孩子发送了信息开始。但是我不知道父母将如何收到孩子发送的号码。

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

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