process synchronization and deadlocks 119961623287018 3

29
Process Synchronization and Deadlocks In a nutshell

Upload: dasaradh-dev

Post on 06-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 1/29

Process Synchronization and

Deadlocks

In a nutshell

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 2/29

Content

Motivation

Race Condition

Critical Section problem & Solutions Classical problems in Synchronization

Deadlocks

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 3/29

Why study these chapters?

This is about getting processes to

coordinate with each other.

How do processes work with resources

that must be shared between them

Very interesting concepts!

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 4/29

 A race condition example

A race condition is where multiple processes/threadsconcurrently read and write to a shared memory locationand the result depends on the order of the execution. ± This was the cause of a patient death on a radiation therapy

machine, the Therac-25 http://sunnyday.mit.edu/therac-25.html

Yakima Software flow

Also can happen in bank account database transactions

with, say a husband and a wife accessing the sameaccount simultaneously from different  ATMs

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 5/29

 A race condition example (2)

We will implement count++ and count--

and run them concurrently

 ± Let us say they are executed by differentthreads accessing a global variable

 ±  At the end we expect count's value not to

change

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 6/29

 A race condition example (3)

count++ implementation: ± register1 = count

 ± register1 = register1 + 1

 ± count = register 1

count-- implementation: ± register2 = count

 ± register2 = register2 - 1 ± count = register2

Let count = 5 initially. One possible concurrent execution of 

count++ and count-- is

register1 = count {register1 = 5}

register1 = register1 + 1 {register1 = 6}

register2 = count {register2 = 5}

register2 = register2 - 1 {register2 = 4}count = register1 {count = 6}

count = register2 {count = 4} ± count = 4 after count++ and count--, even though we started with count = 5

 ± Easy question: what other values can count be from doing this incorrectly?

Obviously, we would like to have count++ execute, followed by count-- (or vice versa)

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 7/29

 A race condition example (4)

Producer/consumer problem is more

general form of the previous problem.

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 8/29

Critical Sections

A critical section is a piece of code thataccesses a shared resource (data structure or device) that must not be concurrently accessedby more than one thread of execution.

The goal is to provide a mechanism by whichonly one instance of a critical section isexecuting for a particular shared resource.

Unfortunately, it is often very difficult to detectcritical section bugs

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 9/29

Critical Sections (2)

A Critical Section Environment contains:

 ± Entry Section Code requesting entry into the

critical section.

 ± Critical Section Code in which only one

process can execute at any one time.

 ± Exit Section The end of the critical section,

releasing or allowing others in. ± Remainder Section Rest of the code  AFTER

the critical section.

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 10/29

Critical Sections (3)

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 11/29

Solution to Critical-Section

Problem The critical section must ENFORCE  ALL THREE of thefollowing rules:

1. Mutual Exclusion - If process Pi is executing in its critical section,then no other processes can be executing in their critical sections

 ± In many calls, this is abbreviated mutex2. Progress - If no process is executing in its critical section and thereexist some processes that wish to enter their critical section, thenthe selection of the processes that will enter the critical section nextcannot be postponed indefinitely

3. Bounded Waiting - A bound must exist on the number of times thatother processes are allowed to enter their critical sections after a

process has made a request to enter its critical section and beforethat request is granted ± Assume that each process executes at a nonzero speed

 ± No assumption concerning relative speed of the N processes

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 12/29

Critical Section Solutions

Hardware

 ± Many systems provide hardware support for critical

section code

Uniprocessors ± could disable interrupts ± Currently running code would execute without preemption

 ± Generally too inefficient on multiprocessor systems

» Have to wait for disable to propagate to all processors

» Operating systems using this not broadly scalable

Modern machines provide special atomic hardwareinstructions

» Atomic = non-interruptable

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 13/29

Critical Section Solutions

Software1. Peterson¶s Solution: for two

processes only.

2. Semaphore: A

flag used toindicate that a routine cannotproceed if a shared resource isalready in use by another routine.The allowable operations on a

semaphore are V("signal") andP("wait"); both are atomicoperations.

 ± Two types: counting and binary (mutexlocks).

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 14/29

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 15/29

