Presentation is loading. Please wait.

Presentation is loading. Please wait.

Critical Sections User Software Solutions Dekker’s Algorithm

Similar presentations


Presentation on theme: "Critical Sections User Software Solutions Dekker’s Algorithm"— Presentation transcript:

1 Critical Sections User Software Solutions Dekker’s Algorithm
Peterson’s Algorithm Hardware Test-and-Set

2 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.

3 Requirements for Solving the CS issue
Mutual Exclusion. If a process is executing in it’s Critical Section, then no other process can execute in it’s Critical Section. Progress. When no process is in a critical section, any process that requests entry to the critical section must be permitted to enter without delay. Bounded Wait. There is an upper bound on the number of times a process can enter it’s critical sections while another is waiting (a.k.a no starvation)

4 Software Solution Attempt 1
Comments: This method insures Mutual Exclusion, however, processone must go first and processone and processtwo must alternate. This method causes busy waiting. (i.e. the use of the CPU to loop, waiting for the condition variable to change). void processOne(void) { while (1) while (processNumber == 2) {// stall} Critical Section processNumber = 2; Other Stuff }

5 Software Solution Attempt 2
Comments: This method allows processes to execute without alternating. If a process is taken off the CPU at {time out}, then it is possible that 2 processes could enter the critical section at the same time void processOne(void) { while (1) while (p2inside) {stall} // time out p1inside = true; Critical Section p1inside = false; Other Stuff }

6 Software Solution 3 Comments: This method provides mutual exclusion The system could deadlock if at the point there //comment appears, process it taken off the processor and P2 then executes the same instruction. void processOne(void) { while (1) P1wantsToEnter = true; // comment while (P2wantsToEnter) {// stall} Critical Section P1wantToEnter = false; Other Stuff }

7 Dekker’s Algorithm Comments: Solves Critical Section problem
Comments: Solves Critical Section problem Has Busy waiting. favoredProcess is used to break a tie. void processOne(void) { while (1) P1wantsToEnter = true; while (P2wantsToEnter) if (favoredProcess == second) P1wantsToEnter = false; while (favoredProcess = second) {// stall} } Critical Section favoredProcess = second; P1wantToEnter = false; Otherstuff

8 Peterson’s Algorithm (1981)
Comments: Same as Dekkers’s but the process that executes: “favoredprocess = other” last will lose the tie breaker. void processOne(void) { while (1) P1wantsToEnter = true; favoredProcess = second; // comment while (P2wantsToEnter AND (favoredProcess == second)) {stall} Critical Section P1wantToEnter = false; Other Stuff }

9 Hardware TestAndSet instruction
TESTANDSET is a hardware instruction (therefore atomic) that reads a variables current value while setting it to a new value all in 1 machine instruction. Read the value of X while setting X to 0 X is a variable with values of 0(not available) or 1(available) process() { a = 0; (while a == 0) { a = TESTANDSET(X) } Critical Section X = 1; }


Download ppt "Critical Sections User Software Solutions Dekker’s Algorithm"

Similar presentations


Ads by Google