SlideShare une entreprise Scribd logo
1  sur  24
The
Critical-Section
Problem
MOHITDADU
The Critical-Section
A code segment that accesses shared variables (or other shared resources)
and that has to be executed as an atomic action is referred to as a critical
section.
n processes competing to use some shared data.
No assumptions may be made about speeds or the number of CPUs.
Each process has a code segment, called Critical Section (CS), in which
the shared data is accessed.
Problem – ensure that when one process is executing in its CS, no other
process is allowed to execute in its CS.
informally,
a critical section is a code segment that accesses shared variables and
has to be executed as an atomic action.
The critical section problem refers to the problem of how to ensure that
at most one process is executing its critical section at a given time.
Important: critical sections in different threads are not necessarily the
same code segment!
PROBLEM DESCRIPTION
PROBLEM DESCRIPTION
Formally,
Mutual exclusion: when a thread is executing in its critical section, no other
threads can be executing in their critical sections.
Progress: if no thread is executing in its critical section, and if there are
some threads that wish to enter their critical sections, then one of these threads
will get into the critical section.
Bounded waiting: after a thread makes a request to enter its critical section,
there is a bound on the number of times that other threads are allowed to enter
their critical sections, before the request is granted.
CS Problem Dynamics (1)
When a process executes code that manipulates shared data (or
resource), we say that the process is in it’s Critical Section (for that
shared data).
The execution of critical sections must be mutually exclusive: at any
time, only one process is allowed to execute in its critical section
(even with multiple processors).
So each process must first request permission to enter its critical
section.
CS Problem Dynamics (2)
The section of code implementing this request is called the Entry Section (ES).
The critical section (CS) might be followed by a Leave/Exit Section (LS).
The remaining code is the Remainder Section (RS).
The critical section problem is to design a protocol that the processes can use so
that their action will not depend on the order in which their execution is
interleaved (possibly on many processors).
General structure of process
while (true)
{
Entry-Section
Critical section
// contains accesses to shared variables or other resources.
Exit-Section
Non-critical section
// a thread may terminate its execution in this section.
}
EXPLANATION
 ENTRY SECTION:- The process will request to enter the critical section
then a part of code decide wheather process can enter in the Critical
Section on not.
 CRITICAL SECTION:- A code segment that accesses shared resources and
that has to be executed as an atomic action.
 EXIT SECTION:- Locking section can be undone which is done in Critical
Section.
 REMAINDER SECTION:- Remaining part of the program .
Solution to Critical-Section Problem
There are 3 requirements that must stand for a correct solution:
1. Mutual Exclusion
2. Progress
3. Bounded Waiting
We can check on all three requirements in each proposed solution,
even though the non-existence of each one of them is enough for an
incorrect solution.
Solution to CS Problem – Mutual Exclusion
1. Mutual Exclusion –
If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.
Whenever one process is entered in the critical section then there is no other
process can enter in the critical section.
Only one process can be execute at a time.
That means access to the critical section must be mutually exclusive.
 Critical sections better be focused and short.
 Better not get into an infinite loop in there.
 If a process somehow halts/waits in its critical section, it
