2016-07-06 10 views
0

私がしたことはn人の子供を作り、親がn個のパイプを使って "start"というメッセージを送りました。子供一人一人のパイプ。今私が苦労しているのは、親を各子供の数に戻すことです。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 

だから私は首尾よく私ができる各child.Butにメッセージのスタートを送信し親が子どもから送られた数字をどのように受け取るかを理解していない。

+1

アレイpのサイズはnです。だから 'p [0]'〜 'p [n-1]'を使って 'p [0]'に書き込むべきです。あなたは 'p [n]'を通して 'p [1]'に書いています。 –

+0

forループを 'for(int j = 0; j

+0

大丈夫 –

答えて

0

同様の処理を行うことができますが、ここでは親にはパイプの読み込み端があり、子には書き込み終了があります。上記の例を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); 
} 
関連する問題