CH 6 Synchronization
CH 6 Synchronization
CH 6 Synchronization
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 6: Synchronization Tools
OBJECTIVES
● To present the concept of process
● 6.1 Background
synchronization.
● 6.2 The Critical-Section Problem
● To introduce the critical-section
● 6.3 Peterson’s Solution problem, whose solutions can be used to
● 6.4 Synchronization Hardware ensure the consistency of shared data
● 6.5 Mutex Locks ● To present both software and hardware
● 6.6 Semaphores solutions of the critical-section problem
● To examine several classical process-
synchronization problems
● To explore several tools that are used to
solve process synchronization problems
Operating System Concepts – 9th Edition 6.2 Silberschatz, Galvin and Gagne ©2013
Operating Systems
Introduction Recall
• Independent process
– A process that does not share data with other processes.
– cannot affect or be affected by the execution of another
process.
• Cooperating process
– can affect or be affected by the execution of another
process.
– processes directly share logical address space (code
& data) or processes share data through files or
Chapter 3
messages.
_______________________________________________________________________________
Silberschatz, A., Galvin, P.B., and Gagne, G. Operating System Concepts (9th Edition). John Wiley & Sons: Asia. (2014) Page 120.
3
6.1 Background
Operating System Concepts – 9th Edition 6.4 Silberschatz, Galvin and Gagne ©2013
6.1 Background
6.5
5
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
6
http://undergraduate.csse.uwa.edu.au/units/CITS2230/handouts/Lecture09/lecture9.pdf
Operating System Concepts – 9th Edition 6.6 Silberschatz, Galvin and Gagne ©2013
7
Critical section
Mutual exclusion
Operating System Concepts – 9th Edition 6.7 Silberschatz, Galvin and Gagne ©2013
Operating Systems
Producer-Consumer Problems
• Several processes work together to complete
common task – Process Cooperation
• Common paradigm for cooperating processes,
producer process produces information that is
consumed by a consumer process.
Examples:
The compiler produces an assembly code which is consumed
by an assembler.
The web server provides HTML files which is consumed by the
Chapter 3
8
Operating Systems
6.1
10
Operating Systems
• Therefore,
Chapter 3
11
Producer Process
while (true) {
/* produce an item in next produced */
Operating System Concepts – 9th Edition 6.12 Silberschatz, Galvin and Gagne ©2013
Consumer Process
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter = counter - 1;
Operating System Concepts – 9th Edition 6.13 Silberschatz, Galvin and Gagne ©2013
Operating Systems
Bounded-Buffer Problems
• Shared Variables
– in, out, counter and buffer[ ]
– Initial values for in, out, counter =0
Buffer [6]
Producer Consumer
Operating System Concepts – 9th Edition 6.15 Silberschatz, Galvin and Gagne ©2013
16
Operating System Concepts – 9th Edition 6.16 Silberschatz, Galvin and Gagne ©2013
Race Condition
● counter = counter + 1 could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
● counter = counter -1 could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
Operating System Concepts – 9th Edition 6.17 Silberschatz, Galvin and Gagne ©2013
Operating Systems
sequence.
19
Race Condition (Cont.)
● How do we solve the race condition?
21
Operating Systems
Ex
tr a
Solution 6.1:
22
6.2 Critical Section Problem
● Part of a program (segment of code for each process)
● Consider system of n processes {P0, P1, … Pn-1}
● Each process has critical section segment of code
● Process may be changing common variables, updating table,
writing file, etc
● When one process in critical section, no other may be in its
critical section
● Critical section problem is to design protocol to solve this.
● Each process must ask permission to enter critical section in entry
section code; it than executes in the critical section; once it
finishes executing in the critical section it enters the exit section
code. The process then enters the remainder section code.
Operating System Concepts – 9th Edition 6.23 Silberschatz, Galvin and Gagne ©2013
General structure of Process Entering the Critical Section
Operating System Concepts – 9th Edition 6.24 Silberschatz, Galvin and Gagne ©2013
Operating Systems
CS – Process Structure
Chapter 3
25
Algorithm for Process Pi
Operating System Concepts – 9th Edition 6.26 Silberschatz, Galvin and Gagne ©2013
Algorithm for Process Pi
Have a variable “turn” to indicate which process is next
do {
Figure 6.2
Operating System Concepts – 9th Edition 6.27 Silberschatz, Galvin and Gagne ©2013
6.2
Requirements for CS solutions
Assumptions:
⚫ Assume that each process executes at a non-zero speed
⚫ No assumption concerning relative speed of the n
processes or #number of CPU.
Mutual Exclusion
The algorithm does satisfy
the three essential criteria
to solve the CS problems. Progress
Bounded Waiting
6.28
28
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Solution to Critical-Section Problem
Must satisfy the following three requirements:
1. Mutual Exclusion - If process Pi is executing in its critical section,
then no other processes can be executing in their critical sections
2. Progress - If no process is executing in its critical section and there
exist some processes that wish to enter their critical section, then the
selection of the processes that will enter the critical section next
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 critical section and before that
request is granted
⚫ Assume that each process executes at a nonzero speed
⚫ No assumption concerning relative speed of the n processes
Operating System Concepts – 9th Edition 6.29 Silberschatz, Galvin and Gagne ©2013
Operating Systems
30
6.3 Software Solution: Peterson’s Algorithm
Operating System Concepts – 9th Edition 6.31 Silberschatz, Galvin and Gagne ©2013
Algorithm for Process Pi
Operating System Concepts – 9th Edition 6.32 Silberschatz, Galvin and Gagne ©2013
Operating Systems
Exercises 1
• Rewrite the Peterson's algorithm for CS solution
for process 0 (P0).
• Rewrite the Peterson's algorithm for CS solution
for process 1 (P1).
P1
Chapter 3
P0
33
Operating Systems
Initially the flags are false. When a process wants to execute it’s critical section, it sets
it’s flag to true and turn as the index of the other process. This means that the
Chapter 3
process wants to execute but it will allow the other process to run first. The process
performs busy waiting until the other process has finished it’s own critical section.
After this the current process enters it’s critical section and adds or removes a
random number from the shared buffer. After completing the critical section, it sets
it’s own flag to false, indication it does not wish to execute anymore.
34
Operating Systems
35
Operating Systems
Exercises 2
• What is the event for each ti if 2 concurrent
processes wishing to enter their CSs.
– Progress
– Bounded Waiting
36
Operating Systems
37
Operating Systems
38
Operating Systems
Peterson’s algorithm :
FALSE TRUE 0 P1
TRUE TRUE 0 P0
Chapter 3
4
TRUE TRUE 1 P1
2
39
Operating Systems
Exercise 3:
Continue from previous example
time flag[0] flag[1] turn Events
t10 TRUE FALSE 1 P0 requests to enter CS0
t11 TRUE TRUE 0 P1 request to enter CS1
t12 TRUE TRUE 0 P0 enters CS0
t13 TRUE TRUE 0 P1 busy waiting in loop
t14 FALSE TRUE 0 P0 executes RS0
t15 FALSE TRUE 0 P1 enters CS1
t16 TRUE TRUE 1 P0 request to enter CS0 3
Exercise 3:
Continue from previous example
time flag[0] flag[1] turn Events
t10 TRUE FALSE 1 P0 requests to enter CS0
t11 TRUE TRUE 0 P1 request to enter CS1
t12 TRUE TRUE 0 P0 enters CS0
t13 TRUE TRUE 0 P1 busy waiting in loop
t14
ProgressFALSE TRUE 0 P0 executes RS0
t15 FALSE TRUE 0 Bounded
P1 enters CS 1
Operating System Concepts – 9th Edition 6.42 Silberschatz, Galvin and Gagne ©2013
Synchronization Hardware
Operating System Concepts – 9th Edition 6.43 Silberschatz, Galvin and Gagne ©2013
Operating Systems
Test-and-Set Instruction
• Test-and-Set is a single indivisible machine instruction
known simply as TS and was introduced by IBM for its
multiprocessing System 360/370 computers.
44
test_and_set Instruction
● Definition:
boolean test_and_set (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
Figure 6.3 The definition of the test_and_set()
instruction
● Properties:
● Executed atomically
● Returns the original value of passed parameter
● Set the new value of passed parameter to “TRUE”.
Operating System Concepts – 9th Edition 6.45 Silberschatz, Galvin and Gagne ©2013
Operating Systems
46
Solution using test_and_set()
● Shared Boolean variable lock, initialized to FALSE
● Each process, wishing to execute CS code:
do {
while (test_and_set(&lock))
; /* do nothing */
/* critical section */
lock = false;
/* remainder section */
} while (true);
Figure 6.4 Mutual exclusion implementation with test_and_set()
Operating System Concepts – 9th Edition 6.47 Silberschatz, Galvin and Gagne ©2013
6.5 Mutex Locks
Operating System Concepts – 9th Edition 6.48 Silberschatz, Galvin and Gagne ©2013
Semaphores
Overview
6.49
49
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Semaphores
Semaphore in OS:
6.51
51
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Semaphores
● Synchronization tool that provides more sophisticated
ways (than Mutex locks) for processes to synchronize
their activities.
● Semaphore S – integer variable
● Can only be accessed via two indivisible (atomic)
operations
● when one process modify semaphore variable S,
then no other process can modify the variable
concurrently mutual exclusion
● wait() is called P()
● and signal() called V()
Operating System Concepts – 9th Edition 6.52 Silberschatz, Galvin and Gagne ©2013
Semaphores
● Definition of the wait() operation
wait(S)
{ while (S <= 0) ; // busy waiting
S = S - 1;
}
(S <= 0 implies busy critical section/region or a process in CS)
(Process calling on wait() operation must wait until S > 0 (+ve value))
Operating System Concepts – 9th Edition 6.53 Silberschatz, Galvin and Gagne ©2013
Types of Semaphores
● Counting semaphore – integer value can range over an
unrestricted domain
Operating System Concepts – 9th Edition 6.54 Silberschatz, Galvin and Gagne ©2013
Semaphore Usage
Can solve various synchronization problems
● A solution to the CS problem.
● Create a semaphore “synch” initialized to 1
wait(synch)
CS
signal(synch);
Operating System Concepts – 9th Edition 6.55 Silberschatz, Galvin and Gagne ©2013
Semaphore Usage
Consider P1 and P2 that require code segment S1 to happen before code
segment S2
Create a semaphore “synch” initialized to 0
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
Operating System Concepts – 9th Edition 6.56 Silberschatz, Galvin and Gagne ©2013
Semaphore Implementation: Busy waiting
● Must guarantee that no two processes can execute the wait()
and signal() on the same semaphore at the same time
Operating System Concepts – 9th Edition 6.57 Silberschatz, Galvin and Gagne ©2013
Semaphore Implementation with no Busy Waiting
Operating System Concepts – 9th Edition 6.58 Silberschatz, Galvin and Gagne ©2013
Operating Systems
Exercise:
Chapter 3
Continue… 59
Chapter 3 Operating Systems
60
Chapter 3 Operating Systems
61
Operating Systems
Example: Producer-Consumer
Initial values: mutex = 1, empty = n, full = 0 Problem
Producer Consumer
do { do {
…. wait(full); //dec full cnt
wait(mutex);
produce an item in nextp
…..
…. remove an item from buffer to nextc
wait(empty); //dec empty cnt ….
wait(mutex); signal(mutex);
…. signal(empty); //inc empty cnt
add nextp to buffer ….
…. consume the item in nextc
signal(mutex); ….
} while (TRUE);
Chapter 3
62
Operating Systems
Exercise:
mutex mutex
full full
63
Operating Systems
Producer : Consumer :
wait() signal() wait() signal()
empty empty
Chapter 3
mutex mutex
full full
64
Operating Systems
wait(empty=5) wait(full=1)
empty=4 full=0
wait(mutex=1) wait(mutex=1)
mutex=0 mutex=0
signal(mutex=0) signal(mutex=0)
mutex=1 mutex=1
signal(full=0) signal(empty=4)
full=1 empty=5
Producer : Consumer :
wait() signal() wait() signal()
empty 4 empty 5
Chapter 3
mutex 0 1 mutex 0 1
full 1 full 0
65
6.6.3 Deadlock and Starvation
● Incorrect use of semaphore operations can produced:
Operating System Concepts – 9th Edition 6.66 Silberschatz, Galvin and Gagne ©2013
Operating Systems
Summary
• Mutual exclusion
– Prevents deadlock
– Maintained with test-and-set, WAIT and
SIGNAL, and semaphores (P, V, and mutex)
67
Operating Systems
EXERCISE (DIY!!!)
Figure 1 shows the implementation of Producer-Consumer using Semaphores.
Supposed the initial values of the buffer size, N=8, mutex=1, empty=6 and full=2.
Complete the value of the appropriate mutex variables in Table below if the following
sequence of order is executed: consumer, consumer, producer. [6 marks]
Consumer
Consumer
Process Wait(empty) Wait(mutex) Signal (mutex) Signal (full)
Chapter 3
Producer
68
End of Chapter 6
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013