course: operating systems instructor: m umairm-umair.com/wp-content/uploads/2014/11/5.os... ·...

24
M Umair – http://www.m-umair.com Course: Operating Systems Instructor: M Umair

Upload: others

Post on 30-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

Course: Operating SystemsInstructor: M Umair

Page 2: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

Process Synchronization

M Umair – http://www.m-umair.com

Page 3: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

A cooperating process is one that can affect or be affected byother processes executing in the system.

Concurrent access to shared data may result in datainconsistency.

In this lecture we will discuss various mechanisms to ensure theorderly execution of cooperating processes that share a logicaladdress space, so that data consistency is maintained.

Introduction

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 4: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

The issues related to Inter process Communication are three.

1) How one process can pass information to another.

2) The second has to do with making sure two or more processesdonot get into each other’s way when engaging in criticalactivities (suppose two processes each try to grab the last 1 MB of

memory).

3) The third concerns proper sequencing when dependencies are present.

if process A produces data and process B prints them, B has to wait until A has produced some data before starting to print.

Introduction

{ Ref: Modern Operating Systems, Andrew S. Tanenbaum }

Page 5: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

Consideration the bounded buffer for IPC.

Race Condition

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Page 6: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

Suppose that the value of the variable counter is currently 5 andthat the producer and consumer processes execute the statements“counter++” and “counter--” concurrently.

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

Race Condition

Page 7: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

A situation like this, where several processes access andmanipulate the same data concurrently and the outcome of theexecution depends on the particular order in which the accesstakes place, is called a race condition.

Several 1985-7 deaths of cancer patients were due to overdosesof radiation resulting from a race condition between concurrenttasks in the Therac-25 software.

To guard against the race condition above, we need to ensurethat only one process at a time can be manipulating the variablecounter. To make such a guarantee, we require that the processesbe synchronized in some way.

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

{ Ref: http://courses.cs.vt.edu/~cs3604/lib/Therac_25/Therac_1.html }

Race Condition

Page 8: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

Consider a system consisting of n processes {P0, P1, ..., Pn−1}.

Each process has a segment of code, in which the process maybe changing common variables, updating a table, writing a file,and so on.

Part of the program where the shared resource is accessed iscalled the critical region or critical section.

When one process is executing in its critical section, no otherprocess is to be allowed to execute in its critical section.

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

The Critical Section Problem

Page 9: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }

The Critical Section Problem

Page 10: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

Making sure that if one process is using a shared variable or file,the other processes will be excluded from doing the same thing.This is known as mutual exclusion.

To have a good solution to avoid critical section problem:

i. No two processes may be simultaneously inside theircritical regions.

ii. No assumptions may be made about speeds or thenumber of CPUs.

iii. No process running outside its critical region may blockother processes.

iv. No process should have to wait forever to enter its criticalregion.

The Critical Section Problem

{ Ref: Modern Operating Systems | Andrew S. Tanenbaum }

Page 11: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

The Critical Section Problem

{ Ref: Modern Operating Systems | Andrew S. Tanenbaum }

Page 12: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

Process disable all interrupts just after entering its critical regionand re-enable them just before leaving it.

With interrupts disabled, no clock interrupts can occur.

The CPU is only switched from process to process as a result ofclock or other interrupts.

Problems with this approach ?

The Critical Section Problem - Disabling Interrupts

{ Ref: Modern Operating Systems | Andrew S. Tanenbaum }

Page 13: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

Consider having a single, shared (lock) variable, initially 0. Whena process wants to enter its critical region, it first tests the lock. Ifthe lock is 0, the process sets it to 1 and enters the critical region.

If the lock is already 1, the process just waits until it becomes 0.Thus, a 0 means that no process is in its critical region, and a 1means that some process is in its critical region.

Problem ?

is lock = 1; atomic ?

The Critical Section Problem – Lock Variables

{ Ref: Modern Operating Systems | Andrew S. Tanenbaum }

Page 14: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

The integer variable turn , initially 0, keeps track of whose turnit is to enter the critical region.

Process 0 inspects turn , finds it to be 0, and enters its criticalregion. Process 1 also finds it to be 0 and therefore sits in a tightloop continually testing turn to see when it becomes 1.

Continuously testing a variable until some value appears is called busy waiting.

