SlideShare a Scribd company logo
1 of 43
 Process Synchronization
Dr. Manmath N. Sahoo
Dept. of CSE, NIT Rourkela
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
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
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
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)
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
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)
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
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
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
Two Processes: Solution – 4
[Peterson’s Solution]
Dr. Manmath N. Sahoo (CS) 11
P1
do{
} while(TRUE)
flag[1] = TRUE;
turn = 2;
while(flag[2] && turn==2);
flag[1] = FALSE;
critical section
reminder section
P2
do{
} while(TRUE)
flag[2] = TRUE;
turn = 1;
while(flag[1] && turn==1);
flag[2] = FALSE;
critical section
reminder section
NIT Rourkela
Two Processes: Solution – 4
[Peterson’s Solution]
 Mutual Exclusion: Satisfied.
 Progress: Satisfied.
 Bounded waiting: Satisfied
Dr. Manmath N. Sahoo (CS) 12
NIT Rourkela
Multiple Processes Solution
[Bakery Algorithm]
 Smallest token no. will be served first
Dr. Manmath N. Sahoo (CS) 13
NIT Rourkela
Token Counter Food Counter
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
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
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
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
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
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
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
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
Test-and-Set instruction: Solution2
22
Pi
do{
} while(TRUE)
waiting[i] = TRUE; keyi = TRUE;
while(waiting[i] && keyi)
keyi = Test-and-Set(&lock);
waiting[i]=FALSE;
j = (i+1) % n;
while(j!=i && waiting[j]==FALSE)
j = (j+1) % n;
if(j==i)
lock = FALSE;
else waiting[j] = FALSE;
critical section
reminder section
Dr. Manmath N. Sahoo (CS)
 Mutual exclusion: YES
 Progress: YES
 Bounded waiting: YES
NIT Rourkela
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;
}
Swap Instruction: Solution1
 global boolean lock;
 local boolean keyi;
 lock & keyi initialized to FALSE
NIT Rourkela Dr. Manmath N. Sahoo (CS) 24
Pi
do{
} while(TRUE)
keyi = TRUE;
while(keyi)
Swap(&lock, &keyi);
lock = FALSE;
critical section
reminder section
Pj
do{
} while(TRUE)
keyj = TRUE;
while(keyj)
Swap(&lock, &keyj);
lock = FALSE;
critical section
reminder section
 Mutual exclusion: YES
 Progress: YES
 Bounded waiting: NO
Swap instruction: Solution2
25
Pi
do{
} while(TRUE)
waiting[i] = TRUE; key = TRUE;
while(waiting[i] && key)
Swap(&lock, &key);
waiting[i]=FALSE;
j = (i+1) % n;
while(j!=i && waiting[j]==FALSE)
j = (j+1) % n;
if(j==i)
lock = FALSE;
else waiting[j] = FALSE;
critical section
reminder section
Dr. Manmath N. Sahoo (CS)
 Mutual exclusion: YES
 Progress: YES
 Bounded waiting: YES
NIT Rourkela
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
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)
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
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
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
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
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
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
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);
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
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])
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.
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
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
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
Readers Writers Problem: Solution 2 –
Preference to Writers
NIT Rourkela 41
Ri
do{
} while(TRUE)
P(rSem);
P(rlock);
rcount++;
if(rcount == 1) P(wSem);
V(rlock);
V(rSem);
P(rlock);
rcount--;
if (rcount == 0) V(wSem);
V(rlock);
READ
reminder section
Wi
do{
} while(TRUE)
P(wlock);
wcount++;
if(wcount == 1) P(rSem);
V(wlock);
P(wSem);
V(wSem);
P(wlock);
wcount--;
if (wcount == 0) V(rSem);
V(wlock);
WRITE
reminder section
P(rQueue);
V(rQueue);
Readers may starve !
Readers Writers Problem: Solution 3 –
Based on the arrival order
NIT Rourkela 42
 Integer rcount
 Semaphore rlock
 Semaphore wSem
 Semaphore order_mutuex
Office
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);

More Related Content

Similar to 5Process Synchronization.pptx gfgfhgjg jh

Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionBipul Chandra Kar
 
Lecture_6_Process.ppt
Lecture_6_Process.pptLecture_6_Process.ppt
Lecture_6_Process.pptwafawafa52
 
