SlideShare uma empresa Scribd logo
1 de 14
Presentation on Peterson
Algorithm for Critical Section
Problem Solution
1
By
Bipul Chandra kar (131-15-015 )
Mahadi Hasan (131-15-023)
Md.Mohaiminul Islam(132-15-114)
Consider system of n processes { P0 , P1 , …, Pn-1 }
Each process has a critical section segment of code
Process may be changing common variables, updating table, writing file,
etc.
When one process is in its critical section, no other may be executing in its
critical section
Critical-section problem is to design a protocol to solve this
Each process must ask permission to enter its critical section in entry section,
may follow critical section with exit section, the remaining code is in its
remainder section
2Critical Section Problem
Programs and critical sections
 The part of the program (process) that is
accessing and changing shared data is called its
critical section
3
Change X
Change X
Change Y
Change Y
Change Y
Change X
Process 1 Code Process 2 Code Process 3 Code
Assuming X and Y are shared data.
Critical Section
 The general way to do that is:
do {
critical section
remainder section
} while (TRUE)
do {
entry section
critical section
exit section
remainder
} while (TRUE)
Entry section will allow only one process to enter and execute critical section code.
4
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
5
Peterson’s Solution
 Two process solution
 Assume that the LOAD and STORE 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!
6
Algorithm for Process Pi
7
do {
flag[i] = TRUE;
turn = j;
while (flag[j] && turn == j);
critical section
flag[i] = FALSE;
remainder section
} while (1)
entry section
exit section
Two processes executing concurrently 8
do {
flag1 = TRUE;
turn = 2;
while (flag2 && turn == 2);
critical section…..
flag1 = FALSE;
remainder section…..
} while (1)
do {
flag2 = TRUE;
turn = 1;
while (flag1 && turn == 1);
critical section…..
flag2 = FALSE;
remainder section…..
} while (1)
PROCESS 1 PROCESS 2
Shared Variables
flag1, flag2
turn
EXAMPLE 9
Process 0:
flag[0] := TRUE
turn := 1
check (flag[1] = TRUE and turn = 1)
- Condition is false because flag[1] = FALSE
- Since condition is false, no waiting in while loop
- Enters the critical section
Phase-1
Process 1:
flag[1] := TRUE
turn := 0
check (flag[0] = TRUE and turn = 0)
- Since condition is true, it keeps busy waiting until
it loses the processor
- Process 0 resumes and continues until it finishes
in the critical section
Phase-2
Process 0:
- Leaves critical section
Sets flag[0] := FALSE
- Start executing the remainder (anything else a process
does besides using the critical section)
- Process 0 happens to lose the processor
Phase-3
Process 1:
check (flag[0] = TRUE and turn = 0)
- This condition fails because flag[0] = FALSE
- No more busy waiting
-Enter the critical section
Phase-4
10
public class cSection {
int turn;
boolean flag[] = new boolean[2];
int i = 0, j = 1;
// CSC variables
int counter = 0;// counter for giving processes an upper bound
int cscVar = 13;
private class ProcessI extends Thread { // process thread for i
@Override
public void run() {
try {
do {
flag[i] = true;
turn = j;
while (flag[j] && turn == j)
; // wait for j to finish
// critical section
System.out.println("I is in critical section");
cscVar++;
System.out.println(cscVar);
counter++;
System.out.println("counter is " + counter + "n___________");
//
flag[i] = false;
// remainder section
} while (counter < 100); // 100 is upper bound, to remove
// infinite looping
}
Implementation
Implementation 11
catch (Exception ex) {
ex.printStackTrace();
}
}
}
private class ProcessJ extends Thread { // process thread for j
@Override
public void run() {
try {
do {
flag[j] = true;
turn = i;
while (flag[i] && turn == i)
;// wait for i to finish
// critical section
System.out.println("J is in critical section");
cscVar--;
System.out.println(cscVar);
counter++;
System.out.println("counter is " + counter + "n___________");
//
flag[j] = false;
// remainder section
} while (counter < 100); // 100 is upper bound, to remove
// infinite looping
}
12
catch (Exception ex) {
ex.printStackTrace();
}
}
}
public cSection() {
System.out.println("Starting Threads/Processes");
Thread I = new ProcessI();
Thread J = new ProcessJ();
I.start(); // start process i
J.start(); // start process j
}
public static void main(String[] args) {
cSection cSec = new cSection();
}
}
Implementation
Conclusion 13
• Simplest algorithm for critical section problem solution.
• Peterson algorithm satisfies the three key point of solving the critical section
problem
Thank You To All .
14

