¡¶²Ù×÷ϵͳ½Ì³Ì¡·£¨µÚÈý°æ£©CH1Ó¦ÓÃÌâ²Î¿¼´ð°¸
´ð£ºÊµÖÊÉÏÊÇÁ½¸ö½ø³ÌµÄͬ²½ÎÊÌ⣬ÉèÐźÅÁ¿S1ºÍS2·Ö±ð±íʾ¿É¼ð°××ӺͺÚ×Ó£¬²»Ê§Ò»°ãÐÔ£¬ÈôÁîÏȼð
°××Ó¡£
var S1,S2:semaphore;
S1:=1;S2:=0; cobegin {
process P1 begin repeat P(S1); ¼ð°××Ó
V(S2); until false; end
process P2 begin repeat P(S2); ¼ðºÚ×Ó
V(S1); until false;
end } coend.
6 É蹫¹²Æû³µÉÏ£¬Ë¾»úºÍÊÛƱԱµÄ»î¶¯·Ö±ðÈçÏ£º
˾»úµÄ»î¶¯£ºÆô¶¯³µÁ¾£ºÕý³£Ðгµ£»µ½Õ¾Í£³µ¡£ ÊÛƱԱµÄ»î¶¯£º¹Ø³µÃÅ£»ÊÛƱ£»¿ª³µÃÅ¡£
ÔÚÆû³µ²»¶ÏµØµ½Õ¾¡¢Í£³µ¡¢ÐÐÊ»¹ý³ÌÖУ¬ÕâÁ½¸ö»î¶¯ÓÐʲôͬ²½¹Øϵ£¿ÓÃÐźÅÁ¿ºÍP¡¢V²Ù×÷ʵÏÖËüÃǵÄͬ²½¡£
´ð£ºÔÚÆû³µÐÐÊ»¹ý³ÌÖУ¬Ë¾»ú»î¶¯ÓëÊÛƱԱ»î¶¯Ö®¼äµÄͬ²½¹ØϵΪ£ºÊÛƱԱ¹Ø³µÃźó£¬Ïò˾»ú·¢¿ª³µÐźţ¬Ë¾»ú½Óµ½¿ª³µÐźźóÆô¶¯³µÁ¾£¬ÔÚÆû³µÕý³£ÐÐÊ»¹ý³ÌÖÐÊÛƱԱÊÛƱ£¬µ½Õ¾Ê±Ë¾»úÍ£³µ£¬ÊÛƱԱÔÚ³µÍ£ºó¿ªÃÅÈó˿ÍÉÏϳµ¡£Òò´Ë£¬Ë¾»úÆô¶¯³µÁ¾µÄ¶¯×÷±ØÐëÓëÊÛƱԱ¹Ø³µÃŵĶ¯×÷È¡µÃͬ²½£»ÊÛƱԱ¿ª³µÃŵĶ¯×÷Ò²±ØÐëÓë˾»úÍ£³µÈ¡µÃͬ²½¡£
Ó¦ÉèÖÃÁ½¸öÐźÅÁ¿£ºs1¡¢s2£»s1±íʾÊÇ·ñÔÊÐí˾»úÆô¶¯Æû³µ(Æä³õֵΪ0)£»s2±íʾÊÇ·ñÔÊÐíÊÛƱԱ¿ªÃÅ(Æä³õֵΪ0)¡£ÓÃP¡¢VÔÓïÃèÊöÈçÏ£º var s1£¬s2£ºsemaphore£»
s1=0£» s2=0; cobegin {
driver ( ); busman ( ); } coend
17
¡¶²Ù×÷ϵͳ½Ì³Ì¡·£¨µÚÈý°æ£©CH1Ó¦ÓÃÌâ²Î¿¼´ð°¸
driver ( ) begin
while(1) {
P(s1)
Æô¶¯³µÁ¾£» Õý³£Ðгµ£» µ½Õ¾Í£³µ£» V(s2);
}
end
busman ( ) begin
while(1) {
¹Ø³µÃÅ£»£¬ V(s1) ÊÛƱ£» P(s2) ¿ª³µÃÅ£» ÉÏϳ˿ͣ»
}
end
7 ÔÚÐźÅÁ¿SÉÏ×÷P¡¢V²Ù×÷ʱ£¬SµÄÖµ·¢Éú±ä»¯£¬µ±S£¾0¡¢S=0¡¢S<0ʱ£¬ËüÃǵÄÎïÀíÒâÒåÊÇʲô£¿ ´ð£ºSµÄÖµ±íʾËü´ú±íµÄÎïÀí×ÊÔ´µÄʹÓÃ״̬£ºS>0±íʾ»¹Óй²Ïí×ÊÔ´¿É¹©Ê¹Óá£S=0±íʾ¹²Ïí×ÊÔ´Õý±»½ø³ÌʹÓõ«Ã»Óнø³ÌµÈ´ýʹÓÃ×ÊÔ´¡£S<0±íʾ×ÊÔ´Òѱ»·ÖÅäÍ꣬»¹Óнø³ÌµÈ´ýʹÓÃ×ÊÔ´¡£
8 (1)Á½¸ö²¢·¢½ø³Ì²¢·¢Ö´ÐУ¬ÆäÖУ¬A¡¢B¡¢C¡¢D¡¢EÊÇÔÓÊÔ¸ø³ö¿ÉÄܵIJ¢·¢Ö´Ðз¾¶¡£
Process P Process Q begin begin
A£» D£» B£» E£» C£» end£» end£»
(2) Á½¸ö²¢·¢½ø³ÌP1ºÍP2²¢·¢Ö´ÐУ¬ËüÃǵijÌÐò·Ö±ðÈçÏ£º P1 P2 repeat repeat k:=k¡Á2; print k; k:=k+1; k:=0; until false; until false;
ÈôÁîkµÄ³õֵΪ5£¬ÈÃP1ÏÈÖ´ÐÐÁ½¸öÑ»·£¬È»ºó£¬P1ºÍP2ÓÖ²¢·¢Ö´ÐÐÁËÒ»¸öÑ»·£¬Ð´³ö¿ÉÄܵĴòÓ¡Öµ£¬Ö¸³öÓëʱ¼äÓйصĴíÎó¡£
´ð£º
(1) ¹²ÓÐ10ÖÖ½»´íÖ´ÐеÄ·¾¶£º
A¡¢B¡¢C¡¢D¡¢E£»A¡¢B¡¢D¡¢E¡¢C£»A¡¢B¡¢D¡¢C¡¢E£»
18
¡¶²Ù×÷ϵͳ½Ì³Ì¡·£¨µÚÈý°æ£©CH1Ó¦ÓÃÌâ²Î¿¼´ð°¸
A¡¢D¡¢B¡¢E¡¢C£»A¡¢D¡¢B¡¢C¡¢E£»A¡¢D¡¢E¡¢B¡¢C£»
D¡¢A¡¢B¡¢E¡¢C£»D¡¢A¡¢B¡¢C¡¢E£»D¡¢A¡¢E¡¢B¡¢C£»D¡¢E¡¢A¡¢B¡¢C¡£ (2) °ÑÓï¾ä±àºÅ£¬ÒÔ±ãÓÚÃèÊö£º
P1 P2
repeat repeat
k:=k¡Á2; ¢Ù print k; ¢Û k:=k+1; ¢Ú k:=0; ¢Ü until false; until false;
1) KµÄ³õֵΪ5£¬¹ÊP1Ö´ÐÐÁ½¸öÑ»·ºó£¬K=23¡£ 2) Óï¾ä²¢·¢Ö´ÐÐÓÐÒÔÏÂÇé¿ö£º
¢Ù¡¢¢Ú¡¢¢Û¡¢¢Ü£¬ÕâʱµÄ´òӡֵΪ£º47 ¢Û¡¢¢Ü¡¢¢Ù¡¢¢Ú£¬ÕâʱµÄ´òӡֵΪ£º23 ¢Ù¡¢¢Û¡¢¢Ú¡¢¢Ü£¬ÕâʱµÄ´òӡֵΪ£º46 ¢Ù¡¢¢Û¡¢¢Ü¡¢¢Ú£¬ÕâʱµÄ´òӡֵΪ£º46 ¢Û¡¢¢Ù¡¢¢Ú¡¢¢Ü£¬ÕâʱµÄ´òӡֵΪ£º23 ¢Û¡¢¢Ù¡¢¢Ü¡¢¢Ú£¬ÕâʱµÄ´òӡֵΪ£º23
ÓÉÓÚ½ø³ÌP1ºÍP2²¢·¢Ö´ÐУ¬¹²ÏíÁ˱äÁ¿K£¬¹Ê²úÉúÁË¡®½á¹û²»Î¨Ò»¡¯¡£
9 ÁíÒ»¸ö¾µäͬ²½ÎÊÌ⣺ÎüÑÌÕßÎÊÌâ(patil£¬1971)¡£Èý¸öÎüÑÌÕßÔÚÒ»¸ö·¿¼äÄÚ£¬»¹ÓÐÒ»¸öÏãÑ̹©Ó¦Õß¡£Îª
ÁËÖÆÔì²¢³éµôÏãÑÌ£¬Ã¿¸öÎüÑÌÕßÐèÒªÈýÑù¶«Î÷£ºÑ̲ݡ¢Ö½ºÍ»ð²ñ£¬¹©Ó¦ÕßÓзḻ»õÎïÌṩ¡£Èý¸öÎüÑÌÕßÖУ¬µÚÒ»¸öÓÐ×Ô¼ºµÄÑ̲ݣ¬µÚ¶þ¸öÓÐ×Ô¼ºµÄÖ½ºÍµÚÈý¸öÓÐ×Ô¼ºµÄ»ð²ñ¡£¹©Ó¦ÕßËæ»úµØ½«Á½Ñù¶«Î÷·ÅÔÚ×À×ÓÉÏ£¬ÔÊÐíÒ»¸öÎüÑÌÕß½øÐжԽ¡¿µ²»ÀûµÄÎüÑÌ¡£µ±ÎüÑÌÕßÍê³ÉÎüÑ̺ó»½Ðѹ©Ó¦Õߣ¬¹©Ó¦ÕßÔÙ°ÑÁ½Ñù¶«Î÷·ÅÔÚ×À×ÓÉÏ£¬»½ÐÑÁíÒ»¸öÎüÑÌÕß¡£ÊÔ²ÉÓãº(1)ÐźÅÁ¿ºÍP¡¢V²Ù×÷£¬(2)¹Ü³Ì±àдËûÃÇͬ²½¹¤×÷µÄ³ÌÐò¡£ ´ð£º(1)ÓÃÐźÅÁ¿ºÍP¡¢V²Ù×÷¡£ var S,S1,S2,S3;semaphore; S:=1;S1:=S2:=S3:=0; flag1,flag2,flag3:Boolean; flag1:=flag2:=flag3:=true; cobegin {
process ¹©Ó¦Õß
begin
repeat P(S);
È¡Á½ÑùÏãÑÌÔÁÏ·Å×ÀÉÏ£¬ÓÉflagi±ê¼Ç; /*flage1¡¢flage2¡¢flage3´ú±íÑ̲ݡ¢Ö½¡¢»ð²ñ if flag2&flag3 then V(S1); /*¹©Ö½ºÍ»ð²ñ else if flag1&flag3 then V(S2); /*¹©Ñ̲ݺͻð²ñ else V(S3); /*¹©Ñ̲ݺÍÖ½ untile false; end
process ÎüÑÌÕß1
begin
repeat P(S1); È¡ÔÁÏ; ×öÏãÑÌ;
19
¡¶²Ù×÷ϵͳ½Ì³Ì¡·£¨µÚÈý°æ£©CH1Ó¦ÓÃÌâ²Î¿¼´ð°¸
V(S);
ÎüÏãÑÌ; untile false; process ÎüÑÌÕß2
begin
repeat P(S2); È¡ÔÁÏ; ×öÏãÑÌ; V(S); ÎüÏãÑÌ; untile false; process ÎüÑÌÕß3
begin
repeat P(S3); È¡ÔÁÏ; ×öÏãÑÌ; V(S); ÎüÏãÑÌ; untile false; }
coend.
10 ϵͳÓÐͬÀà×ÊÔ´m¸ö£¬±»n¸ö½ø³Ì¹²Ïí£¬ÎÊ£ºµ±m£¾nºÍm¡Ünʱ£¬Ã¿¸ö½ø³Ì×î¶à¿ÉÒÔÇëÇó¶àÉÙ¸öÕâ
Àà×ÊԴʱ£¬Ê¹ÏµÍ³Ò»¶¨²»»á·¢ÉúËÀËø? ´ð£ºµ±m¡Ünʱ£¬Ã¿¸ö½ø³Ì×î¶àÇëÇó1¸öÕâÀà×ÊԴʱ£¬ÏµÍ³Ò»¶¨²»»á·¢ÉúËÀËø¡£µ±m>nʱ£¬Èç¹ûm/n²»Õû³ý£¬Ã¿¸ö½ø³Ì×î¶à¿ÉÒÔÇëÇó¡±ÉÌ+1¡±¸öÕâÀà×ÊÔ´£¬·ñÔòΪ¡±ÉÌ¡±¸ö×ÊÔ´£¬Ê¹ÏµÍ³Ò»¶¨²»»á·¢ÉúËÀËø?
11 N¸ö½ø³Ì¹²ÏíM¸ö×ÊÔ´£¬Ã¿¸ö½ø³ÌÒ»´ÎÖ»ÄÜÉêÇë/ÊÍ·ÅÒ»¸ö×ÊÔ´£¬Ã¿¸ö½ø³Ì×î¶àÐèÒªM¸ö×ÊÔ´£¬ËùÓÐ
½ø³Ì×ܹ²µÄ×ÊÔ´ÐèÇóÉÙÓÚM+N¸ö£¬Ö¤Ã÷¸Ãϵͳ´Ëʱ²»»á²úÉúËÀËø¡£ ´ð£ºÉèmax £¨i£©±íʾµÚi¸ö½ø³ÌµÄ×î´ó×ÊÔ´ÐèÇóÁ¿£¬need(i)±íʾµÚi¸ö½ø³Ì»¹ÐèÒªµÄ×ÊÔ´Á¿£¬alloc(i)±íʾµÚi¸ö½ø³ÌÒÑ·ÖÅäµÄ×ÊÔ´Á¿¡£ÓÉÌâÖÐËù¸øÌõ¼þ¿ÉÖª£º
max(1)+©©+max(n)=(need(1)+©©+need(n))+((alloc(1)+©©+alloc(n)) ÁíÒ»·½ÃæËùÓнø³Ì½«ÏÝÈëÎÞÏ޵ȴý״̬¡£¿ÉÒÔÍƳö need(1)+ ©©+need(n) ÉÏʽ±íʾËÀËø·¢Éúºó£¬n¸ö½ø³Ì»¹ÐèÒªµÄ×ÊÔ´Á¿Ö®ºÍСÓÚn£¬ÕâÒâζ×Å´Ë¿ÌÖÁÉÙ´æÔÚÒ»¸ö½ø³Ìi,need(i)=0,¼´ËüÒÑ»ñµÃÁËËùÐèÒªµÄÈ«²¿×ÊÔ´¡£¼ÈÈ»¸Ã½ø³ÌÒÑ»ñµÃÁËËüËùÐèÒªµÄÈ«²¿×ÊÔ´£¬ÄÇôËü¾ÍÄÜÖ´ÐÐÍê³É²¢ÊÍ·ÅËüÕ¼ÓеÄ×ÊÔ´£¬ÕâÓëÇ°ÃæµÄ¼ÙÉèì¶Ü£¬´Ó¶øÖ¤Ã÷ÔÚÕâ¸öϵͳÖв»¿ÉÄÜ·¢ÉúËÀËø¡£ 12 É赱ǰµÄϵͳ״̬ÈçÏ£¬ÏµÍ³´ËʱAvailable=(1£¬1£¬2)£º 20