TableDrivenProg_20160714
TableDrivenProg_20160714TableDrivenProg_20160714
TableDrivenProg_20160714Teddy Hsiung
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OSC.U
 
Concepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDLConcepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDLanand hd
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptxAbdullahBhatti53
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronizationvinay arora
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
Ch7 Process Synchronization galvin
Ch7 Process Synchronization galvinCh7 Process Synchronization galvin
Ch7 Process Synchronization galvinShubham Singh
 
Ch7: Process Synchronization
Ch7: Process SynchronizationCh7: Process Synchronization
Ch7: Process SynchronizationAhmar Hashmi
 
14- Process Synchronization.pptx
14- Process Synchronization.pptx14- Process Synchronization.pptx
14- Process Synchronization.pptxmobeenahmed49
 
Scheduling Fixed Priority Tasks with Preemption Threshold
Scheduling Fixed Priority Tasks with Preemption ThresholdScheduling Fixed Priority Tasks with Preemption Threshold
Scheduling Fixed Priority Tasks with Preemption ThresholdDeepak Raj
 
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)Maho Nakata
 

Similar to 5Process Synchronization.pptx gfgfhgjg jh (20)

CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
Ch7
Ch7Ch7
Ch7
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
 
VERILOG CODE
VERILOG CODEVERILOG CODE
VERILOG CODE
 
Lecture_6_Process.ppt
Lecture_6_Process.pptLecture_6_Process.ppt
Lecture_6_Process.ppt
 
TableDrivenProg_20160714
TableDrivenProg_20160714TableDrivenProg_20160714
TableDrivenProg_20160714
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
Concepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDLConcepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDL
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptx
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Ch7 Process Synchronization galvin
Ch7 Process Synchronization galvinCh7 Process Synchronization galvin
Ch7 Process Synchronization galvin
 
Ch7: Process Synchronization
Ch7: Process SynchronizationCh7: Process Synchronization
Ch7: Process Synchronization
 
14- Process Synchronization.pptx
14- Process Synchronization.pptx14- Process Synchronization.pptx
14- Process Synchronization.pptx
 
Scheduling Fixed Priority Tasks with Preemption Threshold
Scheduling Fixed Priority Tasks with Preemption ThresholdScheduling Fixed Priority Tasks with Preemption Threshold
Scheduling Fixed Priority Tasks with Preemption Threshold
 
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)
quantum chemistry on quantum computer handson by Q# (2019/8/4@MDR Hongo, Tokyo)
 
FPGA Tutorial - LCD Interface
FPGA Tutorial - LCD InterfaceFPGA Tutorial - LCD Interface
FPGA Tutorial - LCD Interface
 

Recently uploaded

Call Girls Bangalore Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Bangalore Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Bangalore Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Bangalore Just Call 8250077686 Top Class Call Girl Service AvailableDipal Arora
 
Call Girls Haridwar Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Haridwar Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Haridwar Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Haridwar Just Call 8250077686 Top Class Call Girl Service AvailableDipal Arora
 
VIP Hyderabad Call Girls Bahadurpally 7877925207 ₹5000 To 25K With AC Room 💚😋
VIP Hyderabad Call Girls Bahadurpally 7877925207 ₹5000 To 25K With AC Room 💚😋VIP Hyderabad Call Girls Bahadurpally 7877925207 ₹5000 To 25K With AC Room 💚😋
VIP Hyderabad Call Girls Bahadurpally 7877925207 ₹5000 To 25K With AC Room 💚😋TANUJA PANDEY
 
Premium Call Girls Cottonpet Whatsapp 7001035870 Independent Escort Service
Premium Call Girls Cottonpet Whatsapp 7001035870 Independent Escort ServicePremium Call Girls Cottonpet Whatsapp 7001035870 Independent Escort Service
Premium Call Girls Cottonpet Whatsapp 7001035870 Independent Escort Servicevidya singh
 
Russian Escorts Girls Nehru Place ZINATHI 🔝9711199012 ☪ 24/7 Call Girls Delhi
Russian Escorts Girls  Nehru Place ZINATHI 🔝9711199012 ☪ 24/7 Call Girls DelhiRussian Escorts Girls  Nehru Place ZINATHI 🔝9711199012 ☪ 24/7 Call Girls Delhi
Russian Escorts Girls Nehru Place ZINATHI 🔝9711199012 ☪ 24/7 Call Girls DelhiAlinaDevecerski
 
