Sie sind auf Seite 1von 17

Concurrency and Mutual Exclusion CS 350 10/12/03

The need for Mutual exclusion in concurrent processing.


! Overview an example: Concurrent processing implies asynchronism: the outcome of concurrent processing may be non deterministic if certain situations are not prevented: Example of a !"ace Condition# This is an example of a print spooler. It is taken from taken from Mo ern !peratin" S#stems$ %# &n re' Tanen%a(m$ pp. 33)3*. It ill(strates an apparentl# m(n ane sit(ation$ an an o%+io(s %(t na,+e approa-h 'hi-h 'ill eli+er erroneo(sl# res(lts. o & pro-ess 'antin" to print a file an enters the file name in a print .(e(e to a'ait printin". o & print aemon perio i-all# -he-ks the .(e(e for files to %e printe an if so it -hooses one$ remo+es it from the .(e(e an prints it. o &ss(me the .(e(e is a infinitel# lar"e arra# 'hose slots are in exe / 0$1$2$0 o There are t'o share +aria%les/ o(t ) points to next file in .(e(e to %e printe in pints to the next free slot in the .(e(e o &ss(me slots 0 thro("h 3 are empt# 1files alrea # printe 2 an slots * tho("h 3 are f(ll 1the# ha+e names of files #et to %e printe 2 next free slot is 4. o &ss(me pro-esses & an 5 %oth 'ant to print a file an the follo'in" se.(en-e takes pla-e/
CS350 6all 2003 7rin-iples of -on-(rren-# pa"e 1

) 7ro-ess & rea s in an stores the +al(e 4 in a lo-al +aria%le next8free8slot. ) & -ontext s'it-h no' takes pla-e 1time sli-e2 an pro-ess 5 s'it-hes into a-ti+e exe-(tion. 7ro-ess 5 also rea s in an also "ets a 4 so it stores the name of the file in slot 4 an (p ates in to %e an 9 it then "oes off an oes somethin" else. ) 7ro-ess & no' %e-omes a-ti+e a"ain an (sin" next8free8slot$ fin s a 4 there an 'rites its file name in slot 4$ erasin" the name that pro-ess 5 :(st p(t there. It then p(ts an 9 in in ) pro-ess 5 ne+er "ets its file printe .

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 2

Mutual Exclusion: $oftware %pproaches


;e -onsi er t'o attempts< to sol+e the m(t(al ex-l(sion pro%lem/ &ll sol(tions s(ffer from %(s# 'aitin"$ an 'ill in efinitel# %lo-k the other pro-ess if one pro-ess -rashes in the -riti-al se-tion.