must not interfere with other processes.
IMPLICATIONS:
Solution to CS Problem– Progress
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 process that will enter the critical section next cannot be
postponed indefinitely:
If only one process wants to enter, it should be able to.
If two or more want to enter, one of them should succeed.
Solution to CS Problem– Bounded Waiting
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 non zero speed.
• No assumption concerning relative speed of the n processes.
Types of solutions to CS
problem
Software Solutions :– Algorithms who’s correctness does not rely
on any other assumptions.
Hardware Solutions :– Rely on some special machine instructions.
Operating System solutions :– Provide some functions and data
structures to the programmer through system /library calls.
Programming Language solutions :– Linguistic constructs
provided as part of a language.
Each process disables all interrupts just after entering in its critical
section and re-enable all interrupts just before leaving critical section.
With interrupts turned off the CPU could not be switched to other
process.
Hence, no other process will enter its critical and mutual exclusion
achieved.
Disabling Interrupts
(Hardware Solution)
Disabling interrupts is sometimes a useful technique within the kernel
of an operating system,
But it is not appropriate as a general mutual exclusion mechanism for
users process.
The reason is that it is unwise to give user process the power to turn
off interrupts.
Conclusion
• In this solution, we consider a single, shared, (lock) variable, initially 0.
• When a process wants to enter in its critical section, it first test the lock.
• If lock is 0, the process first sets it to 1 and then enters the critical section.
• If the lock is already 1, the process just waits until (lock) variable becomes
0.
• Thus, a 0 means that no process in its critical section, and 1 means hold
your horses - some process is in its critical section.
Lock Variable
(Software Solution)
Drawbacks of
software solutions
Software solutions are very delicate .
Processes that are requesting to enter their critical section
are busy waiting (consuming processor time needlessly).
If critical sections are long, it would be more efficient to block
processes that are waiting.
Initial Attempts to Solve
Problem
Threads T0 and T1 use variables flag[i] and flag[j] to indicate their intention to enter their
critical section. A thread will not enter its critical section if the other thread has already
signaled its intention to enter.
boolean flag[i]=false , flag[j]=false ;
Incorrect Solution 1
T1
while (true) {
while (flag[ i ]) { ; } (1)
flag[ j ] = true; (2)
critical section (3)
flag[ j ] = false; (4)
non-critical section (5)
}
T0
while (true) {
while (flag[ j ]) { ; } (1)
flag[ i ] = true; (2)
critical section (3)
flag[ i ] = false; (4)
non-critical section (5)
}
This solution does not guarantee mutual exclusion.
T0
WHILE (TRUE) {
WHILE (TURN != 0) { ; } (1)
CRITICAL SECTION (2)
TURN = 1; (3)
NON-CRITICAL SECTION (4)
}
Global variable turn is used to indicate which thread is allowed to enter its
critical section, i.e., the threads take turns entering their critical sections. The
initial value of turn can be 0 or 1.
int turn = 1;
Incorrect Solution 2
T1
while (true) {
while (turn != 1) { ; } (1)
critical section (2)
turn = 0; (3)
non-critical section (4)
}
Thread T0 cannot exit the loop in (1) since the value of turn is 1 and turn will never be
changed by T1.
T0
while (true) {
flag0= true; (1)
while ( flag1) { (2)
flag0= false; (3)
while( flag1) {;} (4)
flag0= true; (5)
}
critical section (6)
Flag0= false; (7)
non-critical section (8)
}
T1
while ( true ) {
flag1= true; (1)
while ( Flag0) { (2)
flag1= false; (3)
while( Flag0) {;} (4)
flag1= true; (5)
}
critical section (6)
flag1= false; (7)
non-critical section (8)
}
Incorrect Solution 3
When one thread finds that the other thread also intends to enter its critical section, it
sets its own flag to false and waits for the other thread to exit its critical section.
Thus, the mutual exclusion requirement is satisfied.
In this execution sequence,
T0 enters its critical section infinitely often and
T1 waits forever to enter its critical section.
Thus, this solution does not guarantee bounded waiting.
This solution ensures that when both Flag0 and Flag1 are true,
only one of T0 and T1 is allowed to enter its critical section.
Correct solution:
Correct solution is a combination of solutions (2) and (3). If both threads intend to enter their
critical sections, then turn is used to break the tie.
boolean Flag0 = false , Flag1= false;
int turn; // no initial value for turn is needed.
T0
while (true) {
Flag0= true; (1)
turn = 1; (2)
while ( Flag1&& (3)
Turn == 1) { ; }
critical section (4)
Flag0= false; (5)
non-critical section (6)
}
T1
while (true) {
Flag1= true; (1)
turn = 0; (2)
while ( Flag0&& (3)
Turn == 0) { ; }
critical section (4)
Flag1= false; (5)
non-critical section (6)
}