VIP Service Call Girls Sindhi Colony 📳 7877925207 For 18+ VIP Call Girl At Th...
VIP Service Call Girls Sindhi Colony 📳 7877925207 For 18+ VIP Call Girl At Th...VIP Service Call Girls Sindhi Colony 📳 7877925207 For 18+ VIP Call Girl At Th...
VIP Service Call Girls Sindhi Colony 📳 7877925207 For 18+ VIP Call Girl At Th...jageshsingh5554
 
Call Girls Varanasi Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Varanasi Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Varanasi Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Varanasi Just Call 9907093804 Top Class Call Girl Service AvailableDipal Arora
 
Top Rated Hyderabad Call Girls Erragadda ⟟ 6297143586 ⟟ Call Me For Genuine ...
Top Rated  Hyderabad Call Girls Erragadda ⟟ 6297143586 ⟟ Call Me For Genuine ...Top Rated  Hyderabad Call Girls Erragadda ⟟ 6297143586 ⟟ Call Me For Genuine ...
Top Rated Hyderabad Call Girls Erragadda ⟟ 6297143586 ⟟ Call Me For Genuine ...chandars293
 
(Rocky) Jaipur Call Girl - 09521753030 Escorts Service 50% Off with Cash ON D...
(Rocky) Jaipur Call Girl - 09521753030 Escorts Service 50% Off with Cash ON D...(Rocky) Jaipur Call Girl - 09521753030 Escorts Service 50% Off with Cash ON D...
(Rocky) Jaipur Call Girl - 09521753030 Escorts Service 50% Off with Cash ON D...indiancallgirl4rent
 
Top Rated Bangalore Call Girls Mg Road ⟟ 9332606886 ⟟ Call Me For Genuine S...
Top Rated Bangalore Call Girls Mg Road ⟟   9332606886 ⟟ Call Me For Genuine S...Top Rated Bangalore Call Girls Mg Road ⟟   9332606886 ⟟ Call Me For Genuine S...
Top Rated Bangalore Call Girls Mg Road ⟟ 9332606886 ⟟ Call Me For Genuine S...narwatsonia7
 
(Low Rate RASHMI ) Rate Of Call Girls Jaipur ❣ 8445551418 ❣ Elite Models & Ce...
(Low Rate RASHMI ) Rate Of Call Girls Jaipur ❣ 8445551418 ❣ Elite Models & Ce...(Low Rate RASHMI ) Rate Of Call Girls Jaipur ❣ 8445551418 ❣ Elite Models & Ce...
(Low Rate RASHMI ) Rate Of Call Girls Jaipur ❣ 8445551418 ❣ Elite Models & Ce...parulsinha
 
The Most Attractive Hyderabad Call Girls Kothapet 𖠋 6297143586 𖠋 Will You Mis...
The Most Attractive Hyderabad Call Girls Kothapet 𖠋 6297143586 𖠋 Will You Mis...The Most Attractive Hyderabad Call Girls Kothapet 𖠋 6297143586 𖠋 Will You Mis...
The Most Attractive Hyderabad Call Girls Kothapet 𖠋 6297143586 𖠋 Will You Mis...chandars293
 
Best Rate (Guwahati ) Call Girls Guwahati ⟟ 8617370543 ⟟ High Class Call Girl...
Best Rate (Guwahati ) Call Girls Guwahati ⟟ 8617370543 ⟟ High Class Call Girl...Best Rate (Guwahati ) Call Girls Guwahati ⟟ 8617370543 ⟟ High Class Call Girl...
Best Rate (Guwahati ) Call Girls Guwahati ⟟ 8617370543 ⟟ High Class Call Girl...Dipal Arora
 
