itec 502 컴퓨터 시스템 및 실습 chapter 4-2: deadlock mi-jung choi [email protected] dpnm...

24
ITEC 502 컴컴컴 컴컴컴 컴 컴컴 Chapter 4-2: Deadlock Mi-Jung Choi [email protected] DPNM Lab. Dept. of CSE, POSTECH

Upload: lily-haynes

Post on 18-Dec-2015

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC 502 컴퓨터 시스템 및 실습

Chapter 4-2:Deadlock

Mi-Jung [email protected]

DPNM Lab. Dept. of CSE, POSTECH

Page 2: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

2

Contents

1. Deadlock avoidance 2. Deadlock prevention 3. Other issues

Page 3: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

3

Deadlock Avoidance

Simplest and most useful model for DA 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 priori information available

Page 4: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

4

Resource Allocation State: Safe, Unsafe , Deadlock

Page 5: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

5

Safe State When a process requests an available resource,

– system must decide if immediate allocation leaves the system in a safe state

System is in safe state? – if there exists a safe sequence of all processes

Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j<i– If Pi resource needs are not immediately available, then Pi can

wait until all Pj have finished– When Pj is finished, Pi can obtain needed resources, execute,

return allocated resources, and terminate. – When Pi terminates, Pi+1 can obtain its needed resources, and so

on

Page 6: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

6

Basic Facts

If a system is in safe state no deadlocks

If a system is in unsafe state possibility of deadlock

Avoidance ensure that a system will never enter an unsafe state

Page 7: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

7

Deadlock AvoidanceResource Trajectories

Two process resource trajectories

Page 8: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

8

Safe and Unsafe States (1)

Demonstration that the state in (a) is safe

(a) (b) (c) (d) (e)

Page 9: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

9

Safe and Unsafe States (2)

Demonstration that the sate in b is not safe

(a) (b) (c) (d)

Page 10: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

10

Banker’s Algorithm Deadlock avoidance algorithm

– Resource-Allocation Graph is not applicable to a resource allocation system with multiple instance of each resource type

– Supports multiple instances– Named because this algorithm could be used in a bank

Assumption:– Each process must make a priori claim for maximum use– When a process requests a resource it may have to wait– When a process gets all its resources it must return them

in a finite amount of time

Two sub-algorithms– Safety Algorithm– Resource-Request Algorithm

Page 11: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

11

The Banker's Algorithm for a Single Resource

Three resource allocation states– safe– safe– unsafe

(a) (b) (c)

Page 12: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

12

Banker's Algorithm for Multiple Resources

Example of banker's algorithm with multiple resources

Page 13: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

13

Deadlock Prevention (1)

By ensuring that at least one of the four conditions for deadlock cannot hold, we can prevent deadlock

Restrain the ways request can be made– Mutual Exclusion– Hold and Wait– No Preemption– Circular Wait

Page 14: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

14

Deadlock PreventionAttacking the Mutual Exclusion Condition

Mutual Exclusion – not required for sharable resources (read-only file)– must hold for nonsharable resources (printer)

Some devices (such as printer) can be spooled– only the printer daemon uses printer resource– thus deadlock for printer eliminated

Not all devices can be spooled Principle:

– avoid assigning resource when not absolutely necessary– as few processes as possible actually claim the resource

Page 15: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

15

Attacking the Hold and Wait Condition

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 its resources before

it begins execution, or– allow process to request resources only when the process has none

Require processes to request resources before starting– a process never has to wait for what it needs

Problems– may not know required resources at start of run– also ties up resources other processes could be using– Low resource utilization; starvation possible

Variation – process must give up all resources– then request all immediately needed

Page 16: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

16

Attacking the No Preemption Condition

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

This is not a viable option Consider a process given the printer

– halfway through its job– now forcibly take away printer– !!??

Page 17: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

17

Attacking the Circular Wait Condition

Normally ordered resources A resource graph

(a) (b)

Circular Wait– impose a total ordering of all resource types, and – require that each process requests resources in an

increasing order of enumeration

Page 18: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

18

Summary to Deadlock Prevention (1)

Page 19: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

19

Summary to Deadlock Prevention (2)

Deadlock prevention algorithm– Prevent deadlocks by restraining how requests can be

made

The restraints ensures that – at least one of the necessary conditions for deadlock

cannot occur and, hence, that deadlocks cannot hold

Possible side effects deadlock prevention– Low device utilization– Reduced system throughput

Alternative method Deadlock Avoidance

Page 20: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

20

Other Issues: Two-Phase Locking

Phase One– process tries to lock all records it needs, one at a

time– if needed record found locked, start over– (no real work done in phase one)

If phase one succeeds, it starts second phase, – performing updates– releasing locks

Note similarity to requesting all resources at once

Algorithm works where programmer can arrange– program can be stopped, restarted

Page 21: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

21

Non-resource Deadlocks

Possible for two processes to deadlock– each is waiting for the other to do some

task

Can happen with semaphores– each process required to do a down() on

two semaphores (mutex and another)– if done in wrong order, deadlock results

Page 22: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

22

Starvation

Algorithm to allocate a resource – may be to give to shortest job first

Works great for multiple short jobs in a system

May cause long job to be postponed indefinitely– even though not blocked

Solution:– First-come, first-serve policy

Page 23: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

23

Summary

A deadlock requires that four conditions hold simultaneously – Mutual exclusion, hold and wait, no preemption, circular

wait Four principle methods for dealing with deadlocks

– Ignore the deadlock problem– Deadlock detection-recovery scheme– Deadlock avoidance– Deadlock prevention

Deadlock avoidance requires a priori information such as the maximum number of each resource classes– Bankers algorithm

Deadlock prevention ensures that at least one of the necessary conditions never holds

Page 24: ITEC 502 컴퓨터 시스템 및 실습 Chapter 4-2: Deadlock Mi-Jung Choi mjchoi@postech.ac.kr DPNM Lab. Dept. of CSE, POSTECH

ITEC502 컴퓨터 시스템 및 실습

24

Review

1. Deadlock avoidance 2. Deadlock prevention 3. Other issues