You are on page 1of 12





 Concurrent Process/Co-ordinating Process


 !"#$$%&'(")!(*+,-*!(%
%
./012304567608906:/;2<8/7=5178/>0?<8;3663605<30;513@8<7=3A05608/30890123098BB8C7/@01C801:?36D0
E(F#G#(F#(-% !"#$$H0IJ3;K178/08908/30?<8;3660A8360/8105993;1012303J3;K178/08908123<0?<8;36636L0
M!!G#,-*N#%!"#$$H0IJ3;K178/08908/30?<8;36605993;16012303J3;K178/08908123<0?<8;36636L0
0
O,"#%M!(F*-*!(H0P3Q3<5B0?<8;3663605;;36605/A0?<8;36601230R5/7?KB5178/608Q3<0123065R30A5150
;8/;K<<3/1B:>05/A0123/012308K1;8R30A3?3/A608/01230?5<17;KB5<08<A3<07/0C27;2012305;;366015S360?B5;3L0
%
TU,VGW#XHY310Z[05/A0Z\05<30;88?3<517Q30?<8;3663605/A0J076050625<3A0Q5<754B3L%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%U]^ %&),#F%N,*,_W#%
X% `%
abJc0 gbJc0
addc0 ghhc0
6B33?0e[fc0 6B33?0e[fc0
Jbac% Jbgc%
0
0i305<303J?3;17/@0123097/5B0Q5BK30890JC7BB0430j04K10AK30180<5;30;8/A7178/097/5B0Q5BK30890J0C7BB043037123<0k08<0
lL0
%
TU,VGW#`HY310Z[05/A0Z\05<30;88?3<517Q30?<8;3663605/A0J076050625<3A0Q5<754B3L%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%U]^ %&),#F%N,*,_W#%
X% `%
0 0
ab\mJ0 gbJd\0
Z<7/1eaf0 Z<7/1egf0
0
%
i30;5/0@310R8<30125/08/308K1?K10B7S30n[o>[\p>nq>[kp>n[\>[op05/A0n[k>qp0AK30180<5;30;8/A7178/0
98<0548Q30;88?3<517Q30?<8;36636L0
012134256186191
 2 041115



 !"#
$%&'()*+*(,-'.&(+*/0'*.','(/1&'.&23&0+'4%&)&'+%&'.%,)&1'5,)*,6-&.'(,0'6&',((&..&17'8+/3*(',(+*/0'*.'
)&9:*)&1'*0','()*+*(,-'.&(+*/0'*7&7'/0-;'/0&'<)/(&..'(,0'&=&(:+&'*0'*+.'()*+*(,-'.&(+*/0',+','+*3&7'8--'+%&'/+%&)'
<)/(&..&.'%,5&'+/'4,*+'+/'&=&(:+&'*0'+%&*)'()*+*(,-'.&(+*/0.7'
'
$%&'()*+*(,-'.&(+*/0'*.'2*5&0',.'>/--/4.?'

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '
@#AB!"#'%,01-&.'+%&'&0+);'*0+/'+%&'()*+*(,-'.&(+*/07'C+',(9:*)&.'+%&')&./:)(&.'0&&1&1'>/)'&=&(:+*/0'6;'
+%&'<)/(&..7'C0'+%&'&0+);'.&(+*/0D'+%&'<)/(&..')&9:&.+.'>/)'&0+);'*0'+%&'!"#E'
@FB!"#'%,01-&.'+%&'&=*+'>)/3'+%&'()*+*(,-'.&(+*/07'C+')&-&,.&.'+%&')&./:)(&.',01',-./'*0>/)3.'+%&'/+%&)'
<)/(&..&.'+%,+'()*+*(,-'.&(+*/0'*.'>)&&7'
'
G! HI!J!#B"KB"I"#"B!"#L"M!JN
80;'./-:+*/0'+/'+%&'()*+*(,-'.&(+*/0'<)/6-&3'3:.+'.,+*.>;'+%)&&')&9:*)&3&0+.?'
O7PQII@FIB"#'
R:+:,-'&=(-:.*/0'*3<-*&.'+%,+'/0-;'/0&'<)/(&..'(,0'6&'*0.*1&'+%&'()*+*(,-'.&(+*/0',+',0;'+*3&7'C>'
,0;'/+%&)'<)/(&..&.')&9:*)&'+%&'()*+*(,-'.&(+*/0D'+%&;'3:.+'4,*+':0+*-'*+'*.'>)&&7'C+'*.'&..&0+*,-'
)&9:*)&3&0+'+%,+'3:.+'6&'.,+*.>*&1'6;'+%&'./-:+*/0'/>'()*+*(,-'.&(+*/0'<)/6-&37'
S7PL"T!BB'
U)/2)&..'3&,0.'+%,+'*>','<)/(&..'*.'0/+':.*02'+%&'()*+*(,-'.&(+*/0D'+%&0'*+'.%/:-1'0/+'.+/<',0;'/+%&)'
<)/(&..'>)/3',((&..*02'*+7'C0'/+%&)'4/)1.D',0;'<)/(&..'(,0'&0+&)','()*+*(,-'.&(+*/0'*>'*+'*.'>)&&7'C+'*.'
&..&0+*,-')&9:*)&3&0+'+%,+'3:.+'6&'.,+*.>*&1'6;'+%&'./-:+*/0'/>'()*+*(,-'.&(+*/0'<)/6-&37'
V7PW"I#X!XY#T'
8'6/:01'3:.+'&=*.+'/0'+%&'0:36&)'/>'+*3&.'+%,+'/+%&)'<)/(&..&.',)&',--/4&1'+/'&0+&)'+%&*)'()*+*(,-'
.&(+*/0.',>+&)','<)/(&..'%,.'3,1&',')&9:&.+'+/'&0+&)'*+.'()*+*(,-'.&(+*/0',01'6&>/)&'+%,+')&9:&.+'*.'
2),0+&17'
'






012134256186191
 2 041115





!"#$%&
!"#$%&'()'*+,'-(.)*'/0120'31..,3*')145*(10'*1'*+,'65*574',8345)(10'9.1:4,6'
(0'31035..,0*'9.1;.766(0;<=-'*21'9.13,)),)'7**,69*'*1',0*,.'7'3.(*(374'),3*(10'7*'*+,')76,'*(6,>'*+,'
74;1.(*+6'2(44'74412'104?'10,'9.13,))'(0>':7),@'10'2+1),'*5.0'(*'()<'=-'10,'9.13,))'()'74.,7@?'(0'*+,'3.(*(374'
),3*(10>'*+,'1*+,.'9.13,))'2(44':5)?'27(*'-1.'*+,'-(.)*'9.13,))'*1',8(*<'A+()'()'@10,':?'*+,'5),'1-'*21'
-47;)>'270*)B*1B,0*,.CDE'70@'270*)B*1B,0*,.CFE>'2+(3+'(0@(37*,'70'(0*,0*(10'*1',0*,.'*+,'3.(*(374'),3*(10'10'
*+,'97.*'1-'9.13,)),)'D'70@'F>'.,)9,3*(G,4?>'70@'7'G7.(7:4,'*5.0'*+7*'(0@(37*,)'2+1'+7)'9.(1.(*?':,*2,,0'*+,'
*21'9.13,)),)<'H,//,.I)'74;1.(*+6';57.70*,,)'65*574',8345)(10>'-.,,@16'-.16'@,7@413/>'70@'-.,,@16'
-.16')*7.G7*(10J0,'7@G70*7;,'1-'*+()'74;1.(*+6'()'*+7*'(*'@1,)0I*'.,K5(.,')9,3(74'*,)*L70@L),*'M7*16(3'
.,7@N61@(-?N2.(*,O'(0)*.53*(10)'70@'()'*+,.,-1.,'+(;+4?'91.*7:4,':,*2,,0'470;57;,)'70@'673+(0,'
7.3+(*,3*5.,)<'J0,'@()7@G70*7;,'()'*+7*'(*'()'4(6(*,@'*1'*21'9.13,)),)<

$
PQ#"R"STU"V$#"RW

XP XY
Z%#[$V\ Z%#[$V\
] ]
Z%#[$VR^_`\a Z%#[$VR^_P\a
 f   f 
 
 
$VR_Pa $VR_`a
 
 g   g 
 
b b
c5*574'd8345)(10'()'7))5.,@':5*'e.1;.,))'()'01*'7))5.,@'(0'*+()')145*(10<'








012134256186191
 2 041115




 !!"#$$%&!'()!*

+, +
-.'/)(0 -.'/)(0
1 1
"'234567)(8 "'234,67)(8
-.'/"'234,60 -.'/"'234560
 R   R 
 
 
"'234567"2'8 "'234,67"2'8
 
 S   S 
 
9 9
:;<;=>?@AB>;CDEF?DC?=CC;GHI?=FI?JGEKGHCC?DC?=CC;GHI?DF?<LDC?CE>;<DEF?M;<?IH=I>EBN?O=P?EBB;G?DF?<LDC?
CE>;<DEFQ?

Dekker's algorithm to be written in exam

variable flag[2] of type boolean


turn: 0..1;
do{


flag[i] = true;
while flag[j]


if turn = j then


flag[i] = false;
<---- Code as discussed in class
while turn = j
;// no operation


flag[i] = true;

critical section

turn = j;

flag[i] = false;

remainder section

}while(TRUE)

012134256186191
 2 041115




!"#$
%&'()(*+,-.+/0,12)3,-/3+/4/514++3541/67898:"#$/),/);(/5*3)3541/+(5)3,-/<*,=1(>?/
%&@-/'()(*+,-.+/+,12)3,-A/B(/;4C(/)B,/+;4*(D/C4*34=1(+E/
F&=,,1(4-/G14HI3J/E@-3)3413K(D/),/LMN0OA/3-3)3411P/-,/,-(/3+/3-)(*(+)(D/3-/(-)(*3-H/);(/5*3)3541/+(5)3,-/
F&3-)/)2*-/E/Q;(/<*,5(++/B;,+(/)2*-/3+/),/(-)(*/);(/5*3)3541/+(5)3,-?/
/
R S
7T$"U#V 7T$"U#V
W W
6"8XYZ[\#] 6"8XYR[\#]
#\R] #\Z]
7T$"U#\\R^^6"8XYR[\\#V] 7T$"U#\\Z^^6"8XYZ[\\#V]
 g   g 
 
 
6"8XYZ[\68"] 6"8XYR[\68"]
 
 h   h 
 
_ _

'()(*+,-.+/0,12)3,-/<*(+(*C(+/411/);*((/5,-D3)3,-+E/
%&`2)241/Oa512+3,-/3+/4++2*(D/4+/,-1P/,-(/<*,5(++/54-/455(++/);(/5*3)3541/+(5)3,-/4)/4-P/)3>(?/
%&'*,H*(++/3+/41+,/4++2*(DA/4+/4/<*,5(++/,2)+3D(/);(/5*3)3541/+(5)3,-/D,(+/-,)/=1,5b/,);(*/<*,5(++(+/
G*,>/(-)(*3-H/);(/5*3)3541/+(5)3,-?/
%&c,2-D(D/d43)3-H/3+/<*(+(*C(D/4+/(C(*P/<*,5(++/H()+/4/G43*/5;4-5(?/
/
e3+4DC4-)4H(+/,G/'()(*+,-.+/0,12)3,-/
%&@)/3-C,1C(+/c2+P/B43)3-H/
%&@)/3+/13>3)(D/),/f/<*,5(++(+?/
/////////////////////////////////////////////////////////////////////////////
/
/
/
/
/
012134256186191
 2 041115





 !
"#$%&'()#%&*#&+,-,./01.%2%3#$4'53*2'67&%62'8*29:#;<='"#$%&'()#%>?#3&@#&$3&*#(:25A
@&*6&9:#?36535&'%&A##6%3#*2B%3#%?2@&:C#$>D2*E<
DF':25A
EG25A
H#B2*##'%#*6'I6'%2%3#5*6%65&:$#5%62'>&8*25#$$6'JC6*#$&92C%%3#:25A<=B6%6$:25A#(>6%A##8$2'?&6%6'I%6::6%
9#52;#B*##&'(6B6%6$'2%:25A#(>6%%&A#$%3#:25A&'(#K#5C%#$%3#5*6%65&:$#5%62'<
='"#$%&'()#%>LC%C&:#K5:C$62'&'(8*2I*#$$&*#8*#$#*@#(9C%92C'(#(?&6%6'I5&''2%9#8*#$#*@#(<
"3#M M6'$%*C5%62'6$&'6'$%*C5%62'C$#(%2?*6%#EN$#%O%2&;#;2*4:25&%62'&'(*#%C*'6%$2:(
@&:C#&$&$6'I:#&%2;65N6<#<>'2'P6'%#**C8%69:#O28#*&%62'<=B;C:%68:#8*25#$$#$;&4&55#$$%3#$&;#;#;2*4
:25&%62'>&'(6B&8*25#$$6$5C**#'%:48#*B2*;6'I&%#$%P&'(P$#%>'22%3#*8*25#$$;&49#I6'&'2%3#*%#$%P&'(P$#%C'%6:
%3#B6*$%8*25#$$Q$%#$%P&'(P$#%6$B6'6$3#(<RSTF;&4C$#&%#$%P&'(P$#%6'$%*C5%62'2BB#*#(94&'2%3#*#:#5%*2'65
52;82'#'%
R:25A5&'9#9C6:%C$6'I&'&%2;65%#$%P&'(P$#%6'$%*C5%62'&$B2::2?$U

BC'5%62'G25AN922:#&'V:25AOW
?36:#N%#$%X&'(X$#%N:25AOYYEOZ[

"3#5&::6'I8*25#$$29%&6'$%3#:25A6B%3#2:(@&:C#?&$D>2%3#*?6$#%3#?36:#P:228$86'$?&6%6'I%2&5JC6*#%3#
:25A<



boolean TestAndSet(Boolean * target)
{
boolean * rv= *target;


*target= TRUE;
return rv;


}


do
{ while(TestAndSet(&Lock))
; //do nothing
CRITICAL SECTION



Lock=False;
Remainder Section


}while(TRUE);



The characteristics of this synchronization mechanism are-



*It ensures mutual exclusion.
*It is deadlock free.


*It does not guarantee bounded waiting and may cause starvation.


*It suffers from spin lock.
*It is not architectural neutral since it requires the operating system to support test-and-set
instruction.
*It is a busy waiting solution which keeps the CPU busy when the process is actually waiting.
012134256186191
 2 041115





!"#$%&'()*$"+",#"&-",-.$/$*"0&*,&12$".(&.3"&'&*."4*)%",-,.,&2,5&.,)-3",#"&66$##$7")-28".(*)9/(".:)"#.&-7&*7"
&.)%,6")'$*&.,)-#;":&,."<="&-7"#,/-&2"<=>"?($":&,."<=")'$*&.,)-",#"&2#)"6&22$7"@"&-7"#,/-&2"<=",#"&2#)"6&22$7"
A>"
"
"BCDEDFDECGDFHI""
"
:&,.<+="J""
:(,2$<+"KL"M=N""
+OON"
P""
"
BCDEDFDECQDRESHI
"
#,/-&2<+="
"" J"
"+TTN""
P""
"
!22"%)7,4,6&.,)-#".)".($",-.$/$*"0&29$")4".($"#$%&'()*$",-".($":&,."<="&-7"#,/-&2"<=")'$*&.,)-#"%9#."1$"
$U$69.$7",-7,0,#,128>"?(&.",#3":($-")-$"'*)6$##"%)7,4,$#".($"#$%&'()*$"0&29$3"-)").($*"'*)6$##"6&-"
#,%92.&-$)9#28"

VSDWFDEC

X>Y+)20,-/"Z92.,"'*)6$##"[*,.,6&2"+$6.,)-"@*)12$%"
\>Y]$#)9*6$"&22)6&.,)-"&%)-/"0&*,)9#"'*)6$##$#"
^>Y_*7$*,-/"`U$69.,)-")4"'*)6$##$#>"
"
abQC

X>Y cDEbd"?(,#",#"&2#)"e-):-"&#"fFgSWh>"i."6&-"(&0$")-28".:)"0&29$#"j"M"&-7"X>"
i.#"0&29$",#",-,.,&2,5$7".)"X>"i.",#"9#$7".)",%'2$%$-."#)29.,)-")4"6*,.,6&2"#$6.,)-"'*)12$%":,.("%92.,'2$"
'*)6$##$#>"
\>Y kfEFDERd"i.#"0&29$"6&-"*&-/$")0$*"&-"9-*$#.*,6.$7"7)%&,->"i.",#"9#$7".)"6)-.*)2"
&66$##".)"&"*$#)9*6$".(&."(&#"%92.,'2$",-#.&-6$#>"
"

Mutual Exclusion code 

do

{

wait(s);


Critical Section

Signal(s); 
Remainder Section;
}While(TRUE); 
012134256186191
 2 041115



!"#$ %&$
%&$'()($ "(*+,-./+,.,0122+3,42,256+7,859+:,;,<3471=+3,=.>,<3471=+,.>,5?+@,.>7,=.>,<A.=+,5>,?-+,
0122+3:,;,=4>81@+3,=.>,<5=B,5?+@8,.>7,=.>,=4>81@+,?-+@:,*+,>++7,?4,+>813+,?-.?,C-+>,.,<3471=+3,58,
<A.=5>D,.>,5?+@,5>,?-+,0122+3E,?-+>,.?,?-+,8.@+,?5@+,=4>81@+3,8-41A7,>4?,=4>81@+,.>F,5?+@:,G>,?-58,
<340A+@E,0122+3,58,?-+,=35?5=.A,8+=?54>:,
H4,84A/+,?-58,<340A+@E,C+,>++7,?C4,=41>?5>D,8+@.<-43+8,I,J1AA,.>7,K@<?F:,LJ1AAM,B++<8,?3.=B,42,>1@0+3,
42,5?+@8,5>,?-+,0122+3,.?,.>F,D5/+>,?5@+,.>7,LK@<?FM,B++<8,?3.=B,42,>1@0+3,42,1>4==1<5+7,8A4?8:,

N"O(O)&OP)(O"Q#$)RS#,
@1?+6,T,U, this Solution is using Semaphore
J1AA,T,V,WW,G>5?5.AAFE,.AA,8A4?8,.3+,+@<?F:,H-18,21AA,8A4?8,.3+,V, previous solution can be utilized in exam as
K@<?F,T,>,WW,;AA,8A4?8,.3+,+@<?F,5>5?5.AAF,

discussed in class

'&(O"Q,
74X,
, do
, WW<3471=+,.>,5?+@, { //produce next production
, while(Counter==BUFFERSIZE)
, C.5?Y+@<?FZ[,
, C.5?Y@1?+6Z[,
;// do nothing

,
in=(in+1)%5;

, WW<A.=+,5>,0122+3, buffer[in]=next produced;


, counter++;
, 85D>.AY@1?+6Z[, }while(True);
, 85D>.AY21AAZ[,
,
\C-5A+Y?31+Z[,
*-+>,<3471=+3,<3471=+8,.>,5?+@,?-+>,?-+,/.A1+,42,L+@<?FM,58,3+71=+7,0F,U,0+=.18+,4>+,8A4?,C5AA,0+,25AA+7,
>4C:,H-+,/.A1+,42,@1?+6,58,.A84,3+71=+7,?4,<3+/+>?,=4>81@+3,?4,.==+88,?-+,0122+3:,]4CE,?-+,<3471=+3,-.8,
<A.=+7,?-+,5?+@,.>7,?-18,?-+,/.A1+,42,L21AAM,58,5>=3+.8+7,0F,U:,H-+,/.A1+,42,@1?+6,58,.A84,5>=3+.8+7,0F,U,
0+=.18+,?-+,?.8B,42,<3471=+3,-.8,0++>,=4@<A+?+7,.>7,=4>81@+3,=.>,.==+88,?-+,0122+3:,
'&(O"Q!"#$ ,
74X,
,
, C.5?Y21AAZ[, do
, C.5?Y@1?+6Z[, { while(Counter==0)
, ;//do nothing
, WW,3+@4/+,5?+@,234@,0122+3, next consumed=buffer[out];
, out=(out+1)%5;
, 85D>.AY@1?+6Z[, counter--;
, 85D>.AY+@<?FZ[, //consume next consumption
, }while (True);
, WW,=4>81@+8,5?+@,
,
\C-5A+Y?31+Z[,
;8,?-+,=4>81@+3,58,3+@4/5>D,.>,5?+@,234@,0122+3E,?-+3+243+,?-+,/.A1+,42,L21AAM,58,3+71=+7,0F,U,.>7,?-+,
/.A1+,58,@1?+6,58,.A84,3+71=+7,84,?-.?,?-+,<3471=+3,=.>>4?,.==+88,?-+,0122+3,.?,?-58,@4@+>?:,]4CE,?-+,
012134256186191
 2 041115

!"#$!%$&$!%"%'$!(")*+,$-./-012!(")*$3%")
%"#$!"$,#""$!/**41
56789:;:;<8=6:>?@?A67B8=B?C>7D88
=B?C>7D8EFGF7D7;FH2!I%%'J!%),!J/)$"$$!"$K,!%),!"$#"#"%)"
$"/)4%$!!,$%L/$4"!,"%*,!%),!12!%!,$%L/$4"!
,!%),!1M,!%),!"-"$%*!",%L,$!$4!,$%L"#N"$$!%1O!,$%L
"-/,%L#,/-"-*%$"#N"$*))4/$$/$!1

8
E7DGA6?B78E?>PF:?;8F?89:;:;<8=6:>?@?A67B8Q
R"!,!%),!%,$#/-$!*))4%',##S
,JT%U
4!%)$#
V 2WXYKZ
JX[K\J][WOJ^2X[KT%U&[WOJ^2X[KT%_0#`UaZ
RM2Z
J\2IObY][WOJ^2X[KT%U&[WOJ^2X[KT%_0#`Ua
c
2!"$!$"$*,!%),!S5defgefhi8djfhkl8G;m8no5efh1W$!"$4
",!Sp$3"#"",!""-*$!,!%),!1p$3%#!$!"$$4
,!%),!"-"$!,%L,,$#4"$$!"$%12!""-%#$$)$!
/!"(%*"!,!%),!1q$&",!")$%#"#)L#$,'"%'1
8
8 or Code can be written as using semaphore
8
8 Semaphore Chopstick[5];
8 do{
8 wait(Chopstick[i]);
8 wait(Chopstick[i+1]%5);
8
8 EAT
Signal(Chopstick[i]);
01THINKING
2Signal(Chopstick[i+1]%5);
134256186191
 2 041115
 }While(TRUE);



!"#"$"%#&
$"%#&'!'&'()*+,)-.-/012)34)5-4,6)780.)-)+2809+,93:-/)5-;5,;)4+08)<39+)0.,)5-;5,;=)*+,;,)34)-)
5-;5,;)4+08)<+3:+)+-4)0.,)5-;5,;>)0.,)5-;5,;):+-3;>)-.6).):+-3;4)?0;)<-393.1)?0;):7490@,;4)3?)9+,;,)-;,)-.2)
90)439)0.)9+,):+-3;=)
AB C?)9+,;,)34).0):7490@,;>)9+,.)9+,)5-;5,;)4/,,84)3.)+34)0<.):+-3;=)
AB D+,.)-):7490@,;)-;;3E,4>)+,)+-4)90)<-F,)78)9+,)5-;5,;=)
AB C?)9+,;,)-;,)@-.2):7490@,;4)-.6)9+,)5-;5,;)34):7993.1)-):7490@,;G4)+-3;>)9+,.)9+,);,@-3.3.1)
:7490@,;4),39+,;)<-39)3?)9+,;,)-;,),@892):+-3;4)3.)9+,)<-393.1);00@)0;)9+,2)/,-E,)3?).0):+-3;4)-;,)
,@892=)

)))))))))))))))))))))) )
%H'%()*+,)40/7930.)90)9+34)8;05/,@)3.:/76,4)9+;,,)4,@-8+0;,4=)I3;49)34)?0;)9+,):7490@,;)<+3:+):07.94)
9+,).7@5,;)0?):7490@,;4)8;,4,.9)3.)9+,)<-393.1);00@)J:7490@,;)3.)9+,)5-;5,;):+-3;)34).09)3.:/76,6)5,:-74,)
+,)34).09)<-393.1K=)L,:0.6>)9+,)5-;5,;)M)0;)N)34)74,6)90)9,//)<+,9+,;)9+,)5-;5,;)34)36/,)0;)34)<0;F3.1>)O.6)
9+,)9+3;6)@79,P)34)74,6)90)8;0E36,)9+,)@797-/),P:/7430.)<+3:+)34);,Q73;,6)?0;)9+,)8;0:,44)90),P,:79,=)C.)9+,)
40/7930.>)9+,):7490@,;)+-4)9+,);,:0;6)0?)9+,).7@5,;)0?):7490@,;4)<-393.1)3.)9+,)<-393.1);00@)3?)9+,)
.7@5,;)0?):7490@,;4)34),Q7-/)90)9+,).7@5,;)0?):+-3;4)3.)9+,)<-393.1);00@)9+,.)9+,)78:0@3.1):7490@,;)
/,-E,4)9+,)5-;5,;4+08=)
D+,.)9+,)5-;5,;)4+0<4)78)3.)9+,)@0;.3.1>)+,),P,:79,4)9+,)8;0:,67;,)5-;5,;>):-743.1)+3@)90)5/0:F)0.)9+,)
4,@-8+0;,):7490@,;4)5,:-74,)39)34)3.393-//2)M=)*+,.)9+,)5-;5,;)10,4)90)4/,,8)7.93/)9+,)?3;49):7490@,;):0@,4)
78=)
))))))))))))))))))))))))))))))))))))))))))))))))D+,.)-):7490@,;)-;;3E,4>)+,),P,:79,4):7490@,;)8;0:,67;,)9+,):7490@,;)
-:Q73;,4)9+,)@79,P)?0;),.9,;3.1)9+,):;393:-/);,130.>)3?)-.09+,;):7490@,;),.9,;4)9+,;,-?9,;>)9+,)4,:0.6)0.,)
<3//).09)5,)-5/,)90)-.29+3.1)7.93/)9+,)?3;49)0.,)+-4);,/,-4,6)9+,)@79,P=)*+,):7490@,;)9+,.):+,:F4)9+,):+-3;4)
3.)9+,)<-393.1);00@)3?)<-393.1):7490@,;4)-;,)/,44)9+-.)9+,).7@5,;)0?):+-3;4)9+,.)+,)4394)09+,;<34,)+,)
/,-E,4)-.6);,/,-4,4)9+,)@79,P=)
C?)9+,):+-3;)34)-E-3/-5/,)9+,.):7490@,;)4394)3.)9+,)<-393.1);00@)-.6)3.:;,@,.94)9+,)E-;3-5/,)<-393.1)
E-/7,)-.6)-/40)3.:;,-4,4)9+,):7490@,;G4)4,@-8+0;,)9+34)<-F,4)78)9+,)5-;5,;)3?)+,)34)4/,,83.1=)
O9)9+34)803.9>):7490@,;)-.6)5-;5,;)-;,)509+)-<-F,)-.6)9+,)5-;5,;)34);,-62)90)13E,)9+-9)8,;40.)-)+-3;:79=)
D+,.)9+,)+-3;:79)34)0E,;>)9+,):7490@,;),P394)9+,)8;0:,67;,)-.6)3?)9+,;,)-;,).0):7490@,;4)3.)<-393.1);00@)
5-;5,;)4/,,84=)