&irst attempt: int turn ' () ** initial value for turn *+ process ( +* *+ processes , +* 'hile1t(rn => 02 no)op? 'hile1t(rn => 12 no)op? /@ -riti-al se-tion@/ /@ -riti-al se-tion@/ t(rn > 1? t(rn > 0? A A Stri-t alternation %et'een t'o pro-esses +ia (se of share +aria%le t(rn Mutual exclusion achieved Two problems: 7erforman-e etermine %# least a-ti+e 1slo'est2 pro-ess If one pro-esses -rashes o(tsi e of the -riti-al se-tion$ the other 'ill 'ait fore+er for the CS.

$econd attempt: *+ process ( +* *+ processes , +* fla"B0C > tr(e? fla"B1C > tr(e? 'hile1fla"B1C2 no)op? 'hile1fla"B0C2 no)op? /@ -riti-al se-tion@/ /@ -riti-al se-tion@/ fla"B0C > false? fla"B1C > false? A A Crash o(tsi e CS 'ill not -a(se in efinitel# %lo-k other pro-ess M(t(al ex-l(sion a-hie+e -roblem: Dea lo-k -a(se %# ra-e to set fla"BiC

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 3

Correct $oftware $olutions:


.e//er0s %lgorithm: Com%ines (sin" the t(rn +aria%le an fla"BC +aria%le. &+oi s m(t(al -o(rtes# t(rn "(arantees m(t(al ex-l(sion. fla"BC %reaks stri-t alternation pro%lem if #o(r fla" is set to 1 an other fla" is 0$ then enter no matter 'hat t(rn is. In the e+ent that a ra-e -a(ses fla"B0C > fla"B1C > 1$ then there is no ea lo-k to "et to the CS %e-a(se t(rn 'ill allo' one of the pro-esses to enter. fla"BiC is an in i-ation of intent to enter. $ee flow chart below for details.

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e *

Initial +al(es/ fla"B0C > fla"B1C > 0

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 5

-eterson0s %lgorithm: Interpret fla"BiC as in i-tin" that pro-ess i is intereste in enterin" the CS. T(rn resol+es the sim(ltaneit# -onfli-ts as 'ith Dekkers Does the same as Dekkers$ %(t is simpler. $ee flow chart below for details:

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 3

Initial +al(es/ fla"B0C > fla"B1C > 0

This is as good as it gets with software solutions the nemesis of all software solutions is:

1usy waiting 2et0s see what hardware and the O$ has to offer.

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 4

Mutual exclusion: hardware support


3nterrupt disabling: 'hile112 E *+ isa%le interr(pts +*) /@ critical section @/? /@ ena%le interr(pts @/? /@ remainder section @/? 4 M(t(al ex-l(sion "(arantee . 5igh cost: exe-(tion ineffi-ient I (nipro-essor -annot interlea+e pro-esses. ;ill not 'ork a m(ltipro-essin" ar-hite-t(re ph#si-al parallelism s#n-hroniFin" interr(pts a-ross pro-essors a pro%lem. !test and set# instruction $ee $tallings page 6,7 and fig. 8.8a 3n all cases i '' , means C$ is loc/ed9 !the door is loc/ed# boolean testset :int i; E //alternate efinition flo' -hart$ next pa"e if 1i >> 02 E i > 1? ret(rn false? A else Eret(rn tr(eA A <ote: if i >> 1 1lo-ke 2$ i sta#s 1 an ret(rne 1 if i >> 0$ -han"e i to 1 an ret(rn 0 an enter CS &nother 'a# of -o in" the same f(n-tion 11st efinition flo' -hart$ next pa"e/ boolean testset :int i; E if 1 i > > 02 Ei > 1? ret(rn tr(e?A else ret(rn 1complement if i2 A

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 9

6lo' Chart/

6or alternate efinition for testGset1i2 if 1i >> 02 Ei > 1? ret(rn 0?A if 1i >> 12 ret(rn 1?

// or more simpl#/ : > i? i > 1? ret(rn :?


CS350 6all 2003 7rin-iples of -on-(rren-# pa"e H

!exchange# or !swap# instruction +oi ex-han"e 1int re"ister$ int memor#2 E int temp? temp > memor#? memor# > re"ister? re"ist"er > temp? A 6lo' -hart/

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 10

$emaphores
& s#n-hroniFation me-hanism pro+i e %# the operatin" s#stem %ase on a si"nalin" an %lo-kin" s-heme. ;ill enfor-e m(t(al ex-l(sion an also re"(late the operation of pro-esses %ase on the +al(es of -ertain "lo%al n(meri-al +aria%les 1-o(ntin" +aria%les2. $ome properties of semaphores: & si"nal is transmitte +ia semaphore s %# exe-(tin" a primiti+e -alle si"nal1s2. To re-ei+e a si"nal +ia semaphore s$ a pro-ess exe-(tes the primiti+e 'ait1s2. & 'ait -all could -a(se a pro-ess %lo-k epen in" on the value of a semaphore s. & si"nal could release a %lo-ke pro-ess epen in" on the +al(e of the semaphore (pon 'hi-h it is 'aitin". .efinition of a semaphore: & semaphore$ s$ is a ata str(-t(re -onsistin" of an inte"er an a .(e(e. & semaphore$ s$ inte"er ma# %e initialiFe to a nonne"ati+e +al(e. The 'ait1s2 operation e-rements the +al(e of the semaphore inte"er. If the semaphore +al(e %e-omes ne"ati+e$ the pro-ess 7 -allin" the 'ait is %lo-ke an "oes on the .(e(e of the semaphore. 7 is sai to %e 'aitin" on the semaphore s. The si"nal operation in-rements the semaphore inte"er? if the +al(e is not positi+e$ then a pro-ess %lo-ke in the .(e(e of this semaphore is release . =hy it wor/s: the si"nal1s2 an 'ait1s2 operations are atomic. These operations -annot %e interr(pte an are -onsi ere in i+isi%le. 5elo' are the spe-ifi-ations 1 es-ription2 of 'ait an si"nal (sin" C pse( o-o e.

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 11

.efinition of a general $emaphore -rimitives from $tallings &igure 8.>.

struct semaphore E int -o(nt? // +al(e of semaphore .(e(eT#pe .(e(e? //list of pro-esses 'aitin" on this semaphore A s? // s is the e-lare semaphore +aria%le +oi 'ait1s2 E s.-o(nt))? // e-rement semaphore +al(e first if :s.-o(nt I 02 E // test for ne"ati+e pla-e this pro-ess in s..(e(e? %lo-k this pro-ess? A A +oi si"nal1s2 E s.-o(ntJJ? // e-rement semaphore +al(e first if :s.-o(nt 02 E // test for non)positi+e remo+e a pro-ess 7 from s..(e(e? pla-e pro-ess 7 on rea # list? A A 5ow ?eneral $emaphore are @sed: There are t'o -ommon t#pes of "eneral semaphores/ mutual exclusion semaphores -ontrollin" of a -riti-al se-tion$ an counting semaphores -ontrollin" the a-tion of pro-esses that a--ess a limited renewal resource s(-h as rea in" an 'ritin" a finite memor# %(ffer. Mutual Exclusion $emaphores S(-h semaphores are -ommonl# referre to as m(tex semaphores. The m(tex -an %e a "eneral semaphore initialiFe to a one 112. ;hen the +al(e is a 1$ the oor is open an an# pro-ess -an enter its -riti-al se-tion. The first pro-ess to enter 'ill o a 'ait1m(tex2 an -a(se the m(tex to e-rement to a 0 %efore enterin". The next pro-ess tr#in" to enter 'ill also o a 'ait1m(tex2 -a(sin" the m(tex to e-rement to a 1 an 'ill %lo-k in the .(e(e. &n# other pro-esses tr#in" to enter 'ill also %lo-k on 'ait after f(rther e-rementin" the semaphore. &ll s(-h %lo-ke pro-esses are a e to the m(tex .(e(e. ;hen the pro-ess in the -riti-al se-tion exits$ it 'ill o a
CS350 6all 2003 7rin-iples of -on-(rren-# pa"e 12

si"nal1m(tex2 an this 'ill free (p one of the %lo-ke pro-esses in the .(e(e an also in-rement the semaphore %# one. If the semaphore is ne"ati+e$ is a%sol(te +al(e is the n(m%er of pro-esses 'aitin" on this semaphore. Counting $emaphores This case is best described by the bounded buffer example. &ss(me that the %(ffer is a -ir-(lar arra# of$ sa#$ 10 stora"e -ells. Ket a semaphore -alle (n(se %e the -o(nt of the n(m%er of (n(se slots in the %(ffer$ an another semaphore -alle (se %e the -o(nt of the n(m%er of (se slots in the %(ffer. unused is initialiAed to ,( an used is initialiAed to (. La-h time the an item is pro (-e in the %(ffer (n(se is e-remente %# 1 an (se is in-remente %# 1. La-h time an item is -ons(me from the %(ffer$ (se is e-remente %# one an (n(se is in-remente %# 1. De-rementin" is one %# oin" a 'ait on the semaphore$ an in-rementin" is one %# in-rementin" the semaphore. If the 'aits are one %efore a--essin" the %(ffer 1-riti-al se-tion2$ then the pro (-er 1'aitin" on (n(se 2 'ill %lo-k if (n(se is Fero 1%(ffer f(ll2$ an -ons(mer 1'aitin" on (se 2 'ill %lo-k if (se is Fero 1%(ffer empt#2. si"nals are one after a--essin" the %(ffer an 'hen the pro (-er si"nals on (se $ a %lo-ke -ons(mer 'ill %e release an the (se -o(nt 'ill %e in-remente . The -ons(mer 'ill si"nal on (n(se after a--essin" the %(ffer 1-ons(min"2 an a %lo-ke pro (-er 'ill %e release an the (n(se -o(nt 'ill %e in-remente . Mnless the %(ffer is f(ll or empt#$ it is possi%le for %oth the pro (-er an -ons(mer to %e in the -riti-al se-tion at the same time. M(t(al ex-l(sion -an also %e a-hie+e %# (sin" a m(t(al ex-l(sion semaphore as a%o+e. See the %o(n e %(ffer example %elo'.

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 13

1inary $emaphores 5inar# Semaphores ha+e a +al(e 'hi-h -an onl# +ar# %et'een 0 an 1. If the +al(e is 1 the 'ait 'ill not %lo-k. If the +al(e is 0$ the 'ait 'ill %lo-k an the %lo-ke pro-ess 'ill %e .(e(e on the semaphore. The si"nal opertation 'ill set the +al(e to 1 if the .(e(e is empt#$ other'ise it 'ill lea+e it at 0. The implementation is "i+en %elo'/ struct binaryBsemaphore E en(m 1Fero$ one2 +al(e? .(e(eT#pe .(e(e? A void wait1:binaryBsemaphore s; E
if 1s.+al(e >>12 s.+al(e > 0?
// Lock the door & let process enter CS

else E pla-e this pro-ess in s..(e(e? %lo-k this pro-ess? A


A
// no indication of size of queue ... compare to general semaphore // wait unconditionally leaves b-semaphore value at 0

void signal1:binaryBsemaphore s; E
if 1s..(e(e is empt#2 s.+al(e > 1?

//s.value==0 is necessary but not sufficient // condition for empty queue

else E remo+e a pro-ess from s..(e(e? pla-e this pro-ess in the rea # .(e(e? A
A
// if only 1 proc in queue, leave value at 0, since moved proc will go to CS

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 1*

%n example on how mutual exclusion is achieved with a semaphore: // pse( o-o e for pro-ess i of n pro-esses semaphore m(tex > 1? void m(t(al8ex-l(sion1i2 E while 112 E 'ait1m(tex2? I -riti-al se-tion N? si"nal1m(tex2? A I remain er se-tionN? A Example of three processes using general or !counting# semaphores and a binary semaphore based on fig 8.,( $tallings :for binary $emaphores;:

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 15

The producer*consumer problem: // %o(n e %(ffer? int $3CE ** siFe of %(ffer semaphore m(tex > 1? // this is onl# pse( o-o e$ initialiFin" semaphore (se > 0? // a semaphore is a-t(all# a s#stem -all$ semaphore (n(se > SIOL? //Pote/ // (se means the n(m%er of o--(pie slots in %(ffer // (n(se means the n(m%er of empt# slots in %(ffer void pro (-er12 E while 112 E pro (-e)item12? // non -riti-al 1remain er2 se-tion 'ait1(n(se 2? 'ait1m(tex2? appen )item12? // -riti-al se-tion si"nal1m(tex2? si"nal1(se 2? A A void -ons(mer12? E while 112 E 'ait1(se 2? 'ait1m(tex2? take12? // -riti-al se-tion si"nal1m(tex2? si"nal1(n(se 2? -ons(me12? // non -riti-al 1remain er2 se-tion A A

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 13

% bounded buffer scenario for a buffer of siAe D: In this example m(t(al ex-l(sion 'ill %e i"nore $ an onl# the -o(ntin" semaphores 'ill %e ill(strate :producer starts first;. producer event initial +al(e 'ait1(n(se 2 si"nal1(se 'ait1(n(se 2 si"nal1(se 2 'ait1(n(se 2 si"nal1(se 2 'ait1(n(se 2 %lo-ke in rea # .(e(e in rea # .(e(e in rea # .(e(e si"nal1(se 2 'ait1(n(se 2 si"nal1(se 2 in rea # .(e(e in rea # .(e(e in rea # .(e(e in rea # .(e(e in rea # .(e(e in rea # .(e(e in rea # .(e(e 'ait1(n(se 2 unused semaphore 3 2 2 1 1 0 0 )1 )1 0 0 1 1 0 0 0 1 1 2 2 3 3 2 used semaphore 0 0 1 1 2 2 3 3 2 2 1 1 2 2 3 2 2 1 1 0 0 )1 0 consumer comments event in rea # .(e(e$ .(e(e empt# in rea # .(e(e in rea # .(e(e in rea # .(e(e in rea # .(e(e in rea # .(e(e in rea # .(e(e$ .(e(e f(ll in rea # .(e(e$ pro (-er %lo-ks 'ait1(se 2 si"nal1(n(se 2 'ait1(se 2 si"nal1(n(se 2 in rea # .(e(e in rea # .(e(e in rea # .(e(e$ .(e(e f(ll 'ait1(se 2 si"nal1(n(se 2 'ait1(se 2 si"nal1(n(se 2 'ait1(se 2 si"nal1(n(se 2$ .(e(e empt# 'ait1(se 2$ -ons(mer %lo-ks in rea # .(e(e

CS350

6all 2003

7rin-iples of -on-(rren-#

pa"e 14

Das könnte Ihnen auch gefallen