Process Synchronization - CH 6&7
Process Synchronization - CH 6&7
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Outline
▪ Background
▪ The Critical-Section Problem
▪ Peterson’s Solution
▪ Hardware Support for Synchronization
▪ Mutex Locks
▪ Semaphores
▪ Monitors
Operating System Concepts – 10th Edition 6.2 Silberschatz, Galvin and Gagne ©2018
Objectives
▪ Describe the critical-section problem and illustrate a race condition
▪ Illustrate hardware solutions to the critical-section problem using
memory barriers, compare-and-swap operations, and atomic variables
▪ Demonstrate how mutex locks, semaphores, monitors, and condition
variables can be used to solve the critical section problem
Operating System Concepts – 10th Edition 6.3 Silberschatz, Galvin and Gagne ©2018
Background
▪ Processes can execute concurrently
• May be interrupted at any time, partially completing execution
▪ Shared memory is a method of IPC.
▪ Concurrent access to shared data may result in data inconsistency
▪ Maintaining data consistency requires mechanisms to ensure the orderly
execution of cooperating processes
Operating System Concepts – 10th Edition 6.4 Silberschatz, Galvin and Gagne ©2018
Shared Memory
userspace
• One process will create an area in RAM which
the other process can access Process 1
• Both processes can access shared memory like
a regular working memory
– Reading/writing is like regular reading/writing
– Fast Shared
• Limitation: Error prone. Needs synchronization memory
between processes
Process 2
Operating System Concepts – 10th Edition 6.5 Silberschatz, Galvin and Gagne ©2018
Motivation Example
Example: Producer-Consumer Problem
▪ producer process produces information that is consumed by a consumer
process
• unbounded-buffer places no practical limit on the size of the buffer
• bounded-buffer assumes that there is a fixed buffer size
Operating System Concepts – 10th Edition 6.6 Silberschatz, Galvin and Gagne ©2018
Bounded Buffer Producer-Consumer
0 1 2 n-1
…
out in
producer in
consumer out
Buffer is circular
Producer Code
while (true) {
//produce an item and put in nextProduced
Operating System Concepts – 10th Edition 6.12 Silberschatz, Galvin and Gagne ©2018
main()
#define BUFFER_SIZE 1000;
int buffer[BUFFER_SIZE];
int counter = 0;
increment() decrement()
while (true) { while (true) {
while (counter == BUFFER_SIZE); while (counter == 0);
buffer [in] = nextProduced; nextConsumed = buffer [out];
in = (in + 1) % BUFFER_SIZE; out = (out + 1) % BUFFER_SIZE;
counter++; counter--;
} }
counter == ?
Motivating Scenario
shared variable
P1 int counter = 5; P2
{ {
* *
* *
counter++ counter--
* *
} }
• Single core
– process 1 and process 2 are executing at the same time but sharing a
single core
1 2 1 2 1 2 1 2
14
Motivating Scenario
shared variable
P1 int counter = 5; P2
{ {
* *
* *
counter++ counter--
* *
} }
15
Race Condition
counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
17
Race Condition
□ We would arrive at this incorrect state because we allowed both processes to
manipulate the variable counter concurrently
□ Race Condition
□ When several processes access and manipulate the same data concurrently and the
outcome of the execution depends on the particular order in which the access takes
place
□ Synchronization
□ To ensure that only one process at a time can be manipulating the same data
Race Conditions
• Race conditions
– A situation where several processes access and manipulate the same data
(critical section)
– The outcome depends on the order in which the access take place
– Prevent race conditions by synchronization
• Ensure only one process at a time manipulates the critical data
{
*
*
counter++ critical section
*
No more than one process should
}
execute in critical section at a time
19
Race Conditions in Multicore
shared variable
P1 int counter = 5; P2
{ {
* *
* *
counter++ counter--
* *
} }
• Multi core
– Process 1 and process 2 are executing at the same time on different cores
1
2
CPU usage wrt time
20
Critical Section Problem
▪ 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, may follow critical section with exit section, then remainder
section
Operating System Concepts – 10th Edition 6.22 Silberschatz, Galvin and Gagne ©2018
The Critical-Section Problem
do {
entry section
critical section
exit section
remainder section
} while (TRUE);
Critical-Section Problem (Cont.)
Requirements for solution to critical-section problem
Operating System Concepts – 10th Edition 6.24 Silberschatz, Galvin and Gagne ©2018
Three Requirements for a Solution
Operating System Concepts – 10th Edition 6.25 Silberschatz, Galvin and Gagne ©2018
Solutions to Critical-Section Problem
□ Software-based solutions
□ Hardware-based solutions
□ Operating system solution (semaphore)
□ Programming languages solution (monitor)
Software-based solutions
□ Unfortunately, there are no guarantees that software-
based solution work correctly on modern architectures
□ Because of the way modern architectures perform basic machine-language
instructions, such as load and store
□ We present theses solutions because
□ They provides a good algorithmic description of solving the
critical-section problem
□ Illustrates some of the complexities involved in designing
software that addresses the requirements
Turn-based Solution
□ Assumptions
□ There are only two processes: P0 and P1
□ The LOAD and STORE instructions are atomic; that is, cannot be
interrupted
□ The two processes share a variable turn
□ int turn;
□ The variable turn indicates whose turn it is to enter the
critical section
Turn-based Solution
□ Algorithm for process Pi
do {
□ Problem
□ What happened if a process wants to enter the critical section again,
before the other one needs to enter?
□ The solution does not meet the Progress requirement
Correctness of the Software Solution
Operating System Concepts – 10th Edition 6.30 Silberschatz, Galvin and Gagne ©2018
Flag-based Solution
□ Problem
□ What happened if both processes want to enter the critical section at
the same time, and both set their flags true, before entering the
critical section
□ The solution does not meet the Progress requirement.
Peterson’s Solution
□ Algorithm for process Pi
do {
flag[i] = TRUE;
turn = j;
while ( flag[j] && turn == j );
critical section
flag[i] = FALSE;
remainder section
} while (TRUE);
□ Provable that all requirements are satisfied but works for only two
processes => Bakery algorithm was proposed, but it was too
complex to check the entry section => hardware solutions
Peterson’s Solution
▪ Two process solution
▪ Assume that the load and store machine-language instructions
are atomic; that is, cannot be interrupted
▪ The two processes share two variables:
• int turn;
• boolean flag[2]
▪ The variable turn indicates whose turn it is to enter the critical section
▪ The flag array is used to indicate if a process is ready to enter the
critical section.
• flag[i] = true implies that process Pi is ready!
Operating System Concepts – 10th Edition 6.34 Silberschatz, Galvin and Gagne ©2018
Correctness of Peterson’s Solution
▪ Provable that the three CS requirement are met:
1. Mutual exclusion is preserved
Pi enters CS only if:
either flag[j] = false or turn = i
2. Progress requirement is satisfied
3. Bounded-waiting requirement is met
Operating System Concepts – 10th Edition 6.35 Silberschatz, Galvin and Gagne ©2018
Peterson’s Solution and Modern Architecture
Operating System Concepts – 10th Edition 6.36 Silberschatz, Galvin and Gagne ©2018
Bakery Algorithm
• Synchronization between N > 2 processes
• By Leslie Lamport
eat
when 196 displayed
http://research.microsoft.com/en-us/um/people/lamport/pubs/bakery.pdf 29
Simplified Bakery Algorithm
• Processes numbered 0 to N-1
• num is an array N integers (initially 0).
– Each entry corresponds to a process
lock(i) {
num[i] = MAX(num[0], num[1], …., num[N-1]) + 1
for(p = 0; p < N; p++) {
while (num[p] != 0 and num[p] < num[i]);
}
}
This is at the doorway!!!
critical section It has to be atomic
to ensure two processes
unlock(i) { do not get the same token
num[i] = 0;
}
https://www.youtube.com/watch?v=3pUScfud9Sg 30
Original Bakery Algorithm
• Without atomic operation assumptions
• Introduce an array of N Booleans: choosing, initially all values False.
lock(i){
choosing[i] = True
num[i] = MAX(num[0], num[1], …., num[N-1]) + 1 doorway
choosing[i] = False
for(p = 0; p < N; p++) {
while (choosing[p]);
while (num[p] != 0 and (num[p], p) < (num[i], i));
}
}
critical section
unlock(i) { Choosing ensures that a process
num[i] = 0; is not at the doorway
}
(a, b) < (c, d) which is equivalent to: (a < c) or ((a == c) and (b < d)) 31
Operating System Concepts – 10th Edition 6.40 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 6.41 Silberschatz, Galvin and Gagne ©2018
Process Synchronization
Hardware Solutions
Operating System Concepts – 10th Edition 6.43 Silberschatz, Galvin and Gagne ©2018
Solution Using Locks
□ Race conditions are prevented by requiring that critical regions be
protected by locks
□ A process must acquire a lock before entering a critical section; it releases
the lock when it exits the critical section
□ Unfortunately, the design of such locks can be quite sophisticated.
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
Hardware-based Solutions
□ Hardware features can make any programming task easier
and improve system efficiency
□ Many systems provide hardware support for critical section
code
□ Disabling interrupts
□ Test and Set instruction
□ Exchange instruction
Disabling interrupts
do {
Disable interrupt
critical section
Enable interrupt
remainder section
} while (TRUE);
Operating System Concepts – 10th Edition 6.47 Silberschatz, Galvin and Gagne ©2018
Test and Set Instruction
□ Modern machines provide special atomic hardware instruction
to test and modify the content of a word atomically.
□ that is, as one uninterruptible unit (atomic)
The test_and_set Instruction
▪ Definition
boolean test_and_set(boolean *target)
{
boolean rv = *target;
*target = true;
return rv:
}
▪ Properties
• Executed atomically (uninterruptible unit)
• Returns the original value of passed parameter
• Set the new value of passed parameter to true
Operating System Concepts – 10th Edition 6.49 Silberschatz, Galvin and Gagne ©2018
Test and Set Instruction
□ Algorithm for process Pi
boolean lock = FALSE; //shared variable between all processes
do {
while (TestAndSet (&lock) ) ;
critical section
lock = FALSE;
remainder section
} while (TRUE); boolean TestAndSet(boolean *target)
{
boolean rv = *target;
*target = true;
return rv:
}
lock = FALSE;
remainder section
} while (TRUE);
A Bounded-waiting Solution
□ Hardware-based solutions do not satisfy the bounded-waiting requirement
do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key)
key = TestAndSet(&lock);
waiting[i] = FALSE;
critical section
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
remainder section
} while (TRUE);
Semaphore
□ A synchronization tool which is provided by OS
□ wait() or P()
□ signal() or V()
wait(S) { signal(S) {
while (S <=0 ); S++;
S--; }
}
Mutual Exclusion using Semaphores
wait(semaphore S) { signal(semaphore S) {
S.value --; S.value ++;
if (S.value < 0) { if (S.value <= 0) {
add this process to S.queue; remove a process P from S.queue;
block(); wakeup(P);
} }
} }
Semaphore Implementation
Semaphore Implementation
□ Must guarantee that no two processes can execute wait() and signal()
on the same semaphore at the same time
□ Thus, implementation becomes the critical section problem where the
wait and signal code are placed in the critical section
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
… …
signal (S); signal (Q);
signal (Q); signal (S);
□ Examples
□ Producer-Consumer (Bounded-Buffer) Problem
□ Readers and Writers Problem
□ Dining-Philosophers Problem
Producer-Consumer Problem
□ N buffers, each can hold one item
□ Semaphore mutex provides mutual exclusion for accesses to the buffer
pool, initialized to the value 1
Writer: Reader:
do { do {
wait (wrt); wait (mutex);
//writing is performed readcount ++;
signal (wrt); if (readcount == 1)
} while (TRUE); wait (wrt);
signal (mutex);
// reading is performed
wait (mutex);
readcount--;
if (readcount == 0)
signal (wrt);
signal (mutex);
} while (TRUE);
The Dining-Philosophers Problem
□ Problem
□ Deadlock
□ Suppose that all five philosophers become hungry simultaneously
□ Solutions
□ Allow at most four philosophers to sit around the table
□ Allow a philosopher to pick up her chopsticks only if both chopsticks are
available
□ Use an asymmetric solution
■ An odd philosopher picks up first her left chopstick
■ An even philosopher picks up first her right chopstick
Problems with Semaphores
□ Solution
□ Monitors: A high-level abstraction that provides a convenient and
effective mechanism for process synchronization
Monitors
□ The monitor is a programming-language construct that provides
equivalent functionality to that of semaphores and that is easier to control.
initialization_code() {
count = in = out = 0;
}
}
Producer-Consumer with Monitors
monitor ProducerConsumer pc;
Producer:
do {
// produce an item in nextp
pc.produce(nextp);
} while (TRUE);
Consumer:
do {
nextc = pc.consume();
// consume the item in nextc
} while (TRUE);
Dining-Philosophers with Monitors
Allow a philosopher to pick up her chopsticks only if both chopsticks are available
monitor dp {
enum {THINKING, HUNGRY, EATING} state[5];
condition self[5];
void test(int i) {
if ((state [(i + 4) % 5] != EATING) &&
(state [i] == HUNGRY) &&
(state [(i + 1) % 5] != EATING)) {
state[i] = EATING;
self[i].signal();
}
}
Dining-Philosophers with Monitors
initialization-code () {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
} // end monitor dp
philosopher i:
do {
…
dp.pickup(i);
…
dp.putdown(i);
} while (TRUE);
Condition Variables Choices
□ Options include
□ Signal and wait (Hoare) – P immediately leaves the monitor
(blocked), Q is resumed
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018