0
父進程將數組中的整數順序寫入管道。進程如何知道何時從管道讀取
...
close(thePipe[0]);
int array[]={1, 2, 5, 5, 5};
int j;
for(j=0; j<sizeof(array)/sizeof(int); j++){
write(thePipe[1], &(array[j]), sizeof(int));
}
close(thePipe[1];
...
它的子進程讀取這些整數並將它們相加。
...
close(thePipe[1]);
int sum = 0;
int buffer;
while(0 != read(thePipe[0], &buffer, sizeof(buffer))){
sum = sum + buffer;
}
close(thePipe[0]);
...
孩子怎麼知道什麼時候從管道讀取?
即使孩子獲得更多的CPU時間,它仍然不會在父級沒有寫入管道之前讀取。 這是如何工作的?