Chapter 05
Chapter 05
2
Objectives
3
Background
4
Producer
while (true) {
/* produce an item in next produced */
while (counter == BUFFER_SIZE); /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
5
Consumer
while (true) {
while (counter == 0) ; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
6
Race Condition
• counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
• counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
7
Critical Section Problem
8
Critical Section
9
Algorithm for Process Pi
do {
critical section
turn = j;
remainder section
} while (true);
10
Solution to Critical-Section Problem
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
11
Critical-Section Handling in OS
12
Peterson’s Solution
• Good algorithmic description of solving the problem
• 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!
13
Algorithm for Process Pi
do {
flag[i] = true;
turn = j;
while (flag[j] && turn = = j);
critical section
flag[i] = false;
remainder section
} while (true);
14
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag T Turn = 0
15
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag T Turn = 1
16
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag T T Turn = 1
17
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag T T Turn = 0
18
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag T T Turn = 0
19
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag T T Turn = 0
20
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag T T Turn = 0
21
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag F T Turn = 0
22
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag F T Turn = 0
23
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag F T Turn = 0
24
Algorithm for Process Pi
Process 1
Process 0
do {
do {
flag[1] = true;
flag[0] = true;
turn = 0;
turn = 1;
while (flag[0] && turn = = 0);
while (flag[1] && turn = = 1);
critical section
critical section
flag[1] = false;
flag[0] = false;
remainder section
remainder section
} while (true);
} while (true);
Flag F F Turn = 0
25
Peterson’s Solution (Cont.)
26
Synchronization Hardware
• Many systems provide hardware support for implementing the critical section code.
• All solutions below based on idea of locking
• Protecting critical regions via locks
• Uniprocessors – could disable interrupts
• Currently running code would execute without preemption
• Generally too inefficient on multiprocessor systems
• Operating systems using this not broadly scalable
• Modern machines provide special atomic hardware instructions
• Atomic = non-interruptible
• Either test memory word and set value
• Or swap contents of two memory words
27
Solution to Critical-section Problem Using Locks
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
28
test_and_set Instruction
Definition:
1.Executed atomically
2.Returns the original value of passed parameter
3.Set the new value of passed parameter to “TRUE”.
29
Solution using test_and_set()
• Shared Boolean variable lock, initialized to FALSE
• Solution:
do {
while (test_and_set(&lock)); /* do nothing */
/* critical section */
lock = false;
/* remainder section */
} while (true);
30
compare_and_swap Instruction
Definition:
31
Solution using compare_and_swap
• Shared integer “lock” initialized to 0;
• Solution:
do {
while (compare_and_swap(&lock, 0, 1) != 0); /* do nothing */
/* critical section */
lock = 0;
/* remainder section */
} while (true);
32
Bounded-waiting Mutual Exclusion with test_and_set
do {
waiting[i] = true;
key = true;
while (waiting[i] && key)
key = test_and_set(&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);
33
Mutex Locks
• Previous solutions are complicated and generally inaccessible to application programmers
• OS designers build software tools to solve critical section problem
• Simplest is mutex lock
• Protect a critical section by first acquire() a lock then release() the lock
• Boolean variable indicating if lock is available or not
• Calls to acquire() and release() must be atomic
• Usually implemented via hardware atomic instructions
• But this solution requires busy waiting
• This lock therefore called a spinlock
34
acquire() and release()
acquire() {
while (!available)
; /* busy wait */
available = false;;
}
release() {
available = true;
}
do {
acquire lock
critical section
release lock
remainder section
} while (true);
35
Semaphore
• Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities.
• Semaphore S – integer variable
• Can only be accessed via two indivisible (atomic) operations
• wait() and signal()
• Originally called P() and V()
• Definition of the wait() operation
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
• Definition of the signal() operation
signal(S) {
S++;
}
36
Semaphore Usage
• Counting semaphore – integer value can range over an unrestricted domain
• Binary semaphore – integer value can range only between 0 and 1
• Same as a mutex lock
• Can solve various synchronization problems
• Consider P1 and P2 that require S1 to happen before S2
Create a semaphore “synch” initialized to 0
P1:
S 1;
signal(synch);
P2:
wait(synch) ;
S 2;
• Can implement a counting semaphore S as a binary semaphore
37
Semaphore Implementation
• Must guarantee that no two processes can execute the wait() and signal() on the same semaphore
at the same time
• Thus, the implementation becomes the critical section problem where the wait and signal code are placed
in the critical section
• Could now have busy waiting in critical section implementation
• But implementation code is short
• Little busy waiting if critical section rarely occupied
• Note that applications may spend lots of time in critical sections and therefore this is not a good solution
38
Semaphore Implementation with no Busy waiting
39
Implementation with no Busy waiting (Cont.)
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
//add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
//remove a process P from S->list;
wakeup(P);
}
}
40
Deadlock and Starvation
• Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the
waiting processes
• Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);
41
Problems with Semaphores
53
Monitors
• A high-level abstraction that provides a convenient and effective mechanism for process synchronization
• Abstract data type, internal variables only accessible by code within the procedure
• Only one process may be active within the monitor at a time
• But not powerful enough to model some synchronization schemes
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
54
Schematic view of a Monitor
55
Alternative Approaches
• Transactional Memory
• OpenMP
73
Transactional Memory
• A memory transaction is a sequence of read-write operations to memory that are performed atomically.
void update()
{
/* read/write memory */
}
74
OpenMP
• OpenMP is a set of compiler directives and API that support parallel progamming.
The code contained within the #pragma omp critical directive is treated as a critical section and
performed atomically.
75
Functional Programming Languages
• Functional programming languages offer a different paradigm than procedural languages in that they do not
maintain state.
• Variables are treated as immutable and cannot change state once they have been assigned a value.
• There is increasing interest in functional languages such as Erlang and Scala for their approach in handling
data races.
76
Deadlocks
77
System Model
78
Deadlock Characterization
79
Resource-Allocation Graph
A set of vertices V and a set of edges E.
• V is partitioned into two types:
• P = {P1, P2, …, Pn}, the set consisting of all the processes in the system
• R = {R1, R2, …, Rm}, the set consisting of all resource types in the system
80
Resource-Allocation Graph (Cont.)
• Process
• Pi requests instance of Rj
Pi
Rj
• Pi is holding an instance of Rj
Pi
Rj
81
Example of a Resource Allocation Graph
82
Resource Allocation Graph With A Deadlock
83
Graph With A Cycle But No Deadlock
84
Basic Facts
85
Methods for Handling Deadlocks
86