Title: Process Synchronization
1Process Synchronization
- Topics
- 1. Background
- 2. The critical-section problem
- 3. Semaphores
- 4. Critical Regions
- 5. Monitors
2Background
- Concurrent access to Data may result in data
inconsistency - To maintain data consistency we should have
orderly execution of co-operating processes - In the bounded buffer example, what happens I we
maintain a counter to keep track of number of
items produced common to producer and consumer
3Bounded Buffer
- Shared Data buffern,in,out,counter
- Producer Process
- repeat
- produce an item in nextp
- while countern wait
- bufferinnextpin(in1)ncounter
- until false
4Bounded Buffer (continued)
- Consumer Process
- repeat
- while counter0 do no-op
- nextcbufferout
- counter-- consume item nextc
- until false
- Statements counter and counter-- have to be
done atomically - no interrupt
5Critical Section Problem
- n processes competing to use shared data
- Each process has a code segment called critical
section in which shared data is accessed - Issue When one process is in critical section,
no other process is in critical section
6Structure of Process
Entry section
Critical section
Exit section
Remainder section until false
7Solution to Critical Section Problem
1. Mutual Exclusion No two processes are in the
critical section at the same time. 2. Progress
If no process is in the critical section and a
process wishes to enter, that process cannot be
postponed indefinitely 3. Bounded waiting A
bound must exist on the number of times that
other processes are allowed to enter their
critical sections after a process has made a
request to enter its cs and that process is
granted.
8Initial Attempts to solve Problem
Only 2 Processes P_0 and P_1 General Structure
of Process P_j (other is P_i) repeat
entry section critical section exit
sectionremainder section until
false Processes may share some common variables
to synchronize their actions.
9Algorithm 1
Shared variables int turn / turnj it P_jth
turn/ Process P_j repeat while (turn ltgt j)
wait critical section turnI remainder
section umtil false
10Algorithm 2
Shared variables boolean flag2
/flagjtrue means jth turn / Process
P_j entry section flagjtrue while (flagi)
wait exit section flagjfalse As before
satisfies mutual exclusion but no progress.
11Algorithm 3
Shared variables flag2 , turn Process
P_j entry section flagjtrue turnj while
(flagi and turni) wait exit section
flagjfalse Satisfies all three requirements-
critical thing to note is turn is a shared
variable.
12Bakery Algorithm
Critical Section for n processors Before
entering the critical section, process receives a
number. Holder of the smallest number enters its
critical section. If processes P_i and P_j
receive the same number, if iltj, then P_i is
served first else p_j is served. Numbering scheme
always generates numbers in non-decreasing order.
13Bakery Algorithm (continued)
We use lexicographic order. (a,b) lt (c,d) is
either alt c or ac blt d. Shared data Boolean
choosingn int numbern they are
initialized to false and 0 resply.
14Bakery Algorithm (continued)
Enter section choosingj true
numberjmax of numbers 1 choosingjfalse
for (k0kltnk) while(choosingk)
wait while(numberk ltgt0) and (numberk,k)
ltnumberj,j) wait Leave Section numberj0
15Synchronization Hardware
Test and modify the content of a word
atomically boolean Test-and-set(boolean
target) Test-and-set target target true
16Mutual Exclusion with Test-and-set
Shared data boolean lock (initially
false) Process P_j enter section while
(Test-and-lock) wait leave section lock
false
17Semaphore
Synchronization tool that does not require busy
wait Semaphore S - integer variable can only be
accessed via two indivisible atomic
operations wait(s) while (s lt0) no-op s
s-1 signal(s) ss1
18Critical section for n processes
Shared Variable semaphore mutex initial value
of mutex1 entersection wait(mutex) leavesectio
n signal(mutex)
19Semaphore Implementation
Semaphore as a struct typedef semaphore struct
int value ProcessList L Two Simple
operations block - suspends the process that
invokes it wakeup(P) resumes the execution of a
blocked process P
20Semaphore Implementation (continued)
Wait(S) S.value-- if (S.valuelt0) add this
process to S.l block Signal(S) S.value if
(S.valuelt0) remove a process from P from S.L
wakeup(P)
21Implementation (continued)
Wait(S) S.value-- if (S.valuelt0) add this
process to S.l block Signal(S) S.value if
(S.valuelt0) remove a process from P from S.L
wakeup(P)
22Semaphore as General Synchronization Tool
Execute B in P_j only after A is executed in
P_i Use semaphore flag initialized to 0 code P_i
A signal(flag) Code P_j wait(flag) B
23Deadlock and Starvation
Deadlock - two or more processes are waiting for
indefinitely for an event that can be caused by
only one of the waiting processes. S and Q are
semaphores. P_0 wait(S)wait(Q)
..signal(S)signal(Q) P_1wait(Q)wait(S)..sign
al(Q)signal(S) Starvation A process may never
be removed from the semaphore queue .
24Two Types of Semaphores
- Counting Semaphore - integer value can range over
an unrestricted domain - Binary Semaphore - integer value can range from 0
to 1 - easier to implement. - Can implement (simulate) a counting semaphore
using a binary semaphore.
25Implementing S as a binary Semaphore
Data Structures binary_semaphore s1,s2,s3 int
c Initialization s3s11 s20 cinitial value
of semaphore
26Implementing S (continued)
Wait operation wait(s3)wait(s1)cc-1 if (clt0)
signal(s1)wait(s2) else signal(s1) signal(s3
) Signal Operation wait(s1) cc1 if (clt0)
signal(s2) signal(s1)
27Classical Problems of Synchronization
Bounded Buffer Problem Readers and writers
Problem Dining Philosopher
28Bounded Buffer Problem
Shared Data String Item String
buffer semaphore full,empty,mutex item
nextp,nextc full0emptynmutex1
29Producer Process
Repeat produce an item in nextp wait(empty)wai
t(mutex) add nextp to buffer signal(mutex)
signal(full) until false
30Consumer Process
Repeat wait(full)wait(mutex) remove an item
from buffer in nextc signal(mutex)
signal(empty) consume an item in nextc until
false
31Readers and Writers Problem
Shared Data Semaphore mutex,wrt(1) int
readcount Writer Process wait(wrt) write
signa(wrt)
32Reader Process
Entersection wait(mutex) readcount if
(readcount1) wait(wrt) signal(mutex) leavesect
ion wait(mutex)readcount- if(reacount0)
signal(wrt) signal(mutex)
33Dining Philosopher Problem
Shared data Semaphore chopstick5(1) Philosoph
er_j entersection wait(chopstickj)
wait(chopstick(j1)5) Leavesection
signal(chopstickj) signal(chopstick(j1)5)