Contenu connexe

Tendances

OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process ConceptsMukesh Chinta
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionBipul Chandra Kar
 
Dead Lock in operating system
Dead Lock in operating systemDead Lock in operating system
Dead Lock in operating systemAli Haider
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OSMsAnita2
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Planning in AI(Partial order planning)
Planning in AI(Partial order planning)Planning in AI(Partial order planning)
Planning in AI(Partial order planning)Vicky Tyagi
 
deadlock handling
deadlock handlingdeadlock handling
deadlock handlingSuraj Kumar
 
directory structure and file system mounting
directory structure and file system mountingdirectory structure and file system mounting
directory structure and file system mountingrajshreemuthiah
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Kumar
 
Operating Systems Process Scheduling Algorithms
Operating Systems   Process Scheduling AlgorithmsOperating Systems   Process Scheduling Algorithms
Operating Systems Process Scheduling Algorithmssathish sak
 
Methods for handling deadlocks
Methods for handling deadlocksMethods for handling deadlocks
Methods for handling deadlocksA. S. M. Shafi
 

Tendances (20)

OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
Dead Lock in operating system
Dead Lock in operating systemDead Lock in operating system
Dead Lock in operating system
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OS
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Planning in AI(Partial order planning)
Planning in AI(Partial order planning)Planning in AI(Partial order planning)
Planning in AI(Partial order planning)
 
deadlock handling
deadlock handlingdeadlock handling
deadlock handling
 
directory structure and file system mounting
directory structure and file system mountingdirectory structure and file system mounting
directory structure and file system mounting
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
1.prallelism
1.prallelism1.prallelism
1.prallelism
 
operating system structure
operating system structureoperating system structure
operating system structure
 
Software Engineering Practice
Software Engineering PracticeSoftware Engineering Practice
Software Engineering Practice
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20
 
Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
 
Operating Systems Process Scheduling Algorithms
Operating Systems   Process Scheduling AlgorithmsOperating Systems   Process Scheduling Algorithms
Operating Systems Process Scheduling Algorithms
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
Methods for handling deadlocks
Methods for handling deadlocksMethods for handling deadlocks
Methods for handling deadlocks
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Bankers
BankersBankers
Bankers
 

En vedette

Race conditions
Race conditionsRace conditions
Race conditionsMohd Arif
 
Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS BasicsShijin Raj P
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating SystemsRitu Ranjan Shrivastwa
 
Tutorial on Parallel Computing and Message Passing Model - C2
Tutorial on Parallel Computing and Message Passing Model - C2Tutorial on Parallel Computing and Message Passing Model - C2
Tutorial on Parallel Computing and Message Passing Model - C2Marcirio Chaves
 
Parallel architecture-programming
Parallel architecture-programmingParallel architecture-programming
Parallel architecture-programmingShaveta Banda
 
Intro to OpenMP
Intro to OpenMPIntro to OpenMP
Intro to OpenMPjbp4444
 
OpenMP Tutorial for Beginners
OpenMP Tutorial for BeginnersOpenMP Tutorial for Beginners
OpenMP Tutorial for BeginnersDhanashree Prasad
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel ProgrammingUday Sharma
 
Semaphores-R.D.Sivakumar
Semaphores-R.D.SivakumarSemaphores-R.D.Sivakumar
Semaphores-R.D.SivakumarSivakumar R D .
 
Bootloader and bootloading
Bootloader and bootloadingBootloader and bootloading
Bootloader and bootloadingArpita Gupta
 
deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidancewahab13
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of VariableMOHIT DADU
 
Parallel computing
Parallel computingParallel computing
Parallel computingvirend111
 
Operating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationOperating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationEmery Berger
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic CommandsHanan Nmr
 
Linux booting process!!
Linux booting process!!Linux booting process!!
Linux booting process!!sourav verma
 

