Title: Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization
1Computer Architecture and Operating SystemsCS
3230 Operating System SectionLecture
OS-5Process Synchronization
- Department of Computer Science and Software
Engineering - University of Wisconsin-Platteville
2Outlines
- Critical Section Problem
- Mutual Exclusion
- Mutual Exclusion Algorithms
- Synchronization Hardware
- Semaphores
3Critical Section Problem
- n processes all competing to use some shared data
- Each process has a code segment, called critical
section, in which the shared data is accessed. - Mutual Exclusion (MX) ensure that when one
process is executing in its critical section, no
other process is allowed to execute in its
critical section - MX basic synchronization problem
4MX Hardware Support 1
- A process runs until it invokes an
operating-system service or until it is
interrupted - Interrupt Problem MX violation
- Hardware Solution
- Single Processor
- Guarantee mutual exclusion (only one process can
run at any given time) - Problem Race Condition
- Interrupt Disabling
- Processor is limited in its ability to interleave
programs - Disabling interrupts solve the problem
- Multiprocessor
- disabling interrupts will not guarantee mutual
exclusion
5MX General Solution
- There is a set of threads/processes that switch
between executing the critical section (CS) of
code and non-critical section - Before entering the CS, a thread/process requests
it - MX Solution requirement
- safety at most one thread at a time execute the
CS - liveness a thread requesting the CS is given a
chance to enter it - Liveness violation starvation a requesting
thread is not allowed to enter the CS - deadlock all threads are blocked and cannot
proceed - livelock threads continue to operate but none
could enter the CS
6MX Process Structure
- General structure of process Pi
7MX Algorithm 1
P1 ( ) while (true) while (turn !
1) / do nothing / CS turn
2 non-CS P2 ( ) while (true)
while (turn ! 2) / do nothing
/ CS turn 1 non-CS
- Pre condition only two processes/threads in the
system - Shared variables
- int turn
- OS sets turn to an initial value (1 or 2)
- Advantages
- Safety
- Problems
- Violates liveness
8MX Algorithm 2a
P1 ( ) while (true) while (P2_in_CS
true) / do nothing / P1_in_CS
true CS P1_in_CS false
non-CS P2 ( ) while (true) while
(P1_in_CS true) / do nothing /
P2_in_CS true CS P2_in_CS false
non-CS
- Pre condition only two processes/threads in the
system - Shared variables
- Boolean variables
- P1_in_CS
- P2_in_CS
- Boolean variables initially false
- Advantages
- liveness
- Problems
- Violates Safety
9MX Algorithm 2b
P1 ( ) while (true) P1_in_CS
true while (P2_in_CS true) / do
nothing / CS P1_in_CS false
non-CS P2 ( ) while (true)
P2_in_CS true while (P1_in_CS
true) / do nothing / CS P2_in_CS
false non-CS
- Pre condition only two processes/threads in the
system - Shared variables
- Boolean variables
- P1_in_CS
- P2_in_CS
- Boolean variables initially false
- Advantages
- Safety
- Problems
- Violates liveness
10MX Algorithm 3 (Petersons)
P1 ( ) while (true) P1_in_CS
true turn2 while (P2_in_CS
true turn2) / do nothing /
CS P1_in_CS false non-CS P2 ( )
while (true) P2_in_CS
true turn1 while (P1_in_CS
true turn1) / do nothing
/ CS P2_in_CS false non-CS
- Pre condition only two processes/threads in the
system - Shared variables
- int turninitially turn 0
- Boolean variables
- P1_in_CS
- P2_in_CS
- Boolean variables initially false
- Advantages
- Safety
- liveness
11MX Multiple Processes
- Bakery Algorithm
- MX solution for n processes and m processors
12MX Multiple Processes
- Shared data
- boolean choosingn
- int numbern
- Data structures are initialized to false and 0
respectively
int i / unique process id / while (true)
choosingitrue numberimax(number0,,
numbern-1)1
choosingifalse for (j0 jltn j)
while (choosingj) / do nothing /
while (numberjgt0 (numberjltnumberi
numberjnumberi jlti))
/ do nothing / CS numberi 0
non-CS
- Algorithm
- Before entering its critical section, process
receives a number. - The algorithm always generates numbers in
increasing order - Holder of the smallest number enters the critical
section. - If processes Pi and Pj receive the same number
- if i lt j, then Pi is served first else Pj is
served first.
13MX Hardware Support 2
- Special Machine Instructions
- Test and Set Instruction
- Swap (Exchange) Instruction
- Above instructions represent a MX solution for n
processes and m processors
14MX Test and Set Instruction
boolean TestAndSet (boolean target)
boolean rv target tqrget
true return rv
- Shared data boolean lock false
- Process Pi
- while (true)
- while (TestAndSet(lock))
- / do nothing /
- / Note do nothing also called busy
waiting / - CS
- lock false
- non-CS
-
15MX Swap Instruction
void Swap (boolean a, boolean b) boolean
temp a a b b temp
- Shared data boolean lockfalse
- Each process has its local variable key
- Process Pi
- while (true)
- boolean keytrue
- while (key true)
- Swap(lock,key) // busy waiting
- CS
- lock false
- non-CS
-
16MX Special Machine Instructions
- Advantages
- Applicable to any number of processes on either a
single processor or multiple processors sharing
main memory - It is simple and therefore easy to verify
- It can be used to support multiple critical
sections - Disadvantages
- Busy-waiting consumes processor time
- Starvation is possible when a process leaves a
critical section and more than one process is
waiting. - Deadlock
- If a low priority process has the critical region
and a higher priority process needs, the higher
priority process will obtain the processor to
wait for the critical region
17Semaphores
- Semaphore is a special integer variable that is
used for synchronization - Semaphore supports two atomic operations wait
and signal - The atomicity means that the two operations on
the same semaphore can not overlap - there can be no interruptions while these
operations are being implemented. - if more than one threads try to execute Wait (or
Signal), only one of them will succeed - How semaphore works ?
- The semaphore initialized to 1 or a positive
value - Before entering the critical section, a
process/thread calls wait (semaphore) - After leaving the critical section, a process
/thread calls signal (semaphore)
18Using Semaphores for MX
P1 () while (true) wait(s) / CS
/ signal(s) / non-CS / P2 ()
while (true) wait(s) / CS
/ signal(s) / non-CS /
19Semaphores
- Assume our semaphore is called s
- wait (s) signal (s)
- s s 1 s s 1
- if (s lt 0) if (s lt 0)
- block the thread wake up run
one of - that called wait(s) the waiting threads
- otherwise
- continue into CS
- Note
- Negative semaphore number of blocked threads,
where only one right now in the critical section
20Readers /Writers Problem
int readcount0 semaphore wrt(1),mx(1) writer()
wait(wrt) / perform write
/ signal(wrt) reader() wait(mx) readcoun
t if(readcount1) wait(wrt) signal(mx)
/ perform read / wait(mx) readconut-- if(
readcount0) signal(wrt) signal(mx)
- Readers and writers can access concurrently same
item - writers cannot concurrently writing same item,
but readers can - wrt- semaphore for the writing operation
- writer can get to item if open
- readcount - number of readers wishing to read the
item - mx a semaphore protects manipulation with
readcount - Any writer must wait for all readers
- First reader races with writers
- Last reader signals writers
- Readers can starve writers
- If a writer is writing the item, no reader may
read it