💎VVIP Kolkata Call Girls Parganas🩱7001035870🩱Independent Girl ( Ac Rooms Avai...
💎VVIP Kolkata Call Girls Parganas🩱7001035870🩱Independent Girl ( Ac Rooms Avai...💎VVIP Kolkata Call Girls Parganas🩱7001035870🩱Independent Girl ( Ac Rooms Avai...
💎VVIP Kolkata Call Girls Parganas🩱7001035870🩱Independent Girl ( Ac Rooms Avai...Taniya Sharma
 
Call Girls Bareilly Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Bareilly Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Bareilly Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Bareilly Just Call 8250077686 Top Class Call Girl Service AvailableDipal Arora
 
Bangalore Call Girls Nelamangala Number 9332606886 Meetin With Bangalore Esc...
Bangalore Call Girls Nelamangala Number 9332606886  Meetin With Bangalore Esc...Bangalore Call Girls Nelamangala Number 9332606886  Meetin With Bangalore Esc...
Bangalore Call Girls Nelamangala Number 9332606886 Meetin With Bangalore Esc...narwatsonia7
 
Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...
Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...
Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...vidya singh
 
College Call Girls in Haridwar 9667172968 Short 4000 Night 10000 Best call gi...
College Call Girls in Haridwar 9667172968 Short 4000 Night 10000 Best call gi...College Call Girls in Haridwar 9667172968 Short 4000 Night 10000 Best call gi...
College Call Girls in Haridwar 9667172968 Short 4000 Night 10000 Best call gi...perfect solution
 
(👑VVIP ISHAAN ) Russian Call Girls Service Navi Mumbai🖕9920874524🖕Independent...
(👑VVIP ISHAAN ) Russian Call Girls Service Navi Mumbai🖕9920874524🖕Independent...(👑VVIP ISHAAN ) Russian Call Girls Service Navi Mumbai🖕9920874524🖕Independent...
(👑VVIP ISHAAN ) Russian Call Girls Service Navi Mumbai🖕9920874524🖕Independent...Taniya Sharma
 
Top Rated Bangalore Call Girls Ramamurthy Nagar ⟟ 9332606886 ⟟ Call Me For G...
Top Rated Bangalore Call Girls Ramamurthy Nagar ⟟  9332606886 ⟟ Call Me For G...Top Rated Bangalore Call Girls Ramamurthy Nagar ⟟  9332606886 ⟟ Call Me For G...
Top Rated Bangalore Call Girls Ramamurthy Nagar ⟟ 9332606886 ⟟ Call Me For G...narwatsonia7
 

Recently uploaded (20)

Call Girls Bangalore Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Bangalore Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Bangalore Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Bangalore Just Call 8250077686 Top Class Call Girl Service Available
 
Call Girls Haridwar Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Haridwar Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Haridwar Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Haridwar Just Call 8250077686 Top Class Call Girl Service Available
 
VIP Hyderabad Call Girls Bahadurpally 7877925207 ₹5000 To 25K With AC Room 💚😋
VIP Hyderabad Call Girls Bahadurpally 7877925207 ₹5000 To 25K With AC Room 💚😋VIP Hyderabad Call Girls Bahadurpally 7877925207 ₹5000 To 25K With AC Room 💚😋
VIP Hyderabad Call Girls Bahadurpally 7877925207 ₹5000 To 25K With AC Room 💚😋
 
Premium Call Girls Cottonpet Whatsapp 7001035870 Independent Escort Service
Premium Call Girls Cottonpet Whatsapp 7001035870 Independent Escort ServicePremium Call Girls Cottonpet Whatsapp 7001035870 Independent Escort Service
Premium Call Girls Cottonpet Whatsapp 7001035870 Independent Escort Service
 
Russian Escorts Girls Nehru Place ZINATHI 🔝9711199012 ☪ 24/7 Call Girls Delhi
Russian Escorts Girls  Nehru Place ZINATHI 🔝9711199012 ☪ 24/7 Call Girls DelhiRussian Escorts Girls  Nehru Place ZINATHI 🔝9711199012 ☪ 24/7 Call Girls Delhi
Russian Escorts Girls Nehru Place ZINATHI 🔝9711199012 ☪ 24/7 Call Girls Delhi
 
VIP Service Call Girls Sindhi Colony 📳 7877925207 For 18+ VIP Call Girl At Th...
VIP Service Call Girls Sindhi Colony 📳 7877925207 For 18+ VIP Call Girl At Th...VIP Service Call Girls Sindhi Colony 📳 7877925207 For 18+ VIP Call Girl At Th...
VIP Service Call Girls Sindhi Colony 📳 7877925207 For 18+ VIP Call Girl At Th...
 
Call Girls Varanasi Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Varanasi Just Call 9907093804 Top Class Call Girl Service AvailableCall Girls Varanasi Just Call 9907093804 Top Class Call Girl Service Available
Call Girls Varanasi Just Call 9907093804 Top Class Call Girl Service Available
 
Top Rated Hyderabad Call Girls Erragadda ⟟ 6297143586 ⟟ Call Me For Genuine ...
Top Rated  Hyderabad Call Girls Erragadda ⟟ 6297143586 ⟟ Call Me For Genuine ...Top Rated  Hyderabad Call Girls Erragadda ⟟ 6297143586 ⟟ Call Me For Genuine ...
Top Rated Hyderabad Call Girls Erragadda ⟟ 6297143586 ⟟ Call Me For Genuine ...
 
(Rocky) Jaipur Call Girl - 09521753030 Escorts Service 50% Off with Cash ON D...
(Rocky) Jaipur Call Girl - 09521753030 Escorts Service 50% Off with Cash ON D...(Rocky) Jaipur Call Girl - 09521753030 Escorts Service 50% Off with Cash ON D...
(Rocky) Jaipur Call Girl - 09521753030 Escorts Service 50% Off with Cash ON D...
 
Top Rated Bangalore Call Girls Mg Road ⟟ 9332606886 ⟟ Call Me For Genuine S...
Top Rated Bangalore Call Girls Mg Road ⟟   9332606886 ⟟ Call Me For Genuine S...Top Rated Bangalore Call Girls Mg Road ⟟   9332606886 ⟟ Call Me For Genuine S...
Top Rated Bangalore Call Girls Mg Road ⟟ 9332606886 ⟟ Call Me For Genuine S...
 
(Low Rate RASHMI ) Rate Of Call Girls Jaipur ❣ 8445551418 ❣ Elite Models & Ce...
(Low Rate RASHMI ) Rate Of Call Girls Jaipur ❣ 8445551418 ❣ Elite Models & Ce...(Low Rate RASHMI ) Rate Of Call Girls Jaipur ❣ 8445551418 ❣ Elite Models & Ce...
(Low Rate RASHMI ) Rate Of Call Girls Jaipur ❣ 8445551418 ❣ Elite Models & Ce...
 
The Most Attractive Hyderabad Call Girls Kothapet 𖠋 6297143586 𖠋 Will You Mis...
The Most Attractive Hyderabad Call Girls Kothapet 𖠋 6297143586 𖠋 Will You Mis...The Most Attractive Hyderabad Call Girls Kothapet 𖠋 6297143586 𖠋 Will You Mis...
The Most Attractive Hyderabad Call Girls Kothapet 𖠋 6297143586 𖠋 Will You Mis...
 
Best Rate (Guwahati ) Call Girls Guwahati ⟟ 8617370543 ⟟ High Class Call Girl...
Best Rate (Guwahati ) Call Girls Guwahati ⟟ 8617370543 ⟟ High Class Call Girl...Best Rate (Guwahati ) Call Girls Guwahati ⟟ 8617370543 ⟟ High Class Call Girl...
Best Rate (Guwahati ) Call Girls Guwahati ⟟ 8617370543 ⟟ High Class Call Girl...
 
💎VVIP Kolkata Call Girls Parganas🩱7001035870🩱Independent Girl ( Ac Rooms Avai...
💎VVIP Kolkata Call Girls Parganas🩱7001035870🩱Independent Girl ( Ac Rooms Avai...💎VVIP Kolkata Call Girls Parganas🩱7001035870🩱Independent Girl ( Ac Rooms Avai...
💎VVIP Kolkata Call Girls Parganas🩱7001035870🩱Independent Girl ( Ac Rooms Avai...
 
Call Girls Bareilly Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Bareilly Just Call 8250077686 Top Class Call Girl Service AvailableCall Girls Bareilly Just Call 8250077686 Top Class Call Girl Service Available
Call Girls Bareilly Just Call 8250077686 Top Class Call Girl Service Available
 
Bangalore Call Girls Nelamangala Number 9332606886 Meetin With Bangalore Esc...
Bangalore Call Girls Nelamangala Number 9332606886  Meetin With Bangalore Esc...Bangalore Call Girls Nelamangala Number 9332606886  Meetin With Bangalore Esc...
Bangalore Call Girls Nelamangala Number 9332606886 Meetin With Bangalore Esc...
 
Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...
Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...
Manyata Tech Park ( Call Girls ) Bangalore ✔ 6297143586 ✔ Hot Model With Sexy...
 
College Call Girls in Haridwar 9667172968 Short 4000 Night 10000 Best call gi...
College Call Girls in Haridwar 9667172968 Short 4000 Night 10000 Best call gi...College Call Girls in Haridwar 9667172968 Short 4000 Night 10000 Best call gi...
College Call Girls in Haridwar 9667172968 Short 4000 Night 10000 Best call gi...
 
(👑VVIP ISHAAN ) Russian Call Girls Service Navi Mumbai🖕9920874524🖕Independent...
(👑VVIP ISHAAN ) Russian Call Girls Service Navi Mumbai🖕9920874524🖕Independent...(👑VVIP ISHAAN ) Russian Call Girls Service Navi Mumbai🖕9920874524🖕Independent...
(👑VVIP ISHAAN ) Russian Call Girls Service Navi Mumbai🖕9920874524🖕Independent...
 
Top Rated Bangalore Call Girls Ramamurthy Nagar ⟟ 9332606886 ⟟ Call Me For G...
Top Rated Bangalore Call Girls Ramamurthy Nagar ⟟  9332606886 ⟟ Call Me For G...Top Rated Bangalore Call Girls Ramamurthy Nagar ⟟  9332606886 ⟟ Call Me For G...
Top Rated Bangalore Call Girls Ramamurthy Nagar ⟟ 9332606886 ⟟ Call Me For G...
 

5Process Synchronization.pptx gfgfhgjg jh

  • 1.  Process Synchronization Dr. Manmath N. Sahoo Dept. of CSE, NIT Rourkela
  • 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
  • 11. Two Processes: Solution – 4 [Peterson’s Solution] Dr. Manmath N. Sahoo (CS) 11 P1 do{ } while(TRUE) flag[1] = TRUE; turn = 2; while(flag[2] && turn==2); flag[1] = FALSE; critical section reminder section P2 do{ } while(TRUE) flag[2] = TRUE; turn = 1; while(flag[1] && turn==1); flag[2] = FALSE; critical section reminder section 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
  • 22. Test-and-Set instruction: Solution2 22 Pi do{ } while(TRUE) waiting[i] = TRUE; keyi = TRUE; while(waiting[i] && keyi) keyi = Test-and-Set(&lock); waiting[i]=FALSE; j = (i+1) % n; while(j!=i && waiting[j]==FALSE) j = (j+1) % n; if(j==i) lock = FALSE; else waiting[j] = FALSE; critical section reminder section Dr. Manmath N. Sahoo (CS)  Mutual exclusion: YES  Progress: YES  Bounded waiting: YES 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; }
  • 24. Swap Instruction: Solution1  global boolean lock;  local boolean keyi;  lock & keyi initialized to FALSE NIT Rourkela Dr. Manmath N. Sahoo (CS) 24 Pi do{ } while(TRUE) keyi = TRUE; while(keyi) Swap(&lock, &keyi); lock = FALSE; critical section reminder section Pj do{ } while(TRUE) keyj = TRUE; while(keyj) Swap(&lock, &keyj); lock = FALSE; critical section reminder section  Mutual exclusion: YES  Progress: YES  Bounded waiting: NO
  • 25. Swap instruction: Solution2 25 Pi do{ } while(TRUE) waiting[i] = TRUE; key = TRUE; while(waiting[i] && key) Swap(&lock, &key); waiting[i]=FALSE; j = (i+1) % n; while(j!=i && waiting[j]==FALSE) j = (j+1) % n; if(j==i) lock = FALSE; else waiting[j] = FALSE; critical section reminder section Dr. Manmath N. Sahoo (CS)  Mutual exclusion: YES  Progress: YES  Bounded waiting: YES NIT Rourkela
  • 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
  • 41. Readers Writers Problem: Solution 2 – Preference to Writers NIT Rourkela 41 Ri do{ } while(TRUE) P(rSem); P(rlock); rcount++; if(rcount == 1) P(wSem); V(rlock); V(rSem); P(rlock); rcount--; if (rcount == 0) V(wSem); V(rlock); READ reminder section Wi do{ } while(TRUE) P(wlock); wcount++; if(wcount == 1) P(rSem); V(wlock); P(wSem); V(wSem); P(wlock); wcount--; if (wcount == 0) V(rSem); V(wlock); WRITE reminder section P(rQueue); V(rQueue); Readers may starve !
  • 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);