operating system critical section

22
Operating Systems Theory And System Programming (CSM 31122) Critical Section JMHM Jayamaha SEU/IS/10/PS/104 PS0372

Upload: harshana-madusanka-jayamaha

Post on 09-Aug-2015

63 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Operating system   critical section

Operating Systems Theory And System Programming(CSM 31122)

Critical Section

JMHM Jayamaha SEU/IS/10/PS/104

PS0372

Page 2: Operating system   critical section

DefinitionExample of Critical section problemSolution to critical section problem

Software solution Algorithm 1 Algorithm 2 Algorithm 3

Critical Region

Objectives

Page 3: Operating system   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.

Critical Section

Page 4: Operating system   critical section

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.

The Critical-Section Problem

Page 5: Operating system   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

Example of critical section

Page 6: Operating system   critical section

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.

Solution to Critical-Section Problem

Page 7: Operating system   critical section

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: Operating system   critical section

Algorithm 1 Shared variables:

int turn;initially turn = 0

turn = i Pi can enter its critical section

Process Pi

do {while (turn != i) ;critical sectionturn = j;reminder section} while (1);

Satisfies mutual exclusion, but not progress

Page 9: Operating system   critical section

Analysis of Algorithm 1

Does this algorithm satisfy the 3 criteria mentioned.

◦ Mutual Exclusion ◦ Progress◦ Bounded wait

Page 10: Operating system   critical section

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;

}}

JAVA Implemantation for the Algorithm 1

Page 11: Operating system   critical section

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 section

flag [i] = false;

remainder section

} while (1);

Satisfies mutual exclusion, but not progress requirement.

Page 12: Operating system   critical section

Analysis of Algorithm 2

Does this algorithm satisfy the 3 criteria mentioned.

◦ Mutual Exclusion ◦ Progress◦ Bounded wait

Page 13: Operating system   critical section

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

Page 14: Operating system   critical section

public void leavingCriticalSection(int t) {if(t == 0)

flag0 = false;else

flag1 = false;}

}

JAVA Implemantation for the Algorithm 2(continue)

Page 15: Operating system   critical section

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: Operating system   critical section

Analysis of Algorithm 3

Does this algorithm satisfy the 3 criteria mentioned.

◦ Mutual Exclusion ◦ Progress◦ Bounded wait

Page 17: Operating system   critical section

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

Page 18: Operating system   critical section

public void leavingCriticalSection( int t) {if(t == 0)

flag0 = false;else

flag1 = false;}

}

JAVA Implemantation for the Algorithm 3(continue)

Page 19: Operating system   critical section

Critical Regions

High-level synchronization construct A shared variable v of type T, is declared

as:v: shared T

Variable v accessed only inside statementregion 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: Operating system   critical section

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: Operating system   critical section
Page 22: Operating system   critical section

Thank You All