En vedette (20)

Race conditions
Race conditionsRace conditions
Race conditions
 
Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS Basics
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
openmp
openmpopenmp
openmp
 
Tutorial on Parallel Computing and Message Passing Model - C2
Tutorial on Parallel Computing and Message Passing Model - C2Tutorial on Parallel Computing and Message Passing Model - C2
Tutorial on Parallel Computing and Message Passing Model - C2
 
Open mp intro_01
Open mp intro_01Open mp intro_01
Open mp intro_01
 
Openmp
OpenmpOpenmp
Openmp
 
Parallel architecture-programming
Parallel architecture-programmingParallel architecture-programming
Parallel architecture-programming
 
Intro to OpenMP
Intro to OpenMPIntro to OpenMP
Intro to OpenMP
 
OpenMP Tutorial for Beginners
OpenMP Tutorial for BeginnersOpenMP Tutorial for Beginners
OpenMP Tutorial for Beginners
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
Semaphores-R.D.Sivakumar
Semaphores-R.D.SivakumarSemaphores-R.D.Sivakumar
Semaphores-R.D.Sivakumar
 
Bootloader and bootloading
Bootloader and bootloadingBootloader and bootloading
Bootloader and bootloading
 
Semaphore
SemaphoreSemaphore
Semaphore
 
deadlock avoidance
deadlock avoidancedeadlock avoidance
deadlock avoidance
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 
Parallel computing
Parallel computingParallel computing
Parallel computing
 
Operating Systems - Advanced Synchronization
Operating Systems - Advanced SynchronizationOperating Systems - Advanced Synchronization
Operating Systems - Advanced Synchronization
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic Commands
 
Linux booting process!!
Linux booting process!!Linux booting process!!
Linux booting process!!
 

Similaire à Critical section problem in operating system.

Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxAmanuelmergia
 
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 synchronizationAbeera Naeem
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksMukesh Chinta
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmSouvik Roy
 
MODULE 3 process synchronizationnnn.pptx
MODULE 3 process synchronizationnnn.pptxMODULE 3 process synchronizationnnn.pptx
MODULE 3 process synchronizationnnn.pptxsenthilkumar969017
 
Lecture 9 - Process Synchronization.pptx
Lecture 9 - Process Synchronization.pptxLecture 9 - Process Synchronization.pptx
Lecture 9 - Process Synchronization.pptxEhteshamulIslam1
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.pptjayverma27
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptxAbdullahBhatti53
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Ra'Fat Al-Msie'deen
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfAmanuelmergia
 
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
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationAnas Ebrahim
 
Slides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfSlides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfGeekyHassan
 
Ch17 OS
Ch17 OSCh17 OS
Ch17 OSC.U
 

Similaire à Critical section problem in operating system. (20)

Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptx
 
Critical section operating system
Critical section  operating systemCritical section  operating system
Critical section operating system
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and Deadlocks
 
CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
 
MODULE 3 process synchronizationnnn.pptx
MODULE 3 process synchronizationnnn.pptxMODULE 3 process synchronizationnnn.pptx
MODULE 3 process synchronizationnnn.pptx
 
Lecture 9 - Process Synchronization.pptx
Lecture 9 - Process Synchronization.pptxLecture 9 - Process Synchronization.pptx
Lecture 9 - Process Synchronization.pptx
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
Synchronization in os.pptx
Synchronization in os.pptxSynchronization in os.pptx
Synchronization in os.pptx
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdf
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Critical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy BijjamCritical Section Problem - Ramakrishna Reddy Bijjam
Critical Section Problem - Ramakrishna Reddy Bijjam
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
 
Lecture16-17.ppt
Lecture16-17.pptLecture16-17.ppt
Lecture16-17.ppt
 
Slides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdfSlides for OS 06-Sync.pdf
Slides for OS 06-Sync.pdf
 
Ch17 OS
Ch17 OSCh17 OS
Ch17 OS
 
