单选题 20.生产者进程和消费者进程代码如下。生产者进程有一个局部变量nextProduced,以存储新产生的新项:
while(1){
/*produce an item in nextProduced*/
while((in+1)%BUFFER_SIZE==out};/*do nothing*/
buffer[in]=nextProduced;
in=(in+1)%BUFFER_SIZE;
}
消费者进程有一个局部变nextConsumed,以存储所要使用的项:
while(1){
while(in==out);/*do nothing*/
nextConsumed=buffer[out],
out=(0ut+i)%BUFFER_SIZE;
/*consume the item in nextConsumed*/
}
当in==out和(in+1)%BUFFER_SIZE==out条件成立时,缓冲区中item数目各是( )。
【正确答案】 B
【答案解析】通过阅读代码可知,变量in指向缓冲区中下一个空位,变量out指向缓冲区中的第一个非空位。BUFFER SIZE是缓冲区最大能容纳的item数目。buffer中,非空的位置范围是[out,in—1]或者[out,BUFFER SlZE—1]∪[0,in—1],即有如图6—8所示的两种情况。