2. Race Condition
A condition when several processes access and manipulate
the same data-item; and final result depends on the order of
access. A race condition is a situation that may occur inside a critical section
Dr. Manmath N. Sahoo (CS) 2
NIT Rourkela
withdraw (account, amount) {
balance = get_balance(account);
balance = balance - amount;
put balance(account, balance);
}
balance = get_balance(account);
balance -= amount;
balance = get_balance(account);
balance -= amount;
put_balance(account, balance);
put_balance(account, balance);
context switch
context switch
Execution
flow
on
CPU
3. Critical Section
Critical Section is a section of code where some
shared variable(s) is/are modified.
Fig: General structure of a typical process with critical section
Dr. Manmath N. Sahoo (CS) 3
do {
...
...
} while (TRUE);
entry section
exit section
critical section
reminder section
NIT Rourkela
4. Solution to Critical-Section Problem
Criteria
Mutual Exclusion – No more than one process
simultaneously inside CS.
Progress –
CS is free
some processes wish to enter their CS then
the selection of the process that will enter the CS next cannot be postponed
indefinitely
Bounded Waiting – A bound must exist on the number of
times that other processes are allowed to enter their CS after
a process has made a request to enter its CS.
Dr. Manmath N. Sahoo (CS) 4
NIT Rourkela
5. Two Processes: Solution – 1
int turn
shared between processes P1 and P2
initialized to 1 or 2
5
P1
do {
} while(TRUE)
while(turn ≠ 1);
turn = 2;
critical section
reminder section
P2
do {
} while(TRUE)
while(turn ≠ 2);
turn = 1;
critical section
reminder section
NIT Rourkela Dr. Manmath N. Sahoo (CS)
6. Two Processes: Solution – 1
Mutual Exclusion: Satisfied.
Progress: Not Satisfied.
turn=1 and P1 enters to its critical section.
P1 makes turn=2 in its exit section.
P1 wishes to enter to critical section but can’t.
Bounded waiting: Satisfied
Dr. Manmath N. Sahoo (CS) 6
NIT Rourkela
7. Two Processes: Solution – 2
int flag[2] //initialized to FALSE
7
P1
do {
} while(TRUE)
flag[1] = TRUE;
while(flag[2]);
flag[1] = FALSE;
critical section
reminder section
P2
do {
} while(TRUE)
flag[2] = TRUE;
while(flag[1]);
flag[2] = FALSE;
critical section
reminder section
NIT Rourkela Dr. Manmath N. Sahoo (CS)
8. Two Processes: Solution – 2
Mutual Exclusion: Satisfied.
Progress: Not Satisfied.
P1 makes flag[1]=TRUE
P2 makes flag[2]=TRUE
Bounded waiting: Satisfied
Dr. Manmath N. Sahoo (CS) 8
P1 can’t enter P2 can’t enter
NIT Rourkela
9. Two Processes: Solution – 3
Dr. Manmath N. Sahoo (CS) 9
P1
do {
} while(TRUE)
while(flag[2]);
flag[1] = TRUE;
flag[1] = FALSE;
critical section
reminder section
P2
do {
} while(TRUE)
while(flag[1]);
flag[2] = TRUE;
flag[2] = FALSE;
critical section
reminder section
NIT Rourkela
10. Two Processes: Solution – 3
Mutual Exclusion: Not Satisfied.
while(flag[2]); -- Pass
while(flag[1]); -- Pass
P1 makes flag[1] = TRUE; and enters into critical section
P1 makes flag[2] = TRUE; and enters into critical section
Progress: Satisfied.
Bounded waiting: Not Satisfied
P1 may continuously enter into the critical section even
when P2 is waiting in its while loop
Dr. Manmath N. Sahoo (CS) 10
NIT Rourkela
12. Two Processes: Solution – 4
[Peterson’s Solution]
Mutual Exclusion: Satisfied.
Progress: Satisfied.
Bounded waiting: Satisfied
Dr. Manmath N. Sahoo (CS) 12
NIT Rourkela
13. Multiple Processes Solution
[Bakery Algorithm]
Smallest token no. will be served first
Dr. Manmath N. Sahoo (CS) 13
NIT Rourkela
Token Counter Food Counter
14. Shared variables
int number[0:n-1]; //initialized to 0
NIT Rourkela 14
Pi
do{
} while(TRUE)
number[i] = max(number[0:n-1])+1
for j = 0 to n-1 {
while( number[j] && (number[j] < number[i] );
}
number[i]=0;
critical section
reminder section
Numbers of multiple processes may be same => mutual exclusion will be violated
15. Shared variables
int number[0:n-1]; //initialized to 0
NIT Rourkela 15
Pi
do{
} while(TRUE)
number[i] = max(number[0:n-1])+1 // assumption that this
instruction is atomic in nature.
for j = 0 to n-1 {
while( number[j] && (number[j],pid[j]) < (number[i],pid[i]) );
}
number[i]=0;
critical section
reminder section
mutual exclusion will be violated still
16. Dr. Manmath N. Sahoo (CS) 16
Pi
do{
} while(TRUE) link – Youtube.
https://www.youtube.com/watch?v=Y7rBAWFQNR4&list=PLskQvPDUk0sJ6RRPdkgO2qziych6vQwZ1&index=8
choosing[i] = TRUE;
number[i] = max(number[0:n-1])+1
choosing[i] = FALSE;
for j = 0 to n-1 {
while(choosing[j]); // If someone is choosing don’t
allow anyone to enter into CS
while( number[j] && (number[j],j) < (number[i],i) );
}
number[i]=0;
critical section
reminder section
NIT Rourkela
Shared variables
int number[0:n-1]; //initialized to 0
bool choosing[0:n-1] //initialized to false
17. Hardware Solutions to Critical Sections
Dr. Manmath N. Sahoo (CS) 17
Enable and Disable Interrupt
Test-and-set instruction
Swap instruction
Motivating example why SW based solutions are not preferred:
while(true){
while(lock != false);
lock=true
Critical section
lock = false
…reminder section
}
NIT Rourkela
Not atomic; hence violates
Mutual Exclusion
18. Enable and Disable Interrupt
May miss out some important system interrupts
Not suitable for multi-processor systems
Dr. Manmath N. Sahoo (CS) 18
Pi
do{
} while(TRUE)
DI
EI
critical section
reminder section
NIT Rourkela
19. Test-and-Set instruction
Dr. Manmath N. Sahoo (CS) 19
Test-and-set instruction - defined below as if it
were a function
boolean Test-and-Set (boolean *target){
boolean rv = *target; // return value
*target = true; // set value of target
return(rv);
}
NIT Rourkela
20. Test-and-Set instruction: Solution1
Dr. Manmath N. Sahoo (CS) 20
Pi
do{
} while(TRUE)
while(Test-and-Set(&lock));
lock = FALSE;
critical section
reminder section
Shared lock initialized to
FALSE.
Mutual exclusion: YES
Progress: YES
Bounded waiting: NO
NIT Rourkela
21. Test-and-Set instruction: Solution2
Variables used:
global boolean waiting[n] //initialized to FALSE
global boolean lock //initialized to FALSE
local keyi
Dr. Manmath N. Sahoo (CS) 21
NIT Rourkela
23. Swap Instruction
NIT Rourkela Dr. Manmath N. Sahoo (CS) 23
Swap instruction - defined below as if it were a
function
boolean Swap (boolean *a, *b){
boolean temp = *a;
*a = *b;
*b = temp;
}
26. Semaphore
A synchronization primitive proposed by
Dijkstra in 1968.
Consists of an integer value
Two operations
P(S) or wait(S): waits for semaphore to become positive
V(S) or signal(S): increments semaphore by 1
P(S) and V(S) operations are atomic.
Two Types: (i) Binary (ii) Counting
NIT Rourkela Dr. Manmath N. Sahoo (CS) 26
27. Binary Semaphore:
Spin-Lock/Busy-Wait Solution
Can take two values: 1 or 0 (initialized to 1)
Struct Semaphore{ int value; };
Semaphore mutex;
mutex.value=1;
NIT Rourkela Dr. Manmath N. Sahoo (CS) 27
Pi
do{
} while(TRUE)
while(mutex.value == 0);
mutex.value=0;
mutex.value++;
critical section
reminder section
Mutual exclusion: YES
Progress: YES
Bounded waiting: NO
P(mutex)
V(mutex)
28. Binary Semaphore:
Solution with Waiting List
Struct Semaphore{
int value;
Struct Process *List;
};
Semaphore mutex;
mutex.value=1;
NIT Rourkela 28
Pi
do{
} while(TRUE)
if(mutex.value == 0){
Add Pi to mutex->List;
block();
}
else mutex.value=0;
if(mutex->List is nonempty){
Remove Pj from mutex->List;
wakeup(Pj);
}
else mutex.value++;
critical section
reminder section
Mutual exclusion: YES
Progress: YES
Bounded waiting: YES
29. Counting Semaphore
Useful when we have multiple instances of same
shared resource.
Initialized to the number of instances of the
resource. (e.g. printer)
NIT Rourkela Dr. Manmath N. Sahoo (CS) 29
30. Counting Semaphore:
Spin-Lock/Busy-Wait Solution
Semaphore countSem;
countSem.value=3;
NIT Rourkela Dr. Manmath N. Sahoo (CS) 30
Pi
do{
} while(TRUE)
while(countSem.value == 0);
countSem.value--;
countSem.value++;
critical section
reminder section
Mutual exclusion: YES
Progress: YES
Bounded waiting: NO
31. Counting Semaphore:
Solution with Waiting List
NIT Rourkela Dr. Manmath N. Sahoo (CS) 31
Pi
do{
} while(TRUE)
countSem.value--;
if(countSem.value < 0){
Add Pi to countSem->List;
block();
}
countSem.value++;
if(countSem.value <= 0){
Remove process Pj from countSem->List;
wakeup(Pj);
}
critical section
reminder section
32. Bounded Buffer Problem
AKA “producer/consumer” problem
there is a buffer in memory with N entries
producer threads insert entries into it (one at a time)
consumer threads remove entries from it (one at a time)
Threads are concurrent
so, we must use synchronization constructs to control
access to shared variables describing buffer
NIT Rourkela Dr. Manmath N. Sahoo (CS) 32
head
tail
33. Bounded Buffer Problem
Constraints
The consumer must wait if buffers are empty
(synchronization constraint)
The producer must wait if buffers are full (synchronization
constraint)
Only one thread can manipulate the buffer at a time
(mutual exclusion)
NIT Rourkela Dr. Manmath N. Sahoo (CS) 33
34. Bounded Buffer Problem:
Developing a Solution
Each constraint needs a semaphore
Semaphore mutex = 1;
Semaphore nFreeBuffers = N;
Semaphore nLoadedBuffers = 0;
NIT Rourkela Dr. Manmath N. Sahoo (CS) 34
Producer
P(mutex);
// put 1 item in the buffer
V(mutex);
Consumer
P(mutex);
// take 1 item from buffer
V(mutex);
P(nLoadedBuffers);
P(nFreeBuffers);
V(nLoadedBuffers); V(nFreeBuffers);
35. Dining Philosophers Problem
What if all the philosophers grab their left chopsticks!!
NIT Rourkela Dr. Manmath N. Sahoo (CS) 35
Pi
do{
} while(TRUE)
P(Fork[i])
P(Fork[(i+1)%5])
V(Fork[(i+1)%5])
V(Fork[i])
EAT
THINK
P0
P1
P3
P2
P4
0
1
3
2
4
36. Dining Philosophers Problem:
Solution to deadlock
Allow at most n-1 philosophers to seat.
Allow a philosopher to grab the chopsticks only if both are
available.
An odd philosopher grabs his left chopstick first then the
right chopstick. An even philosopher grabs his right
chopstick then the left chopstick.
NIT Rourkela Dr. Manmath N. Sahoo (CS) 36
DI
P(Chopstick[i])
P(Chopstick[(i+1)%5])
EI
P0 P1 P2 P3 P4
P(C[1]) P(C[1]) P(C[3]) P(C[3]) P(C[0])
P(C[0]) P(C[2]) P(C[2]) P(C[4]) P(C[4])
37. Readers Writers Problem
NIT Rourkela Dr. Manmath N. Sahoo (CS) 37
A dataset is shared among a number of
concurrent processes
Readers – only read; they do not perform any updates
Writers – can both read and write.
Conditions
Any number of readers may simultaneously read the file.
If a writer is writing to the file, no reader/writer is allowed
to access the file.
38. Readers Writers Problem: Solution 1 –
Preference to Readers
Semaphore rlock initialized to 1.
Integer rcount initialized to 0.
Semaphore wSem initialized to 1.
38
Ri
do{
} while(TRUE)
P(rlock);
rcount++;
if (rcount==1) P(wSem);
V(rlock);
P(rlock);
rcount--;
if (rcount==0) V(wSem);
V(rlock);
READ
Wi
do{
} while(TRUE)
P(wSem);
V(wSem);
WRITE
39. Readers Writers Problem: Solution 1 –
Preference to Readers
Readers only
All readers are allowed to READ
Writers only
One writer at a time
Both readers and writers with read first
Writer has to wait on P(wrt)
Both readers and writers with write first
Reader has to wait on P(wrt)
Writers may starve !
NIT Rourkela Dr. Manmath N. Sahoo (CS) 39
40. Readers Writers Problem: Solution 2 –
Preference to Writers
Integer rcount – keeps track of number of readers.
Integer wcount – keeps track of number of writers.
Semaphore rlock – controls the updating of rdcount.
Semaphore wlock – controls the updating of wrtcount.
Semaphore rSem – inhibits all readers while there is at least one writer
desiring access to critical section.
Semaphore wSem – inhibits all writers while there is at least one reader
desiring access to critical section.
Semaphore rQueue – to avoid long queue on rSem. So that waiting
writer processes get preference.
NIT Rourkela Dr. Manmath N. Sahoo (CS) 40
42. Readers Writers Problem: Solution 3 –
Based on the arrival order
NIT Rourkela 42
Integer rcount
Semaphore rlock
Semaphore wSem
Semaphore order_mutuex
Office
43. Readers Writers Problem: Solution 3 –
Based on the arrival order
NIT Rourkela Dr. Manmath N. Sahoo (CS) 43
Ri
do{
} while(TRUE)
P(rlock);
rcount++;
if (rcount==1)
P(wSem));
V(rlock);
P(rlock);
rcount--;
if (rcount==0)
V(wSem);
V(rlock);
READ
Wi
do{
} while(TRUE)
P(wSem));
V(wSem);
WRITE
P(order_mutex);
P(order_mutex);
V(order_mutex);
V(order_mutex);