-- 作者:llllllll
-- 发布时间:10/14/2006 6:50:00 PM
-- 回复北大必考题操作系统之PV操作
<html><body>我的解法:(以下几进程并发执行) semaphore:RedGuest,BlkGuest,ShipGuest,Check,ship,s; int Red=0,Blk=0; RedGuest=0; /*用于红客登船安全同步*/ BlkGuest=0; /*用于黑客登船安全同步*/ ShipGuest=4; /*用于红、黑客等船同步,即船运客未回*/ check=0; /*用于登船安全检查同步*/ ship=0; /*开船同步信号*/ s=1; /*用于红、黑客登船互斥*/ void RedGuestGetShip(){/*红客登船进程*/ L1:V(check); /*Call the SafeCheck()*/ P(ShipGuest); P(s); P(RedGuest); Red=Red+1; the RedGuest get ship; V(check); /*Call the SafeCheck()*/ V(s); goto L1; } void BlkGuestGetShip(){/*黑客登船进程*/ L2:V(check); /*Call the SafeCheck()*/ P(ShipGuest); P(s) P(BlkGuest); Blk=Blk+1; the RedGuest get ship; V(check); /*Call the SafeCheck()*/ V(s); goto L2; } void SafeCheck(){ /*登船安全检查进程*/ p(check); if(Red+Blk==4) { V(ship); /*start the ship*/ } if(Red+Blk==3) { if(Red%2!=0) /*if Red%2!=0,so there are 3 or 1 RedGuest on ship,*/ V(RedGuest); /* then let 1 RedGuest get ship is safe.*/ else /*if Red%2==0,so there are 2 or 0 RedGuest on ship,*/ V(BlkGuest); /* then let 1 RedGuest get ship is safe.*/ } else { V(RedGuest); V(BlkGuest); } } void ShipGuest(){ /*船送乘客过河进程*/ P(ship); Ship the Guest to another bank and return; Red=0; Blk=0; V(ShipGuest); V(ShipGuest); V(ShipGuest); V(ShipGuest); } </body></html>
|