critical section problem - ramakrishna reddy bijjam
TRANSCRIPT
Operating Systems Theory And System Programming
Critical Section
RAMAKRISHNA REDDY BIJJAMAssistant ProfessorAVANTHI PG COLLEGEHyderabad9966484777
Objectives
DefinitionExample of Critical section problemSolution to critical section problem
Software solution Algorithm 1 Algorithm 2 Algorithm 3
Critical Region
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.
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.
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
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.
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.
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
Analysis of Algorithm 1 Does this algorithm satisfy the 3 criteria
mentioned.
◦ Mutual Exclusion ◦ Progress◦ Bounded wait
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;}}
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.
Analysis of Algorithm 2 Does this algorithm satisfy the 3 criteria
mentioned.
◦ Mutual Exclusion ◦ Progress◦ Bounded wait
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();}
}
JAVA Implemantation for the Algorithm 2(continue)
public void leavingCriticalSection(int t) {if(t == 0)
flag0 = false;else
flag1 = false;}
}
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.
Analysis of Algorithm 3 Does this algorithm satisfy the 3 criteria
mentioned.
◦ Mutual Exclusion ◦ Progress◦ Bounded wait
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();}}
JAVA Implemantation for the Algorithm 3(continue)
public void leavingCriticalSection( int t) {if(t == 0)
flag0 = false;else
flag1 = false;}
}
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.
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.
Thank You