012134256186191
 2 041115




!!""#$%$
&'()*+,*-.)//+.-001'2.3(2-'+4&567+2/+3+0).83'2/0+982.8+3::-9/+,*-.)//)/+(-+.-001'2.3()+)3.8+-(8)*+
3';+/<'.8*-'2=)+(8)2*+3.(2-'/>+5*-.)//)/+.3'+.-001'2.3()+92(8+)3.8+-(8)*+1/2'?+(8)/)+(9-+93</@+
A>B C83*);+D)0-*<+
E>B D)//3?)+,3//2'?+

FG % HI "  JI G H 
C83*);+0)0-*<+*)K12*)/+,*-.)//)/+(-+/83*)+/-0)+L3*23M:)+3';+2(+.-0,:)():<+;),)';/+-'+8-9+,*-?*300)*+
92::+20,:)0)'(+2(>++
N')+93<+-O+.-001'2.3(2-'+1/2'?+/83*);+0)0-*<+.3'+M)P+C1,,-/)+,*-.)//A+3';+,*-.)//E+3*)+)Q).1(2'?+
/201:(3')-1/:<+3';+(8)<+/83*)+/-0)+*)/-1*.)/+-*+1/)+/-0)+2'O-*03(2-'+O*-0+-(8)*+,*-.)//R+,*-.)//A+
?)')*3()+2'O-*03(2-'+3M-1(+.)*(32'+.-0,1(3(2-'/+-*+*)/-1*.)/+M)2'?+1/);+3';+S)),/+2(+3/+3+*).-*;+2'+
/83*);+0)0-*<>+T8)'+,*-.)//E+'));+(-+1/)+(8)+/83*);+2'O-*03(2-'R+2(+92::+.8).S+2'+(8)+*).-*;+/(-*);+2'+
/83*);+0)0-*<+3';+(3S)+'-()+-O+(8)+2'O-*03(2-'+?)')*3();+M<+,*-.)//A+3';+3.(+3..-*;2'?:<>+5*-.)//)/+.3'+
1/)+/83*);+0)0-*<+O-*+)Q(*3.(2'?+2'O-*03(2-'+3/+3+*).-*;+O*-0+-(8)*+,*-.)//+3/+9)::+3/+O-*+;):2L)*2'?+3'<+
/,).2O2.+2'O-*03(2-'+(-+-(8)*+,*-.)//>++
+

+++++++++++++ +
I!!%U$UV%!!$UIGH+
&'+(82/+0)(8-;R+,*-.)//)/+.-001'2.3()+92(8+)3.8+-(8)*+92(8-1(+1/2'?+3'<+S2';+-O+-O+/83*);+0)0-*<>+&O+
(9-+,*-.)//)/+,A+3';+,E+93'(+(-+.-001'2.3()+92(8+)3.8+-(8)*R+(8)<+,*-.));+3/+O-::-9@+
WB X/(3M:2/8+3+.-001'2.3(2-'+:2'S+42O+3+:2'S+3:*)3;<+)Q2/(/R+'-+'));+(-+)/(3M:2/8+2(+3?32'>7+
012134256186191
 2 041115

 !"#$%#$&''$'('%#$)'%!*%&%%+',
-#./'01*%&%%+'2
345678&''$9.'%#%1#:145678&''$:
3;5<5=>58&''$9"1':1;5<5=>58&''$:


?"&''$'%@!#)1AA%.'%@11A+%)/'%@,BA%%'1AA%.'%@9%%''CA1D.'%$#
)(!1&*/%!.A1*1$&&#.%A%%'1A+%)/'%@"#%%''CA1*1$&&)(
!1&*/%!.A1"D.'%$#,E'#..&''$!#"+01*'2F5G75;HG67HIJ7KL
?"F5G75;HMG;N%'('.A1'1%#$O''$C*9.'%#%1#%.9'1(!%.9#.&''$/#$"#.
!1#1/%#A1&%1#,?"!1#1/%#A1&%1#!1#%#'%#A1&%1#/%P0"1.1%A(#'1(1A)(AA
'*!9'Q(#!#(&)9*%1%C,R#//C9&''$%''#('%#$SBSD'C/,
H

Swap Solution for Critical Section

# another type of hardware assistance for process synchronization


# a special hardware instruction that does a complete swap (ordinarily three operations) atomically
i.e., the complete swap is executed or no part of it is given pass-by-reference parameters A and B, it swaps their
values

Swap Solution to the Critical Section Problem uses two variables called lock and key
intuition: if lock is false, then a process can enter the critical section, and otherwise it can't initially, lock is false

void swap(boolean *a, boolean *b)


{ boolean * temp =*b;
*b=*a;
*a=*temp;
}
do
{
Key= true
while (Key == true) swap(&lock,&Key);
critical section
lock = false;
remainder section
}while(True);

012134256186191
 2 041115


You might also like