1. THE CRITICAL-SECTION PROBLEMTHE CRITICAL-SECTION PROBLEM
Video Lecture and E-Content Created byVideo Lecture and E-Content Created by
R.D.SIVAKUMAR,M.Sc.,M.Phil.,M.Tech.,R.D.SIVAKUMAR,M.Sc.,M.Phil.,M.Tech.,
Assistant Professor of Computer Science &Assistant Professor of Computer Science &
Assistant Professor and Head, Department of M.Com.(CA),Assistant Professor and Head, Department of M.Com.(CA),
Ayya Nadar Janaki Ammal College,Ayya Nadar Janaki Ammal College,
Sivakasi – 626 124.Sivakasi – 626 124.
Mobile: 099440-42243Mobile: 099440-42243
e-mail :e-mail : sivamsccsit@gmail.comsivamsccsit@gmail.com website:website: www.rdsivakumar.blogspot.inwww.rdsivakumar.blogspot.in
2. CRITICAL-SECTIONCRITICAL-SECTION
• A section of code (changing commonA section of code (changing common
variables, updating a table) is shared withvariables, updating a table) is shared with
more than one processes is called criticalmore than one processes is called critical
section.section.
3. • All the processes wants to access the sameAll the processes wants to access the same
code. But only one process is desired to be incode. But only one process is desired to be in
its critical section.its critical section.
• A critical section that operates on code.A critical section that operates on code.
Example : Railway Reservation SystemExample : Railway Reservation System
• A solution to the critical section problem mustA solution to the critical section problem must
satisfy the following three requirementssatisfy the following three requirements
4. CRITICAL-SECTION THREE REQUIREMENTSCRITICAL-SECTION THREE REQUIREMENTS
• Mutual Exclusion – At a time, only oneMutual Exclusion – At a time, only one
process should allow to execute in a criticalprocess should allow to execute in a critical
section.section.
• Progress – If no process is in critical sectionProgress – If no process is in critical section
them quickly decide which one process entersthem quickly decide which one process enters
the critical section next.the critical section next.
• Bounded Waiting – It is otherwise called asBounded Waiting – It is otherwise called as
limit waiting. Once a process enters itslimit waiting. Once a process enters its
critical section, it does not get another turncritical section, it does not get another turn
until a waiting process gets a turn.until a waiting process gets a turn.
5. SOLUTIONSOLUTION
• In 1981, G.L. Peterson published a simpler algorithm for twoIn 1981, G.L. Peterson published a simpler algorithm for two
process mutual exclusion.process mutual exclusion.
• Boolean flag [2]Boolean flag [2]
int turn;int turn;
while (true)while (true)
{{
flag [i] = true;flag [i] = true;
turn = j;turn = j;
while (flag [j] && turn == j)while (flag [j] && turn == j)
critical section;critical section;
flag [i] = false;flag [i] = false;
remainder section;remainder section;
}}
6. SOLUTIONSOLUTION
• In 1981, G.L. Peterson published a simpler algorithm for twoIn 1981, G.L. Peterson published a simpler algorithm for two
process mutual exclusion.process mutual exclusion.
• Boolean flag [2]Boolean flag [2]
int turn;int turn;
while (true)while (true)
{{
flag [i] = true;flag [i] = true;
turn = j;turn = j;
while (flag [j] && turn == j)while (flag [j] && turn == j)
critical section;critical section;
flag [i] = false;flag [i] = false;
remainder section;remainder section;
}}
7. SOLUTION CONTI …SOLUTION CONTI …
• The process share two variablesThe process share two variables
boolean flag [2];boolean flag [2];
int turn;int turn;
• The Flag variable indicate if a process isThe Flag variable indicate if a process is
ready to enter the critical section.ready to enter the critical section.
• Flag [0] = true implies that process p0 isFlag [0] = true implies that process p0 is
ready.ready.
• Turn variable indicates where turn it is toTurn variable indicates where turn it is to
enter the critical section.enter the critical section.
8. SOLUTION CONTI …SOLUTION CONTI …
• When process p0 sets flag [0] to true, then p1When process p0 sets flag [0] to true, then p1
can not enter its critical section.can not enter its critical section.
• If p1 is already is in critical section, thenIf p1 is already is in critical section, then
flag [i] = true and p0 is blocked fromflag [i] = true and p0 is blocked from
entering its critical section.entering its critical section.