Professional Documents
Culture Documents
Untitled
Untitled
Chapter 6:
Objectives
"
❖ Discuss basic concepts related to concurrency, such as:
■ Race condition
■ Mutual exclusion
■ Critical-section problem
• Requirements for critical-section problem solutions
gesoursse
Operating Systems - Dr. Abdulbaset Gaddah ,,,,,, ..., ~ need synctonization 1
Multiple Processes
'
'-...,/
3
I
Principles of Concurrency
❖ Concurrency is a concept where multiple processes or t hreads ru n
simultaneously in uniprocessor QLmulti processor systems
❖ Interleaving and overlapping execution can be viewed as examples
of concurrent processing, and both present the same problems
❖ the relative speed of execution of processes f annot be predicted}
■ Depends on activities of other processes,
■ The way the OS handles interrupts, and
■ Scheduling policies of t he OS
Process 1 Process 1
Process 2 Process 2
Process 3 ~1'/,~m::;J
1/ _ __
(A) Interleavi ng (multlprogrammin 11processor) (B ) lni erl eaving & overlapping (rmultiprocessin
hot mote
Operati ng Syste ms - Dr. .Abdulbaset Gadda h 4
Race condition
- - ----------
ls JIJ fr
{' ~~
❖ Race condition occurs when multiple processes (or threads) access
and manipulate (read and write) the same data items concurrently
❖ The final result depends on the execut1
ion order of the processes
■ The output depends on who finishes t he race last
switch t
beforewrite
Th
T e s'>1Jt1c.
lit. eals w
ync d~,-..,\s ith cCrH:-ic.-..,
wi\.ti ritical sr,c..1.f
ectioann o011b
nly
sh
harre
es
Aq-._,sdata ~ J,.~
~'-ii' h .a_.&11 m
"
ss-~
e
The Cr1tica I Section Problem
•!{Critical sectionl- a section of code within a rocess that mani ulates
shared data or resource, n ,t cannot be executed concurren
❖
For the critical section problem solution:
entry sectiin
,,
,.
,r
,'
,
The protocol
for ME
L o
frequest to ente~ its critical section and before that request is granted
10
r
Operating Systems - Dr. Abdulbaset Gaddah
11
Algorithm 1 - Incorrect
int turn = i; // shared control variable
Pi: Pj:
while (t " != i) ; /* busy wait*/ while u n != j);
CS; C;
turn = j; turn= i;
RS; RS;
■ E er enter it
❖ Bo.u I mg v10 a e - suppose o process terminates nile its
A~
Operating Systems • Dr. .Abdulbaset Gaddah
----- ~
12
12
Pi: Pj:
flag [i] = ri.te; /* wish to enter cs */ flag[j] :;,{rue;
while (flag[j]), · /* busy wait*/ wh ·1~ag [i]) ;
CS; S;
flag[i] = false; */ flag[j] = false;
RS; RS;
13
Peterson's Algorithm
• flag [ i] =
a
■ f l a g is used to indicate if a process isffi!aciy}to enter the critical section
true implies that process Pi is ready!
14
I turn = j; I I turn = i; I
I wh.ile (flag[j] && turn == j); I ~ while (flag[i]_ && _ turn == i); I
L - - - - - - - - - - - - - - - - - _I
CRITICAL SECTION CRITICAL SECTION
flag[i] = false;
Exit Section J flag[j] = false;
Exit Section J
REMAINDER SECTION REMAINDER SECTION
,v
Operating Systems - Dr: Abdulbaset Gaddah 15
15
Proof of Correctness
\.. , /
16
17
'-V
Operating Systems - Dr. .Abdulbaset Gaddah 18
18
do (
acquire lock
c ri t i cal section
release lock
remainder section
whi l e (TRUE);
19
'-V
Operating Systems - Dr. .Abdulbaset Gaddah 20
20
21
~~ - - -
~
I .J~t•-.,
.,-, ~ C...
22
IN Any 24
key = TRUE; \
while (key == TRUE)
Swap ( &loclc, &key ) ;
1J.a') t. '/
~
•
ci;itical sectiori
lock= FALSE;
remaindei; section
} wh il e ( TRUE ) ;
23
❖ Starvation is possible
■
I :;}
J-
As the selection of the process who will enter crit ical section is arbitrary.
I_,.';
It may happen that some processes cou ld indefinitely be denied access
❖ Deadlock is possible - consider this scenario on a single-CPU system
""- Process Pl executes the special instruction and enters its CS
'--- Pl is then interrupted to give the CPU to P2, which has higher priority
- - . Now if P2 attempts to use the same resource as Pl, it will be denied due
to the ME mechanism. Thus, it will go into a busy waiting loop
....._ However1 Pl will never be dispatched because it is of lower priority than
another ready process, P2
'-V
Operating Systems - Dr. Abdu lbaset Gaddah 24
24
wait (S) {
signal (S)
whi l e (S <= 0) ; .
s-- ; .... '' ..,. S+--+ ;
'
' -r
------------i_ ~
--J,.., .,,..,.
All the modifications (and testi ng ) to the integer value
of the semaphore must be executed indivisibly r,v
Operating Systems - Dr: Abdulbaset Gaddah 25
25
Process Pi:
do {
wait( S );
cr i t i cal s e c ti on
signal( S );
remainder section
wh i l e (T RUE ) ;
26
27
Semaphores Usage
"
'-. /
,,
A common semaphore synch, init ialized to 0
~
I
-
Process Pl: Process P2:
wa i t (s ync h) ; \
'
S1 ;
❖
. P2 will execute 52 only after Pl has invoked s i g n a l (s y n c )
- (
'- V
Operati ng Systems - Dr. .Abdulbaset Gaddah 28
2a •ts,'•
S 1n<'}..,''
15211
~rs,.,
525s,,5
5,5 5ji..,
2 ·- _
Operating Systems - Dr. Abdulbaset Gaddah 14
Pitfall: Semaphores Implementation
'
\...,/
29
Blocking in Semaphores
❖ To overcome the need for busy waiting. Each semaphore has an
associated queue of processes
■ When wait(S) is called by a process:
• if 5 was "available" (>0), decrement 5 by 1 and let process continue
• if S was' . < =0 rather · · _place
process into a waiting queue associa
• The CPU scheduler then sellects another process to execute
■ ea t
When signal (S) is called by a process:
• if no processes are waiting into the associated queue, increment S by 1
• if pmcesses are waiting into the associated queue, unblock qne (change its
state from waiting to ready and place it in the ready queue)
• The CPU may or may not let the "signaHng" process continue executing,
depending on the CPU-scheduling algorithm
30
®
©~1 ------lffi----~ ~-----~•
Processcir
L IIlIIBkttl;:e{I i'Jllt,1.1 ,-
P } - "R('Jldf
Si>ma.J)l,orf,!
l lcloJB~
flTit'IH!
L 1I I I I ;~ I !ID!BIAJ
Blod,m queue Sew.,phol'e Ready qneae
,© l'.J'(H."t'S!il'.DI'
....-----4[IJ....--------,
0
l'rort'.5~1)1"
L I IB!A)c)
Blodrecl. q11eue
[?TT----+ I I I I
SrnrnplloL·e
J
Rt:idy gnt:Ili'
~ I ©
------.
J
Prnm,or
LIIIIBI I 1.-Jd-..J L ~D
A -<,+ . . .
>•-1 J
fl!(l('kt•Ll 111.K'11.@' Sl'JIJ:.tltlJllll" R t.'lld~· (!1U!'1l'
l l) 8 li1.l~ll)lc
©
and B to ready
t.. t...;;'"~' S•~·· ..,,.~
L 1 1 1 1 1
Bt;,~e~ ~ll<ll<
~ Se10aph01~
set
I
Rt.1~r ~am queueHere processes wA, B,-..{and-1:.. C depend
IBIA) c~r»1:,v,
31
Semaphore's Structure
"
'-. /
- - - - -- - -
❖ Hence, a semaphore is defined as a "C" structure:
typedef struct {
int va lue ;
struct process *list;
} semaphore S;
32
33
,v
Operating Systems - Dr. Abdulbaset Gaddah 34
34
35
'-V
Operating Systems - Dr. .Abdulbaset Gaddah 36
36
❖ A producer process is generating data and placing these in a bulk[ ' e S e>Ut"c:
c~
❖ A consumer process is taking items out of the buffer one at time
❖ We need a buffer to hold items that are produced and eventually
consumed
-
❖ Only one producer or consumer may access the buffer at any one
time
❖ A common paradigm for cooperating processes
❖ The Problem::
........__ ensure that the producer can't add data into full buffer and consumer
can't remove data from an empty buffer
r
Operating Systems - Dr: Abdulbaset Gaddah
37
0 1 2 3 4
I I I
b[l] b[2] b[3] 1I~[4] I I. . .
b[5]
r
out
r
in
38
(
,v
Operating Systems - Dr. Abdu lbaset Gaddah 39
39
L-
append(v): take():
b[in] - v; w ~ b [out] ;
in++ ; out++ ;
return w;
- (
'- V
Operating Systems - Dr; .Abdu lbaset Gaddah 40
40
41
do { Producet do l Consumer
produce (v); wait{N) ;
wait (El; wait(S) ;
wait{S); w = tak e O ;
append {vl ; signal{S) ;
signal{S) ; signal (E) ;
signal {N) ; consume {w) ;
) while [ TRUE ) ; l while (TRUE);
append(v): take():
b[ i n ] - v; w ~ b [out] ;
in= (in+l) mod k; out= {out+l) mod k ;
return w;
(
'- V
Operating Systems - Dr. .Abdulbaset Gaddah 42
42
43
44
do { Process Pi
// think
wait (T) ;
wait(chopstick[i ] );
wait (chopstick[( i +l) % 5]) ;
// eat
signal(chopstick [ i]);
signa l (chopstick [ (i+l) % 5 ]);
sign a l (T) ;
wh i l e (TRUE) ;
45
......, ""St Usage must be correct in all the processes ( correct order, correct
variables, no omissions)
'-.:. One bad (or malicious) process can fail the entire collection of
processes
'-1--'
Operating Systems - Dr. Abdulbaset Gaddah 46
46
47
THANK YOU
48