Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th EditionChapter 5: Process Synchronization... Critical Section Problem Consider system of n processes {p 0 , p 1 ,
Trang 1Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Chapter 5: Process
Synchronization
Trang 2Chapter 5: Process Synchronization
Trang 35.3 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Trang 4 Processes can execute concurrently
May be interrupted at any time, partially completing execution
Trang 55.5 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Producer
while (true) {
/* produce an item in next produced */
while (counter == BUFFER_SIZE) ;
Trang 75.7 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
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
Consider this execution interleaving with “count = 5” initially:
S0: producer execute register1 = counter {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = counter {register2 = 5}
S3: consumer execute register2 = register2 – 1 {register2 = 4}
S4: producer execute counter = register1 {counter = 6 } S5: consumer execute counter = register2 {counter = 4}
Trang 8Critical Section Problem
Consider system of n processes {p 0 , p 1 , … p n1}
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
Trang 95.9 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Critical Section
General structure of process P i
Trang 10Algorithm for Process Pi
do {
while (turn == j);
critical section turn = j;
remainder section } while (true);
Trang 115.11 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Solution to Critical-Section Problem
1. Mutual Exclusion If process P i 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
Trang 12Essentially free of race conditions in kernel mode
Trang 135.13 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
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!
Trang 14Algorithm for Process Pi
remainder section } while (true);
Trang 155.15 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Peterson’s Solution (Cont.)
Trang 175.17 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Solution to Critical-section Problem Using Locks
do {
acquire lock
critical section release lock
remainder section } while (TRUE);
Trang 195.19 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Solution using test_and_set()
Shared Boolean variable lock, initialized to FALSE
Solution:
do {
while (test_and_set(&lock)) ; /* do nothing */
Trang 20compare_and_swap Instruction
Definition:
int compare _and_swap(int *value, int expected, int new_value) {
int temp = *value;
Trang 215.21 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Solution using compare_and_swap
Shared integer “lock” initialized to 0;
Solution:
do {
while (compare_and_swap(&lock, 0, 1) != 0) ; /* do nothing */
Trang 22Bounded-waiting Mutual Exclusion with test_and_set
do { waiting[i] = true;
else waiting[j] = false;
Trang 235.23 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Simplest is mutex lock
Protect a critical section by first acquire() a lock then
Trang 24acquire() and release()
Trang 255.25 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Semaphore
Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities.
}
Definition of the signal() operation
signal(S) { S++;
}
Trang 26wait(synch);
S 2 ;
Trang 275.27 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Trang 28Semaphore Implementation with no Busy waiting
wakeup – remove one of processes in the waiting queue and place it in the ready queue
typedef struct{
int value;
struct process *list;
} semaphore;
Trang 295.29 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
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);
} }
Trang 30Deadlock and Starvation
Priority Inversion – Scheduling problem when lowerpriority process
Trang 315.31 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Classical Problems of Synchronization
Trang 32Bounded-Buffer Problem
n buffers, each can hold one item
Semaphore mutex initialized to the value 1
Semaphore full initialized to the value 0
Semaphore empty initialized to the value n
Trang 335.33 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Bounded Buffer Problem (Cont.)
Trang 34Bounded Buffer Problem (Cont.)
Trang 355.35 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Semaphore rw_mutex initialized to 1
Semaphore mutex initialized to 1
Integer read_count initialized to 0
Trang 36Readers-Writers Problem (Cont.)
Trang 375.37 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Readers-Writers Problem (Cont.)
signal(mutex);
} while (true);
Trang 38
Readers-Writers Problem Variations
Trang 395.39 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Trang 40Dining-Philosophers Problem Algorithm
The structure of Philosopher i:
do { wait (chopstick[i] );
Trang 415.41 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Dining-Philosophers Problem Algorithm (Cont.)
Deadlock handling
Allow at most 4 philosophers to be sitting simultaneously at the table
Allow a philosopher to pick up the forks only if both are available (picking must be done in a critical
section
Use an asymmetric solution an oddnumbered philosopher picks up first the left chopstick and then the right chopstick. Evennumbered philosopher picks
up first the right chopstick and then the left chopstick.
Trang 42Problems with Semaphores
Trang 435.43 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
// shared variable declarations procedure P1 (…) { … }
procedure Pn (…) {……}
Initialization code (…) { … } }
}
Trang 44Schematic view of a Monitor
Trang 455.45 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Condition Variables
condition x, y;
Two operations are allowed on a condition variable:
x.wait() – a process that invokes the operation is suspended until x.signal()
x.signal() – resumes one of processes (if any) that
invoked x.wait()
If no x.wait() on the variable, then it has no effect on the variable
Trang 46Monitor with Condition Variables
Trang 475.47 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Condition Variables Choices
Both Q and P cannot execute in paralel. If Q is resumed, then P must wait
Trang 48Monitor Solution to Dining Philosophers
monitor DiningPhilosophers {
enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5];
void pickup (int i) { state[i] = HUNGRY;
Trang 495.49 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Solution to Dining Philosophers (Cont.)
void test (int i) {
if ((state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) ) { state[i] = EATING ;
self[i].signal () ; }
}
initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING;
} }
Trang 50 Each philosopher i invokes the operations pickup() and
Trang 515.51 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Monitor Implementation Using Semaphores
Variables
semaphore mutex; // (initially = 1) semaphore next; // (initially = 0) int next_count = 0;
Each procedure F will be replaced by
wait(mutex);
… body of F;
…
if (next_count > 0) signal(next)
else signal(mutex);
Mutual exclusion within a monitor is ensured
Trang 52Monitor Implementation – Condition Variables
else signal(mutex);
wait(x_sem);
x_count ;
Trang 535.53 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Monitor Implementation (Cont.)
The operation x.signal can be implemented as:
if (x_count > 0) { next_count++;
signal(x_sem);
wait(next);
next_count ;
}
Trang 54Resuming Processes within a Monitor
Trang 555.55 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Allocate a single resource among competing processes using
priority numbers that specify the maximum time a process plans to use the resource
Trang 56A Monitor to Allocate Single Resource
monitor ResourceAllocator {
boolean busy;
condition x;
void acquire(int time) {
if (busy) x.wait(time);
busy = TRUE;
} void release() { busy = FALSE;
x.signal();
} initialization code() { busy = FALSE;
Trang 575.57 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Trang 595.59 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Dispatcher objects either signaledstate (object available)
or nonsignaled state (thread will block)
Trang 60Linux Synchronization
Linux:
Prior to kernel Version 2.6, disables interrupts to implement short critical sections
Trang 615.61 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Trang 62Alternative Approaches
Transactional Memory
OpenMP
Functional Programming Languages
Trang 635.63 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Trang 64}
The code contained within the #pragma omp critical directive
is treated as a critical section and performed atomically
OpenMP
Trang 655.65 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition
Trang 66End of Chapter 5