Chapter 2 Part IV Process Synchronization
Chapter 2 Part IV Process Synchronization
4)
( s ec
r 2
e
hapt
C
es s ion
ro c z at
p roni
n c h
s y
Sem II, 2011...IT3106... Operating Systems.. 1
.G5-G8
2.4 Process Synchronization
o Cooperating Processes(revision)
o Problem of concurrent execution
o The Critical-Section Problem
o Peterson’s Solution
o Synchronization Hardware
o Semaphores
o Monitors
o Synchronization Examples
At a micro level, the following scenario could occur using this code:
TO; Producer Execute register1 = counter register1 = 5
T1; Producer Execute register1 = register1 + 1 register1 = 6
T2; Consumer Execute register2 = counter register2 = 5
T3; Consumer Execute register2 = register2 – 1 register2 = 4
T4; Producer Execute counter = register1 counter = 6
T5; Consumer Execute counter = register2 counter = 4
executed?
Entry Section
do {
while ( turn != i );
/* critical section */ Critical Section
Disadvantages
Busy-waiting consumes processor time
Starvation is possible when a process leaves a critical section
and more than one process is waiting.
Deadlock(explain why?)
1. Disabling interrupts:- here each process will disable all interrupts just
after entering its critical region and re-enable them just before leaving
it. Disabling Interrupts: Works for the Uni-Processor case
only. WHY?
Are the three Critical Section
Process Pi:
Thus, once a process has
repeat
disabled interrupts, it can disable interrupts
examine and update the shared critical section
enable interrupts
memory without fear that any remainder section
other process will intervene. forever
Sem II, 2011...IT3106... Operating Systems...G5-G8 19
Mutual Exclusion with busy waiting(CONTD)
n
ents l Sectio
Process 0 Process 1
?
Met
while (TRUE){ while (TRUE) {
ca
Requ ree Criti
while(turn != 0) ; while(turn != 1);
critical_region(); critical_region();
irem
th
turn = 1; turn = 0;
the
noncritical_region(); noncritical_region();
Are
} } Systems..
Sem II, 2011...IT3106... Operating
.G5-G8
24
Peterson’s Solution
CRITICAL SECTION
t? o n
Me ecti
nts cal S
flag[i] = FALSE;
ire e Criti
me
Re thre
REMAINDER SECTION
qu
the
Are
}while(1);
P1: P2:
statement 1; wait ( synch );
signal ( synch ); statement 2;
• Bounded-Buffer Problem
• Readers and Writers Problem Reading
Assignment
• Dining-Philosophers Problem
Read About
1. What problem is related to each of the above issues?
2. How the problem can be alleviated?
X X X
writer reader writer reader
reader
writer reader
reader
reader
writer
reader reader
Synchronization Examples
(Read section 6.8 on page 217-230)
ng !
eadi
s
are r
c e s
you
ro i o n
hile
f P a t
bts w
d o n i z
En chro
r dou
sk fo
y n
to a
S
ree
Be f
Sem II, 2011...IT3106... Operating Systems...G5-G8 40