OSCh17
OSCh17OSCh17
OSCh17
 

Dernier

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
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
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
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
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Dernier (20)

(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
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
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
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
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
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
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 

Critical section problem in operating system.

  • 2. The Critical-Section A code segment that accesses shared variables (or other shared resources) and that has to be executed as an atomic action is referred to as a critical section. n processes competing to use some shared data. No assumptions may be made about speeds or the number of CPUs. Each process has a code segment, called Critical Section (CS), in which the shared data is accessed. Problem – ensure that when one process is executing in its CS, no other process is allowed to execute in its CS.
  • 3. informally, a critical section is a code segment that accesses shared variables and has to be executed as an atomic action. The critical section problem refers to the problem of how to ensure that at most one process is executing its critical section at a given time. Important: critical sections in different threads are not necessarily the same code segment! PROBLEM DESCRIPTION
  • 4. PROBLEM DESCRIPTION Formally, Mutual exclusion: when a thread is executing in its critical section, no other threads can be executing in their critical sections. Progress: if no thread is executing in its critical section, and if there are some threads that wish to enter their critical sections, then one of these threads will get into the critical section. Bounded waiting: after a thread makes a request to enter its critical section, there is a bound on the number of times that other threads are allowed to enter their critical sections, before the request is granted.
  • 5. CS Problem Dynamics (1) When a process executes code that manipulates shared data (or resource), we say that the process is in it’s Critical Section (for that shared data). The execution of critical sections must be mutually exclusive: at any time, only one process is allowed to execute in its critical section (even with multiple processors). So each process must first request permission to enter its critical section.
  • 6. CS Problem Dynamics (2) The section of code implementing this request is called the Entry Section (ES). The critical section (CS) might be followed by a Leave/Exit Section (LS). The remaining code is the Remainder Section (RS). The critical section problem is to design a protocol that the processes can use so that their action will not depend on the order in which their execution is interleaved (possibly on many processors).
  • 7. General structure of process while (true) { Entry-Section Critical section // contains accesses to shared variables or other resources. Exit-Section Non-critical section // a thread may terminate its execution in this section. }
  • 8. EXPLANATION  ENTRY SECTION:- The process will request to enter the critical section then a part of code decide wheather process can enter in the Critical Section on not.  CRITICAL SECTION:- A code segment that accesses shared resources and that has to be executed as an atomic action.  EXIT SECTION:- Locking section can be undone which is done in Critical Section.  REMAINDER SECTION:- Remaining part of the program .
  • 9. Solution to Critical-Section Problem There are 3 requirements that must stand for a correct solution: 1. Mutual Exclusion 2. Progress 3. Bounded Waiting We can check on all three requirements in each proposed solution, even though the non-existence of each one of them is enough for an incorrect solution.
  • 10. Solution to CS Problem – Mutual Exclusion 1. Mutual Exclusion – If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. Whenever one process is entered in the critical section then there is no other process can enter in the critical section. Only one process can be execute at a time. That means access to the critical section must be mutually exclusive.
  • 11.  Critical sections better be focused and short.  Better not get into an infinite loop in there.  If a process somehow halts/waits in its critical section, it must not interfere with other processes. IMPLICATIONS:
  • 12. Solution to CS Problem– Progress 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 process that will enter the critical section next cannot be postponed indefinitely: If only one process wants to enter, it should be able to. If two or more want to enter, one of them should succeed.
  • 13. Solution to CS Problem– Bounded Waiting 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 non zero speed. • No assumption concerning relative speed of the n processes.
  • 14. Types of solutions to CS problem Software Solutions :– Algorithms who’s correctness does not rely on any other assumptions. Hardware Solutions :– Rely on some special machine instructions. Operating System solutions :– Provide some functions and data structures to the programmer through system /library calls. Programming Language solutions :– Linguistic constructs provided as part of a language.
  • 15. Each process disables all interrupts just after entering in its critical section and re-enable all interrupts just before leaving critical section. With interrupts turned off the CPU could not be switched to other process. Hence, no other process will enter its critical and mutual exclusion achieved. Disabling Interrupts (Hardware Solution)
  • 16. Disabling interrupts is sometimes a useful technique within the kernel of an operating system, But it is not appropriate as a general mutual exclusion mechanism for users process. The reason is that it is unwise to give user process the power to turn off interrupts. Conclusion
  • 17. • In this solution, we consider a single, shared, (lock) variable, initially 0. • When a process wants to enter in its critical section, it first test the lock. • If lock is 0, the process first sets it to 1 and then enters the critical section. • If the lock is already 1, the process just waits until (lock) variable becomes 0. • Thus, a 0 means that no process in its critical section, and 1 means hold your horses - some process is in its critical section. Lock Variable (Software Solution)
  • 18. Drawbacks of software solutions Software solutions are very delicate . Processes that are requesting to enter their critical section are busy waiting (consuming processor time needlessly). If critical sections are long, it would be more efficient to block processes that are waiting.
  • 19. Initial Attempts to Solve Problem
  • 20. Threads T0 and T1 use variables flag[i] and flag[j] to indicate their intention to enter their critical section. A thread will not enter its critical section if the other thread has already signaled its intention to enter. boolean flag[i]=false , flag[j]=false ; Incorrect Solution 1 T1 while (true) { while (flag[ i ]) { ; } (1) flag[ j ] = true; (2) critical section (3) flag[ j ] = false; (4) non-critical section (5) } T0 while (true) { while (flag[ j ]) { ; } (1) flag[ i ] = true; (2) critical section (3) flag[ i ] = false; (4) non-critical section (5) } This solution does not guarantee mutual exclusion.
  • 21. T0 WHILE (TRUE) { WHILE (TURN != 0) { ; } (1) CRITICAL SECTION (2) TURN = 1; (3) NON-CRITICAL SECTION (4) } Global variable turn is used to indicate which thread is allowed to enter its critical section, i.e., the threads take turns entering their critical sections. The initial value of turn can be 0 or 1. int turn = 1; Incorrect Solution 2 T1 while (true) { while (turn != 1) { ; } (1) critical section (2) turn = 0; (3) non-critical section (4) } Thread T0 cannot exit the loop in (1) since the value of turn is 1 and turn will never be changed by T1.
  • 22. T0 while (true) { flag0= true; (1) while ( flag1) { (2) flag0= false; (3) while( flag1) {;} (4) flag0= true; (5) } critical section (6) Flag0= false; (7) non-critical section (8) } T1 while ( true ) { flag1= true; (1) while ( Flag0) { (2) flag1= false; (3) while( Flag0) {;} (4) flag1= true; (5) } critical section (6) flag1= false; (7) non-critical section (8) } Incorrect Solution 3 When one thread finds that the other thread also intends to enter its critical section, it sets its own flag to false and waits for the other thread to exit its critical section.
  • 23. Thus, the mutual exclusion requirement is satisfied. In this execution sequence, T0 enters its critical section infinitely often and T1 waits forever to enter its critical section. Thus, this solution does not guarantee bounded waiting. This solution ensures that when both Flag0 and Flag1 are true, only one of T0 and T1 is allowed to enter its critical section.
  • 24. Correct solution: Correct solution is a combination of solutions (2) and (3). If both threads intend to enter their critical sections, then turn is used to break the tie. boolean Flag0 = false , Flag1= false; int turn; // no initial value for turn is needed. T0 while (true) { Flag0= true; (1) turn = 1; (2) while ( Flag1&& (3) Turn == 1) { ; } critical section (4) Flag0= false; (5) non-critical section (6) } T1 while (true) { Flag1= true; (1) turn = 0; (2) while ( Flag0&& (3) Turn == 0) { ; } critical section (4) Flag1= false; (5) non-critical section (6) }