生产者与消费者问题
toann 2004-09-13 03:38:01 mutex:初值为1,控制互斥访问缓冲池;
生产者私用信号量empty:初值为n,表示空缓冲块数目;
消费者私用信号量full:初值为0,表示满缓冲块数目;
整型变量i、j:初值为0,i表示空缓冲块序号头指针,j表示满缓冲块序号头指针
procedure producer //生产者进程 procedure consumer //消费者进程
begin begin
while true do while true do
begin begin
produce next product; P(full);
P(empty); P(mutex);
P(mutex); goods := buffer(j);
buffer(i) := product; j := (j+1) mod n;
i := (i+1) mod n; V(mutex);
V(mutex); V(empty);
V(full); consume product;
end end
end; end;
两进程为并发进程,我的问题是:如果交换两进程中P操作或V操作的次序,会有什么影响?
书上说如果颠倒P操作的次序,会造成死锁。但没有说明原因。
请大虾帮忙看看,能否详细解释一下。万分感谢!!
^_^