Lecture Locks
-
Upload
subhadip-das-sarma -
Category
Documents
-
view
216 -
download
0
description
Transcript of Lecture Locks
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 1/23
CGS 3763 Operating Systems Concepts
Spring 2013
Dan C. MarinescuOffice: HC 30!
Office "ours: M#$% 11:30 # 12:30 &M
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 2/23
'ast time:
(o%ay: ("rea% coor%ination) 'oc*s
+e,t time
'ecture - Mon%ay) /anuary 2) 2013
12116 2
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 3/23
("rea% coor%ination
Critica section co%e t"at accesses a s"are% resource
ace con%itions t4o or more t"rea%s access s"are% %ata an% t"e resut
%epen%s on t"e or%er in 4"ic" t"e t"rea%s access t"e s"are% %ata.
Mutua e,cusion ony one t"rea% s"ou% e,ecute a critica section at any
one time.
Sc"e%uing agorit"ms %eci%e 4"ic" t"rea% to c"oose 4"en mutipe
t"rea%s are in a 5++&' state 8O first in first out
'8O ast in first out
9riority sc"e%uing
D eariest %ea%ine first
9reemption aiity to stop a running acti;ity an% start anot"er one 4it" a
"ig"er priority. Si%e effects of t"rea% coor%ination
Dea%oc*
9riority in;ersion a o4er priority acti;ity is ao4e% to run efore one 4it" a "ig"er priority
'ecture 1 3
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 4/23
Soutions to t"rea% coor%ination proems must satisfy a
set of con%itions
'ecture 1 4
1. Safety: ("e re<uire% con%ition 4i ne;er e ;ioate%.
2. 'i;eness: ("e system s"ou% e;entuay progress irrespecti;e of contention.
3. ree%om rom Star;ation: +o process s"ou% e %enie% progress for e;er. ("at
is) e;ery process s"ou% ma*e progress in a =nite time.
!. oun%e% $ait: ;ery process is assure% of not more t"an a =,e% numer of
o;erta*es y ot"er processes in t"e system efore it ma*es progress.>. airness: %epen%ent on t"e sc"e%uing agorit"m
? 8O: +o process 4i e;er o;erta*e anot"er process.
? '5: ("e process 4"ic" recei;e% t"e ser;ice east recenty gets t"e ser;ice ne,t.
or e,ampe for t"e mutua e,cusion proem t"e soution s"ou% guarantee t"at:
Safety t"e mutua e,cusion property is ne;er ;ioate%
'i;eness a t"rea% 4i access t"e s"are% resource in a =nite time
ree%om for star;ation a t"rea% 4i access t"e s"are% resource in a =nite time
oun%e% 4ait a t"rea% 4i access t"e s"are% resource at east after a =,e% numer of
accesses y ot"er t"rea%s.
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 5/23
("rea% coor%ination proems
Dining p"iosop"ers Critica section
'ecture 1 5
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 6/23
& soution to critica section proem
&ppies ony to t4o t"rea%s (i an% ( @ 4it" i)@ AB0)1 4"ic" s"are integer turn if turn=i t"en it is t"e turn of ( i to enter t"e critica section
boolean flag[2] if flag[i]= TRUE t"en (i is rea%y to enter t"e critica section
(o enter t"e critica section t"rea% (i
sets flag[i]= TRUE
sets turn=j
8f ot" t"rea%s 4ant to enter t"en turn 4i en% up 4it" a ;aue of eit"er i or j
an% t"e correspon%ing t"rea% 4i enter t"e critica section.
(i enters t"e critica section ony if eit"er flag[j]= FALSE or turn=i
("e soution is correct Mutua e,cusion is guarantee%
("e i;einess is ensure%
("e oun%e%#4aiting is met
ut t"is soution may not 4or* as oa% an% store instructions can e
interrupte% on mo%ern computer arc"itectures
'ecture 1 6
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 8/23
'oc*s efore#or#&fter actions
'oc*s s"are% ;ariaes 4"ic" acts as a fag to coor%inate access to a
s"are% %ata. Manipuate% 4it" t4o primiti;es &CE58
'&S
Support impementation of efore#or#&fter actions ony one t"rea% can
ac<uire t"e oc*) t"e ot"ers "a;e to 4ait.
& t"rea%s must oey t"e con;ention regar%ing t"e oc*s. ("e t4o operations &CE58 an% '&S must e atomic.
Har%4are support for impementation of oc*s SM ea% an% Set Memory
CM9 Compare an% S4ap
SM Fmem
8f mem=LOCKED t"en SM returns r=LOCKED an% sets mem=LOCKED 8f mem=ULOCKED t"e SM returns r=LOCKED an% !et! mem=LOCKED
'ecture 1 8
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 11/23
Dea%oc*s
Happen <uite often in rea ife an% t"e propose% soutions are not
a4ays ogica: $"en t4o trains approac" eac" ot"er at a crossing)
ot" s"a come to a fu stop an% neit"er s"a start up again unti
t"e ot"er "as gone.I a pear from Jansas egisation. Dea%oc* @ury.
Dea%oc* egisati;e o%y.
'ecture 1 11
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 12/23
,ampes of %ea%oc*
(raffic ony in one %irection.
Soution one car ac*s up Fpreempt resources an% roac*. Se;era
cars may "a;e to e ac*e% up .
Star;ation is possie.
'ecture 1 12
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 14/23
("rea% %ea%oc*
Dea%oc*s pre;ent sets of concurrent t"rea%sprocesses fromcompeting t"eir tas*s.
Ho4 %oes a %ea%oc* occur a set of oc*e% t"rea%s eac"
"o%ing a resource an% 4aiting to ac<uire a resource "e% y
anot"er t"rea% in t"e set.
,ampe
oc*s A an% ") initiaiKe% to 1
# 0 # 1
4ait F& 4aitF
4ait F 4aitF&
&im pre;ent or a;oi% %ea%oc*s
'ecture 1 14
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 15/23
System mo%e
esource types R 1) R 2) . . .) R m $C#U %&%le!' memor& !(a%e' )*O+e,i%e!-
ac" resource type R i "as . i instances.
esource access mo%e:
re<uest
use reease
'ecture 1 15
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 16/23
Simutaneous con%itions for %ea%oc*
Mutua e,cusion: ony one process at a time can use a resource. Ho% an% 4ait: a process "o%ing at east one resource is 4aiting to
ac<uire a%%itiona resources "e% y ot"er processes. +o preemption: a resource can e reease% ony ;ountariy y t"e
process "o%ing it Fpresumay after t"at process "as finis"e%.
Circuar 4ait: t"ere e,ists a set B# 0) # 1) L) # 0 of 4aiting processes suc"
t"at # 0 is 4aiting for a resource t"at is "e% y # 1) # 1 is 4aiting for a
resource t"at is "e% y # 2) L) # n 1 is 4aiting for a resource t"at is "e% y
# n) an% # 0 is 4aiting for a resource t"at is "e% y # 0.
'ecture 1 16
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 17/23
$ait for grap"s
'ecture 1 17
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 19/23
("e 4ait an% signa operations
9 Fs F4ait
B
8f s.C 0 t"en s.C N N
ese @oin s.'
Fs Fsigna
B
8f s.' is empty t"en s.C P P
ese reease a process from s.'
'ecture 1 19
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 20/23
Monitors
Semap"ores can e use% incorrecty
mutipe t"rea%s may e ao4e% to enter t"e critica section guar%e% y t"e semap"ore may cause %ea%oc*s
("rea%s may access t"e s"are% %ata %irecty 4it"out c"ec*ing t"e semap"ore.
Soution encapsuate s"are% %ata 4it" access met"o%s to operate on t"em.
Monitors an astract %ata type t"at ao4s access to s"are% %ata 4it" specific
met"o%s t"at guarantee mutua e,cusion
'ecture 1 20
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 22/23
&sync"ronous e;ents an% signas
Signas) or soft4are interrupts) 4ere originay intro%uce% in 5ni, to notify aprocess aout t"e occurrence of a particuar e;ent in t"e system.
Signas are anaogous to "ar%4are 8O interrupts: $"en a signa arri;es) contro 4i arupty s4itc" to t"e signa "an%er.
$"en t"e "an%er is =nis"e% an% returns) contro goes ac* to 4"ere it came from
&fter recei;ing a signa) t"e recei;er reacts to it in a 4e#%e=ne% manner. ("atis) a process can te t"e system FOS 4"at t"ey 4ant to %o 4"en signa
arri;es: 8gnore it.
Catc" it an% %ei;er it. 8n t"is case) it must specify Fregister t"e signa "an%ing
proce%ure. ("is proce%ure resi%es in t"e user space. ("e *erne 4i ma*e a ca to
t"is proce%ure %uring t"e signa "an%ing an% contro returns to *erne after it is%one.
Ji t"e process F%efaut for most signas.
,ampes: ;ent # c"i% e,it) signa # to parent. Contro signa from *eyoar%.
'ecture 1 22
7/21/2019 Lecture Locks
http://slidepdf.com/reader/full/lecture-locks 23/23
Signas state an% impementation
& signa "as t"e foo4ing states: Signa sen% # & process can sen% signa to one of its group memer process
Fparent) siing) c"i%ren) an% furt"er %escen%ants.
Signa %ei;ere% # Signa it is set.
9en%ing signa # %ei;ere% ut not yet recei;e% Faction "as not een ta*en.
Signa ost # eit"er ignore% or o;er4ritten. 8mpementation: ac" process "as a *erne space Fcreate% y %efaut
cae% signa %escriptor "a;ing its for eac" signa. Setting a it is %ei;ering
t"e signa) an% resetting t"e it is to in%icate t"at t"e signa is recei;e%. &
signa cou% e oc*e%ignore%. ("is re<uires an a%%itiona it for eac"
signa. Most signas are system controe% signas.
'ecture 1 23