Professional Documents
Culture Documents
GM-3 1
GM-3 1
GM-3 1
Process Synchronization
• Background
• The Critical-Section Problem
• Peterson’s Solution
• Synchronization Hardware
• Semaphores
• Classic Problems of Synchronization
Objectives
• To introduce the critical-section problem, solutions for consistency
of shared data
while (true) {
while (counter == 0) ; // do nothing
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item from the buffer */
}
Race Condition
• counter++ could be implemented as(Producer)
register1 = counter
register1 = register1 + 1
counter = register1
• counter-- could be implemented as (consumer)
register2 = counter
register2 = register2 - 1
count = register2
• Consider this execution interleaving with “counter = 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}
Race Condition :
• Provable that
1. Mutual exclusion is preserved : turn can be either 0 or 1
2. Progress requirement is satisfied :when process i wishes, process j not in CS , Flag[j]
-> false, Process i allowed
3. Bounded-waiting requirement is met :
Synchronization Hardware
• Hardware support for critical section code
• critical-section problem solved using a tool-a lock.
• Race conditions are prevented
• A process must acquire a lock before entering a critical section; it
releases the lock when exit CS
• Many modern computer systems provide special hardware
instructions to test and modify the content of a word or to swap
the contents of two words atomically-non-interruptable
Solution to Critical-section
Problem Using Locks
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
TestAndSet Instruction
• Definition:
• The algorithms satisfy the mutual-exclusion requirement, they do not satisfy the
bounded-waiting requirement.
Bounded-waiting
do {
waiting[i] = TRUE;
key = TRUE; boolean waiting[n];
while (waiting[i] && key) boolean lock; initialized to false
key = TestAndSet(&lock);
waiting[i] = FALSE;
Value of key become false only if the TestAndSet () is
// critical section
j = (i + 1) % n;
executed. The first process to execute the TestAndSet
while ((j != i) && !waiting[j]) () will find key== false; all others must wait.
j = (j + 1) % n;
if (j == i)
Progress: a process exiting the critical section either
lock = FALSE;
else
sets lock to false or sets waiting[j] to false. Both allow
waiting[j] = FALSE; a process that is waiting to enter its critical section to
// remainder section proceed.
} while (TRUE);
• Busy waiting wastes CPU cycles that some other process might be able to
use productively.
• This type of semaphore is also called spinlock because the process "spins"
while waiting for the lock.
• Modified definitions of the wait() and signal() semaphore operations
Semaphore Implementation
with no Busy waiting
• Bounded-Buffer Problem
• Readers and Writers Problem
• Dining-Philosophers Problem
Bounded-Buffer Problem
} while (TRUE);
Readers-Writers Problem (Cont.)
● The structure of a reader process
do {
wait (mutex) ; Entry Section
readcount ++ ;
if (readcount == 1)
wait (wrt) ;
signal (mutex)
Critical Section // reading is performed
Exit Section
wait (mutex) ;
readcount - - ;
if (readcount == 0)
signal (wrt) ;
signal (mutex) ;
} while (TRUE);
Dining-Philosophers Problem
• Philosophers spend their lives thinking
and eating
• Don’t interact with their neighbors,
occasionally try to pick up 2 chopsticks
(one at a time) to eat from bowl
• Need both to eat, then release both
when done
• In the case of 5 philosophers
• Shared data
• Bowl of rice (data set)
• Semaphore chopstick [5]
initialized to 1
Dining-Philosophers Problem Algorithm
• The structure of Philosopher i:
do {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);