A lock that uses busy waiting is called a spin lock.

The Critical Section Problem – Strict Alternation

{ Ref: Modern Operating Systems | Andrew S. Tanenbaum }

Page 15: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

The Critical Section Problem – Strict Alternation

{ Ref: Modern Operating Systems | Andrew S. Tanenbaum }

Turn Variable

Process 0

Process 1

Page 16: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

When process 0 leaves the critical region, it sets turn to 1.

Suppose that process 1 finishes its critical region quickly.

Both processes are in their noncritical regions, with turn set to 0.

Now process 0 executes its whole loop quickly, exiting its criticalregion and setting turn to 1.

At this point turn is 1 and both processes are executing in theirnoncritical regions.

The Critical Section Problem – Strict Alternation

{ Ref: Modern Operating Systems | Andrew S. Tanenbaum }

Page 17: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

Suddenly, process 0 finishes its noncritical region and goes backto the top of its loop.

Unfortunately, it is not permitted to enter its critical region now,because turn is 1 and process 1 is busy with its noncritical region.

It hangs in its while loop until process 1 sets turn to 0.

Taking turns is not a good idea when one of the processes ismuch slower than the other.

The Critical Section Problem – Strict Alternation

{ Ref: Modern Operating Systems | Andrew S. Tanenbaum }

Page 18: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

The Critical Section Problem – Peterson’s Solution

{ Ref: Modern Operating Systems | Andrew S. Tanenbaum }

Page 19: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

Modern computer systems provide special hardwareinstructions that allow us either to test and modify the content ofa word or to swap the contents of two words atomically.

Synchronization Hardware

{ Ref: Modern Operating Systems | Andrew S. Tanenbaum }

At start initialize lock to FALSE

Atomic Execution

Page 20: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

A semaphore is an integer variable which is accessed throughtwo special atomic operations, called wait and signal.

Let S be an integer variable, our semaphore, then the classicaldefinition of wait /signal is given by

The Critical Section Problem – Semaphore

{ Ref: http://www.classes.cs.uchicago.edu/archive/2010/fall/51081-1/LabFAQ/lab7/Semaphores.html}

Set S=1 Critical Section

Page 21: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

We can use semaphore to synchronize processes.

Suppose we require that S2 (P1) be executed only after S1 (P0)has completed.

Possible solution is

The Critical Section Problem – Semaphore

{ Ref: http://www.classes.cs.uchicago.edu/archive/2010/fall/51081-1/LabFAQ/lab7/Semaphores.html}

Set S=0

P0 P1

Page 22: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

The Critical Section Problem – Semaphore

{ Ref: http://www.classes.cs.uchicago.edu/archive/2010/fall/51081-1/LabFAQ/lab7/Semaphores.html}

Get into critical section

Avoiding busy waiting using semaphore.

Waiting process gets blocked and placedin waiting queue associated withsemaphore.

wakeup() operation will unblock thewaiting process.

wakeup()

Page 23: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

The Critical Section Problem – Priority Inversion

{ Ref: http://msdn.microsoft.com/en-us/library/aa915356.aspx}

Time P1(High) P2(Mid) P3(Low) Critical SectionOwner

Comments

1 Sleep Sleep Active (Enter

critical section)

P3 P3 locks & enters the critical section

2 Sleep Active (Preempt P3, Some processing)

Sleep P3

3 Active (Preempt P2,

Enter critical section)

Sleep Sleep P3 P1 cant enter critical section. Its locked by P3.

4 Sleep Active Sleep P3

5 Sleep Active Sleep P3

6 Sleep Active Sleep P3

Trouble: A very long wait for high priority process

Page 24: Course: Operating Systems Instructor: M Umairm-umair.com/wp-content/uploads/2014/11/5.OS... · Course: Operating Systems Instructor: M Umair. Process Synchronization ... Process 1

M Umair – http://www.m-umair.com

Solution ?

The low priority process (in execution) will temporarily inheritsthe highest priority.

In this fashion it will never be preempted by medium priorityprocess.

After execution, the low priority process will revert back to itsoriginal priority.

The Critical Section Problem – Priority Inversion

{ Ref: Operating System Concepts 8th Edition | Abraham Silberschatz , Greg Gagne , Peter B. Galvin }