Deadlocks

das

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 16/29

Bridge Crossing Example

Traffic only in one direction.

Each section of a bridge can be viewed as a resource.

If a deadlock occurs, it can be resolved if one car backs

up (preempt resources and rollback). Several cars may have to be backed up if a deadlock

occurs.

Starvation is possible.

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 17/29

Deadlocks

Deadlock: processes waiting indefinitely

with no chance of making progress.

Starvation: a process waits for a long timeto make progress.

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 18/29

Deadlocks

Deadlock applications not just OS

 ± Network

Two processes may be blocking a send message

to the other process if they are both waiting for a

message from the other process

» Receive/waiting blocks writing

 ± Databases.

 ± Spooling/streaming data.

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 19/29

Deadlock Characterization

Mutual exclusion: only one process at a time can use aresource.

Hold and wait: a process holding at least one resource iswaiting to acquire additional resources held by other processes.

No preemption: a resource can be released only voluntarilyby the process holding it, after that process has completedits task.

Circular wait: there exists a set {P 0, P 

1

, «, P 0} of waiting

processes such that P 0 is waiting for a resource that is heldby P 1, P 1 is waiting for a resource that is held by

P 2, «, P n ±1 is waiting for a resource that is held byP n, and P 

0is waiting for a resource that is held by P 

0.

Deadlock can arise if four conditions hold simultaneously.

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 20/29

Resource- Allocation Graph

V is partitioned into two types:

 ± P = {P 1

, P 2

, «, P n

}, the set consisting of all the

processes in the system.

 ± R = {R 1, R 2, «, R m}, the set consisting of all

resource types in the system.

request edge ± directed edge P 1p R  j 

assignment edge ± directed edge R  j p P i 

 A set of vertices V and a set of edges E .

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 21/29

Resource- Allocation Graph

(C

ont.) Process

Resource Type with 4 instances

P i requests instance of R  j 

P i  is holding an instance of R  j 

P i 

P i 

R  j 

R  j 

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 22/29

Example of a Resource  Allocation

Graph

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 23/29

Resource  Allocation Graph With  A 

Deadlock

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 24/29

Graph With  A Cycle But No

Deadlock

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 25/29

Basic Facts

If graph contains no cycles no deadlock.

If graph contains a cycle

 ± if only one instance per resource type, thendeadlock.

 ± if several instances per resource type, possibility

of deadlock.

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 26/29

Methods for Handling

Deadlocks Ensure that the system will never enter a

deadlock state.

Allow the system to enter a deadlock state and

then recover.

Ignore the problem and pretend that deadlocksnever occur in the system; used by most

operating systems, including UNIX.

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 27/29

Deadlock Prevention

Mutual Exclusion ± not required for sharable

resources; must hold for nonsharable resources.

Hold and Wait ± must guarantee that whenever a

process requests a resource, it does not hold any

other resources.

 ± Require process to request and be allocated all itsresources before it begins execution, or allow process to

request resources only when the process has none.

 ± Low resource utilization; starvation possible.

Restrain the ways request can be made.

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 28/29

Deadlock Prevention (Cont.)

No Preemption ±

 ± If a process that is holding some resources requests

another resource that cannot be immediately allocated to it,

then all resources currently being held are released.

 ± Preempted resources are added to the list of resources for which the process is waiting.

 ± Process will be restarted only when it can regain its old

resources, as well as the new ones that it is requesting.

Circular Wait ± impose a total ordering of allresource types, and require that each process

requests resources in an increasing order of 

enumeration.

8/3/2019 Process Synchronization and Deadlocks 119961623287018 3

http://slidepdf.com/reader/full/process-synchronization-and-deadlocks-119961623287018-3 29/29

Deadlock  Avoidance

Simplest and most useful model requires that each process

declare the maximum number of resources of each type that

it may need.

The deadlock-avoidance algorithm dynamically examines

the resource-allocation state to ensure that there can never 

be a circular-wait condition.

Resource-allocation state is defined by the number of available and allocated resources, and the maximum

demands of the processes.

Requires that the system has some additional a pr ior i  informationavailable.