2016-07-06 96 views
0

我所做的是我創建了n個孩子,並且父母使用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 

所以我成功發送的消息開始到各child.But我可以我們不知道父母如何接收孩子們發送的號碼。

+1

數組p的大小爲n。所以你應該通過'p [n-1]'寫入'p [0]'。你正在通過'p [n]'給'p [1]'寫信。 –

+0

將您的循環更改爲'for(int j = 0; j

+0

好吧我會編輯 –

回答

0

你可以做一個類似的過程但是在這裏父節點有管道的讀端和寫端的子節點。擴展上面的例子只包含一個管道。每個孩子可以有多個管道。

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