Mais conteúdo relacionado

Mais procurados

Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Ravindra Raju Kolahalam
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-SystemsVenkata Sreeram
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating SystemsRitu Ranjan Shrivastwa
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSwati Chauhan
 
Synchronization hardware
Synchronization hardwareSynchronization hardware
Synchronization hardwareSaeram Butt
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memorysgpraju
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's typesNishant Joshi
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.MOHIT DADU
 

Mais procurados (20)

Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Deadlock ppt
Deadlock ppt Deadlock ppt
Deadlock ppt
 
Heaps
HeapsHeaps
Heaps
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
 
Synchronization hardware
Synchronization hardwareSynchronization hardware
Synchronization hardware
 
Critical section operating system
Critical section  operating systemCritical section  operating system
Critical section operating system
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
SCHEDULING ALGORITHMS
SCHEDULING ALGORITHMSSCHEDULING ALGORITHMS
SCHEDULING ALGORITHMS
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memory
 
Mutual exclusion and sync
Mutual exclusion and syncMutual exclusion and sync
Mutual exclusion and sync
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
 
Interrupts and types of interrupts
Interrupts and types of interruptsInterrupts and types of interrupts
Interrupts and types of interrupts
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.
 

Semelhante a Peterson Critical Section Problem Solution

Critical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamCritical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamRamakrishna Reddy Bijjam
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)Nagarajan
 
Operatioooooooooooooooooooooooooooooooooooooooooooooo
OperatiooooooooooooooooooooooooooooooooooooooooooooooOperatioooooooooooooooooooooooooooooooooooooooooooooo
Operatioooooooooooooooooooooooooooooooooooooooooooooolegeved374
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process SynchronizationSonali Chauhan
 
14- Process Synchronization.pptx
14- Process Synchronization.pptx14- Process Synchronization.pptx
14- Process Synchronization.pptxmobeenahmed49
 
Peterson’s Solution.pdf by Mustehsan Mehmood
Peterson’s Solution.pdf by Mustehsan MehmoodPeterson’s Solution.pdf by Mustehsan Mehmood
Peterson’s Solution.pdf by Mustehsan MehmoodWarisBaig
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmSouvik Roy
 
OS Process synchronization Unit3 synchronization
OS Process synchronization Unit3  synchronizationOS Process synchronization Unit3  synchronization
OS Process synchronization Unit3 synchronizationsubhamchy2005
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptxAbdullahBhatti53
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OSC.U
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronizationVaibhav Khanna
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process SynchronizationShipra Swati
 

Semelhante a Peterson Critical Section Problem Solution (20)

Critical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamCritical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy Bijjam
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)
 
Operatioooooooooooooooooooooooooooooooooooooooooooooo
OperatiooooooooooooooooooooooooooooooooooooooooooooooOperatioooooooooooooooooooooooooooooooooooooooooooooo
Operatioooooooooooooooooooooooooooooooooooooooooooooo
 
Operating System
Operating SystemOperating System
Operating System
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
Ch7
Ch7Ch7
Ch7
 
14- Process Synchronization.pptx
14- Process Synchronization.pptx14- Process Synchronization.pptx
14- Process Synchronization.pptx
 
CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
OSCh7
OSCh7OSCh7
OSCh7
 
OS_Ch7
OS_Ch7OS_Ch7
OS_Ch7
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
 
Peterson’s Solution.pdf by Mustehsan Mehmood
Peterson’s Solution.pdf by Mustehsan MehmoodPeterson’s Solution.pdf by Mustehsan Mehmood
Peterson’s Solution.pdf by Mustehsan Mehmood
 
Lecture16-17.ppt
Lecture16-17.pptLecture16-17.ppt
Lecture16-17.ppt
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
 
OS Process synchronization Unit3 synchronization
OS Process synchronization Unit3  synchronizationOS Process synchronization Unit3  synchronization
OS Process synchronization Unit3 synchronization
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptx
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 

Último

UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Christo Ananth
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSrknatarajan
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 

Último (20)

UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 

