06 lcd slides 1 - process synchronization powerpoint

14
Process Synchronization Race Conditions The Critical Section Semaphores Classic Synchronization Problems

Upload: anne-lee

Post on 10-Apr-2017

416 views

Category:

Software


0 download

TRANSCRIPT

Page 1: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

Process Synchronization

Race Conditions The Critical Section Semaphores Classic Synchronization Problems

Page 2: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Race Conditions Race conditions usually occur if two or more processes are allowed to modify

the same shared variable at the same time.

To prevent race conditions, the operating system must perform process synchronization to guarantee that only one process is updating a shared variable at any one time.

Page 3: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Critical Section part of the process that contains the instruction or instructions that access a

shared variable or resource

Process P1

.

.

.

mov ax, counter

inc ax

mov counter, ax

.

.

.

Process P2

.

.

.

mov ax, counter

inc ax

mov counter, ax

.

.

.

Critical Section of Process

P1

Critical Section of Process

P2

Figure 6.1 Critical Sections for Process P1 and P2

Page 4: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Critical SectionSoftware solutions to the critical section problem:

SOLUTION 1Process P0

.

.

.

while (TurnToEnter != 0) { };

TurnToEnter = 1;

.

.

.

critical section

Process P1

.

.

.

while (TurnToEnter != 1) { };

TurnToEnter = 0;

.

.

.

critical section

Figure 6.2 Critical Sections of Processes P1 and P2 for Solution 1

Page 5: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Critical SectionSOLUTION 2

Process P0

.

.

.

WantToEnter[0] = true; while (WantToEnter[1] == true) { };

WantToEnter[0] = false;

.

.

.

critical section

Process P1

.

.

.

WantToEnter[1] = true; while (WantToEnter[0] == true) { };

WantToEnter[1] = false;

.

.

.

critical section

Figure 6.3 Critical Section P1 and P2 for Solution 2

Page 6: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Critical SectionSOLUTION 3 (Peterson’s Algorithm)

Process P0

.

.

.

WantToEnter[0] = true; TurnToEnter = 1; while (WantToEnter[1] == true && TurnToEnter == 1) { };

WantToEnter[0] = false;

.

.

.

critical section

Process P1

.

.

.

WantToEnter[1] = true; TurnToEnter = 0; while (WantToEnter[0] == true && TurnToEnter == 0) { };

WantToEnter[1] = false;

.

.

.

critical section

Figure 6.4 Critical Sections of Processes P1 and P2 for Solution 3

Page 7: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Critical SectionSOLUTION TO THE CRITICAL SECTION PROBLEM INVOLVING SEVERAL PROCESS ( Bakery Algorithm)

Process Pi

.

.

.

choosing[i] = true; number[i] = max(number[0], ... , number[n – 1]) + 1; choosing[i] = false;

for (j = 1; j < n; j++) { while (choosing[j] == true) { } while ((number[j] != 0) &&

((number[j] < number[i]) || (number[j] == number[i]) && (j < i))) { }; }

number[i] = 0;

.

.

.

critical section

Figure 6.5 Critical Section of Process P1 for the Bakery Algorithm

Page 8: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Critical SectionHardware solutions to the critical section problem: Disabling interrupts Special hardware instructions

boolean test_and_set (boolean &i){ boolean val;

if (*i == false) { val = false; *i = true; } else val = true;

return val;}

These instructions are executed atomically (uninterruptible)

Figure 6.6 Definition of test_and set instruction

Page 9: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Critical Section

.

.

.

while (test_and_set (&lock) == true) { };

*lock := false;

.

.

.

critical section

Process Pi

Figure 6.7 Using the test_and_set instruction to solve the critical section problem

Page 10: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

SemaphoresSemaphore is a tool that can easily be used to solve more complex synchronization problems and does not use busy waiting.

.

.

.

wait(mutex);

signal(mutex);

.

.

.

critical section

Process Pi

Figure 6.8 Using Semaphores to Solve the Critical Section Problem

Page 11: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Classic Synchronization ProblemsThe Dining Philosophers Problem

Restrictions:1. A philosopher cannot start eating unless he has both forks.2. A philosopher cannot pick up both forks at the same time. He has to do it one at a

time.3. He cannot get the fork that is being used by the philosopher to his right or to his left.

Figure 6.9 The Dining Philosophers Problem

Page 12: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Classic Synchronization Problems

.

.

.

wait(LeftFork); wait(RightFork); eat; signal(RightFork); signal(LeftFork);

.

.

.

Figure 6.10 Solution to the Dining Philosophers Problem

Page 13: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Classic Synchronization ProblemsThe Readers and Writers Problem

.

.

.

wait(wrt);

signal(wrt);

.

.

.

start writing data

Writer Process

Figure 6.11 Using Semaphores to Implement a Writer Process

Page 14: 06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT

*Property of STI J0024

Classic Synchronization Problems

.

.

.

wait(mutex); readcount++; if readcount ==1 wait(wrt); signal(mutex);

wait(mutex);readcount--;If readcount == 0 signal(wrt);signal(mutex);

.

.

.

start reading data

Reader Process

Figure 6.12 Using Semaphores to Implement a Reader Process