简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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