Peterson Critical Section Problem Solution

  • 1. Presentation on Peterson Algorithm for Critical Section Problem Solution 1 By Bipul Chandra kar (131-15-015 ) Mahadi Hasan (131-15-023) Md.Mohaiminul Islam(132-15-114)
  • 2. Consider system of n processes { P0 , P1 , …, Pn-1 } Each process has a critical section segment of code Process may be changing common variables, updating table, writing file, etc. When one process is in its critical section, no other may be executing in its critical section Critical-section problem is to design a protocol to solve this Each process must ask permission to enter its critical section in entry section, may follow critical section with exit section, the remaining code is in its remainder section 2Critical Section Problem
  • 3. Programs and critical sections  The part of the program (process) that is accessing and changing shared data is called its critical section 3 Change X Change X Change Y Change Y Change Y Change X Process 1 Code Process 2 Code Process 3 Code Assuming X and Y are shared data.
  • 4. Critical Section  The general way to do that is: do { critical section remainder section } while (TRUE) do { entry section critical section exit section remainder } while (TRUE) Entry section will allow only one process to enter and execute critical section code. 4
  • 5. 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 5
  • 6. Peterson’s Solution  Two process solution  Assume that the LOAD and STORE 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! 6
  • 7. Algorithm for Process Pi 7 do { flag[i] = TRUE; turn = j; while (flag[j] && turn == j); critical section flag[i] = FALSE; remainder section } while (1) entry section exit section
  • 8. Two processes executing concurrently 8 do { flag1 = TRUE; turn = 2; while (flag2 && turn == 2); critical section….. flag1 = FALSE; remainder section….. } while (1) do { flag2 = TRUE; turn = 1; while (flag1 && turn == 1); critical section….. flag2 = FALSE; remainder section….. } while (1) PROCESS 1 PROCESS 2 Shared Variables flag1, flag2 turn
  • 9. EXAMPLE 9 Process 0: flag[0] := TRUE turn := 1 check (flag[1] = TRUE and turn = 1) - Condition is false because flag[1] = FALSE - Since condition is false, no waiting in while loop - Enters the critical section Phase-1 Process 1: flag[1] := TRUE turn := 0 check (flag[0] = TRUE and turn = 0) - Since condition is true, it keeps busy waiting until it loses the processor - Process 0 resumes and continues until it finishes in the critical section Phase-2 Process 0: - Leaves critical section Sets flag[0] := FALSE - Start executing the remainder (anything else a process does besides using the critical section) - Process 0 happens to lose the processor Phase-3 Process 1: check (flag[0] = TRUE and turn = 0) - This condition fails because flag[0] = FALSE - No more busy waiting -Enter the critical section Phase-4
  • 10. 10 public class cSection { int turn; boolean flag[] = new boolean[2]; int i = 0, j = 1; // CSC variables int counter = 0;// counter for giving processes an upper bound int cscVar = 13; private class ProcessI extends Thread { // process thread for i @Override public void run() { try { do { flag[i] = true; turn = j; while (flag[j] && turn == j) ; // wait for j to finish // critical section System.out.println("I is in critical section"); cscVar++; System.out.println(cscVar); counter++; System.out.println("counter is " + counter + "n___________"); // flag[i] = false; // remainder section } while (counter < 100); // 100 is upper bound, to remove // infinite looping } Implementation
  • 11. Implementation 11 catch (Exception ex) { ex.printStackTrace(); } } } private class ProcessJ extends Thread { // process thread for j @Override public void run() { try { do { flag[j] = true; turn = i; while (flag[i] && turn == i) ;// wait for i to finish // critical section System.out.println("J is in critical section"); cscVar--; System.out.println(cscVar); counter++; System.out.println("counter is " + counter + "n___________"); // flag[j] = false; // remainder section } while (counter < 100); // 100 is upper bound, to remove // infinite looping }
  • 12. 12 catch (Exception ex) { ex.printStackTrace(); } } } public cSection() { System.out.println("Starting Threads/Processes"); Thread I = new ProcessI(); Thread J = new ProcessJ(); I.start(); // start process i J.start(); // start process j } public static void main(String[] args) { cSection cSec = new cSection(); } } Implementation
  • 13. Conclusion 13 • Simplest algorithm for critical section problem solution. • Peterson algorithm satisfies the three key point of solving the critical section problem
  • 14. Thank You To All . 14