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