简体   繁体   English

N个孩子向父母发送消息

[英]N children send message to parent

What I did is I created n children and than the parent sent the message "start" to them using n pipes. 我要做的是创建了n个孩子,然后父母使用n个管道向他们发送了“开始”消息。 One pipe for each child.Now what I'm struggling to do is to send the parent back the number of each child. 每个孩子一个烟斗。现在我正在努力做的是将每个孩子的编号发回给父母。

This is my code until now: 到目前为止,这是我的代码:

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

} }

And this is the output: 这是输出:

  0_start
  2_start
  1_start
  4_start
  3_start

So I successfully sent the message start to each child.But I can't figure out how will the parent receive the numbers sent by the children. 这样我就成功地向每个孩子发送了开始信息,但是我不知道父母将如何接收孩子发送的电话号码。

You can do a similar process But here the parent has the read end of the pipe and the children the write end. 您可以执行类似的过程,但此处父级具有管道的读取端,子级具有写入端。 Extended the example above to include one pipe only . 将上面的示例扩展为仅包含一个管道。 You could have multiple pipes one each for each child. 您可以有多个管道,每个管道一个。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM