critical section problem - ramakrishna reddy bijjam
TRANSCRIPT
![Page 1: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/1.jpg)
Operating Systems Theory And System Programming
Critical Section
RAMAKRISHNA REDDY BIJJAMAssistant ProfessorAVANTHI PG COLLEGEHyderabad9966484777
![Page 2: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/2.jpg)
Objectives
DefinitionExample of Critical section problemSolution to critical section problem
Software solution Algorithm 1 Algorithm 2 Algorithm 3
Critical Region
![Page 3: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/3.jpg)
Critical Section
When a process is accessing shared modifiable data or a resource that can only operate on behalf of one process at a time , the process is said to be in a critical section.
When one process is in a critical section , all other processes (at least those that access the shared modifiable data and/or resource) are excluded from their critical section.
![Page 4: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/4.jpg)
The Critical-Section Problem
n processes all competing to use some shared data Each process has a code segment, called critical section, in which
the shared data is accessed. Problem – ensure that when one process is executing in its critical
section, no other process is allowed to execute in its critical section.
![Page 5: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/5.jpg)
Example of critical section Transfer Rs. 100 from saving account to checking account
P1 P2Saving = saving – 100 saving = saving * 1.01Checking = checking +100 checking = checking * 101
Initially : saving = 100 checking = 0
P1 ran first & P2 ran first & P1’s first line then P2P2 ran second p1 ran second & P1’s second line
Saving = 0 saving = 1 saving = 0Checking = 101 checking = 100 checking = 100
![Page 6: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/6.jpg)
Solution to Critical-Section Problem1. 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.
![Page 7: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/7.jpg)
Initial Attempts to Solve Problem Only 2 processes, P0 and P1 General structure of process Pi (other process Pj)
do {
entry section
critical section
exit section
reminder section} while (1);
Processes may share some common variables to synchronize their actions.
![Page 8: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/8.jpg)
Algorithm 1 Shared variables:
int turn;initially turn = 0
turn = i Pi can enter its critical section
Process Pido {while (turn != i) ;critical sectionturn = j;reminder section} while (1);
Satisfies mutual exclusion, but not progress
![Page 9: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/9.jpg)
Analysis of Algorithm 1 Does this algorithm satisfy the 3 criteria
mentioned.
◦ Mutual Exclusion ◦ Progress◦ Bounded wait
![Page 10: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/10.jpg)
JAVA Implemantation for the Algorithm 1
public class Algorithm_1 implements MutualExclusion {
private volatile int turn;
public Algorithm_1() {turn = TURN_0;}
public void enteringCriticalSection(int t) {while(turn != t)Thread.yield();}
public void leavingCriticalSection(int t){turn = 1 - t;}}
![Page 11: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/11.jpg)
Algorithm 2 Shared variables
boolean flag[2];initially flag [0] = flag [1] = false.
flag [i] = true Pi ready to enter its critical section
Process Pi
do {flag[i] := true;while (flag[j]) ; critical sectionflag [i] = false;remainder section} while (1);
Satisfies mutual exclusion, but not progress requirement.
![Page 12: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/12.jpg)
Analysis of Algorithm 2 Does this algorithm satisfy the 3 criteria
mentioned.
◦ Mutual Exclusion ◦ Progress◦ Bounded wait
![Page 13: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/13.jpg)
JAVA Implemantation for the Algorithm 2
public class Algorithm_2 implements MutualExclusion {private volatile boolean flag0;private volatile boolean flag1;
public Algorithm_2() {flag0 = false;flag1 = false;
}public void enteringCriticalSection(int t) {
if(t == 0){flag0 = true;while(flag1 == true)
Thread.yield();} else {
flag1 = false;while(flag0 == true)
Thread.yield();}
}
![Page 14: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/14.jpg)
JAVA Implemantation for the Algorithm 2(continue)
public void leavingCriticalSection(int t) {if(t == 0)
flag0 = false;else
flag1 = false;}
}
![Page 15: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/15.jpg)
Algorithm 3 Combined shared variables of algorithms 1 and 2. Process Pi
do {flag [i]:= true;turn = j;while (flag [j] and turn = j) ;critical sectionflag [i] = false;remainder section} while (1);
Meets all three requirements; solves the critical-section problem for two processes.
![Page 16: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/16.jpg)
Analysis of Algorithm 3 Does this algorithm satisfy the 3 criteria
mentioned.
◦ Mutual Exclusion ◦ Progress◦ Bounded wait
![Page 17: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/17.jpg)
JAVA Implemantation for the Algorithm 3
public class Algorithm_3 implements MutualExclusion {private volatile int turn;private volatile boolean flag0;private volatile boolean flag1;
public Algorithm_3() {flag0 = false;flag1 = false;turn = TURN_0;}public void enteringCriticalSection( int t) {int other = 1 - t;turn = other;
if(t == 0) {flag0 = true;while((flag0 == true) && (turn == other))Thread.yield();} else {flag1 = true;while((flag0 == true) && (turn == other))Thread.yield();}}
![Page 18: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/18.jpg)
JAVA Implemantation for the Algorithm 3(continue)
public void leavingCriticalSection( int t) {if(t == 0)
flag0 = false;else
flag1 = false;}
}
![Page 19: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/19.jpg)
Critical Regions High-level synchronization construct A shared variable v of type T, is declared as:
v: shared T Variable v accessed only inside statement
region v when B do S
where B is a boolean expression.
While statement S is being executed, no other process can access variable v.
![Page 20: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/20.jpg)
Critical Regions Regions referring to the same shared
variable exclude each other in time.
When a process tries to execute the region statement, the Boolean expression B is evaluated. If B is true, statement S is executed. If it is false, the process is delayed until B becomes true and no other process is in the region associated with v.
![Page 21: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/21.jpg)
![Page 22: Critical Section Problem - Ramakrishna Reddy Bijjam](https://reader036.vdocuments.mx/reader036/viewer/2022062311/587f59581a28ab0d378b6e33/html5/thumbnails/22.jpg)
Thank You