27
Operating Systems Operating Systems COMP 4850/CISG 5550 COMP 4850/CISG 5550 Deadlocks Deadlocks Dr. James Money Dr. James Money

Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Embed Size (px)

DESCRIPTION

Deadlocks Example: Printing Example: Printing –Imagine if two processes had access to the printer simultaneously –One line might from process one –The second might be from process two –And repeating… Solution: grant temporary exclusive access to a shared resource Solution: grant temporary exclusive access to a shared resource

Citation preview

Page 1: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Operating SystemsOperating SystemsCOMP 4850/CISG 5550COMP 4850/CISG 5550

DeadlocksDeadlocks

Dr. James MoneyDr. James Money

Page 2: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

DeadlocksDeadlocks• Computer contain many resourcesComputer contain many resources

– Some can be sharedSome can be shared– The rest cannot be sharedThe rest cannot be shared

• Examples of non-shared resources areExamples of non-shared resources are– PrintersPrinters– Tape drivesTape drives– Special memory addressesSpecial memory addresses– File accessFile access

Page 3: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

DeadlocksDeadlocks• Example: PrintingExample: Printing

– Imagine if two processes had access to Imagine if two processes had access to the printer simultaneouslythe printer simultaneously

– One line might from process oneOne line might from process one– The second might be from process twoThe second might be from process two– And repeating…And repeating…

• Solution: grant temporary exclusive Solution: grant temporary exclusive access to a shared resourceaccess to a shared resource

Page 4: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

DeadlocksDeadlocks• Many times, processes need access to 2+ Many times, processes need access to 2+

resources at the same timeresources at the same time• Two processes are scanning a document and Two processes are scanning a document and

writing it to CDwriting it to CD• First process scans, then tries to write to CDFirst process scans, then tries to write to CD• Second process locks the CD first, then tries Second process locks the CD first, then tries

to scanto scan• Both processes block! This is called a Both processes block! This is called a

deadlockdeadlock..

Page 5: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

DeadlocksDeadlocks• Same situation can occur across Same situation can occur across

multiple machines as wellmultiple machines as well• Many times on a network, these Many times on a network, these

same devices are shared and the same devices are shared and the same problem occurssame problem occurs

• It may involve 3,4,5+ users and It may involve 3,4,5+ users and machinesmachines

Page 6: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

ResourcesResources• Deadlocks occur when processes Deadlocks occur when processes

exclusive access to devices, file, etc.exclusive access to devices, file, etc.• To simplify, we refer to any object To simplify, we refer to any object

that can grant exclusive access as a that can grant exclusive access as a resourceresource..

• In a given computer system there are In a given computer system there are 10s-100s resources to be managed.10s-100s resources to be managed.

Page 7: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Preemptable and Preemptable and Nonpreemptable ResourcesNonpreemptable Resources• There are two types of resources:There are two types of resources:

– Preemptable – resource that can be taken Preemptable – resource that can be taken away from a process without any ill away from a process without any ill effectseffects•MemoryMemory•Usually does not cause deadlocksUsually does not cause deadlocks

– Nonpreemtable – resource that cannot be Nonpreemtable – resource that cannot be taken away from a process without taken away from a process without having a computation failhaving a computation fail•CD WriterCD Writer•We focus on this for deadlocksWe focus on this for deadlocks

Page 8: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

ResourcesResources• The sequence to use a resource is:The sequence to use a resource is:

– Request the resourceRequest the resource– Use the resourceUse the resource– Release the resourceRelease the resource

• If the resource is not available when If the resource is not available when requesting, the process is forced to wait, requesting, the process is forced to wait, either:either:– BlockingBlocking– Returns an error code and process tries again laterReturns an error code and process tries again later

Page 9: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Resource AcquisitionResource Acquisition• For some resources, such as a For some resources, such as a

database system, the user process database system, the user process can manage the resourcescan manage the resources

• One way is to use a semaphore, as in One way is to use a semaphore, as in critical regionscritical regions

Page 10: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Resource AcquisitionResource Acquisitiontypedef int semaphore;typedef int semaphore;semaphore resource1;semaphore resource1;

void processA(void){void processA(void){down(&resource1);down(&resource1);use_resource1();use_resource1();up(&resource1);up(&resource1);

}}

Page 11: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Resource Acquisition – 2 Resource Acquisition – 2 ResourcesResourcestypedef int semaphore;typedef int semaphore;semaphore resource1;semaphore resource1;semaphore resource2;semaphore resource2;

void processA(void){void processA(void){down(&resource1);down(&resource1);down(&resource2);down(&resource2);usebothresources();usebothresources();up(&resource1);up(&resource1);up(&resource2);up(&resource2);

}}

Page 12: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Resource AcquisitionResource Acquisition• Only one process involved so farOnly one process involved so far• What happens when we go to two What happens when we go to two

processes?processes?

Page 13: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Resource AcquisitionResource Acquisitiontypedef int semaphore;typedef int semaphore;semaphore resource1;semaphore resource1;semaphore resource2;semaphore resource2;

void processA(void){void processA(void){down(&resource1);down(&resource1);down(&resource2);down(&resource2);usebothresources();usebothresources();up(&resource2);up(&resource2);up(&resource1);up(&resource1);

}}

void processB(void){void processB(void){down(&resource1);down(&resource1);down(&resource2);down(&resource2);usebothresources();usebothresources();up(&resource2);up(&resource2);up(&resource1);up(&resource1);

}}

Page 14: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Resource AcquisitionResource Acquisitiontypedef int semaphore;typedef int semaphore;semaphore resource1;semaphore resource1;semaphore resource2;semaphore resource2;

void processA(void){void processA(void){down(&resource1);down(&resource1);down(&resource2);down(&resource2);usebothresources();usebothresources();up(&resource2);up(&resource2);up(&resource1);up(&resource1);

}}void processB(void){void processB(void){

down(&resource2);down(&resource2);down(&resource1);down(&resource1);usebothresources();usebothresources();up(&resource1);up(&resource1);up(&resource2);up(&resource2);

}}

Page 15: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Introduction to DeadlocksIntroduction to Deadlocks• Deadlocks are formally defined byDeadlocks are formally defined by

– A set of processes is deadlocked if each A set of processes is deadlocked if each process in the set is waiting for an event that process in the set is waiting for an event that only another process in the set can causeonly another process in the set can cause

• Since they are all waiting, none of them Since they are all waiting, none of them will wake upwill wake up

• Assumption of no interrupts and single Assumption of no interrupts and single threadsthreads

Page 16: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Conditions for DeadlockConditions for Deadlock1.1. Mutual Exclusion – each resource is either Mutual Exclusion – each resource is either

currently assigned to one process or is currently assigned to one process or is availableavailable

2.2. Hold and Wait – processes currently holding Hold and Wait – processes currently holding resources can request new resourcesresources can request new resources

3.3. No preemption – Resources previously granted No preemption – Resources previously granted cannot forcibly be taken away from the cannot forcibly be taken away from the process. They must be released by the processprocess. They must be released by the process

4.4. Circular Wait – there must be a circular chain Circular Wait – there must be a circular chain of 2+ processes, each whom is waiting for a of 2+ processes, each whom is waiting for a resource held by the next member of the chainresource held by the next member of the chain

Page 17: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Conditions for DeadlockConditions for Deadlock• All four conditions must exist for a All four conditions must exist for a

deadlock to occurdeadlock to occur• If one is absent, deadlock cannot If one is absent, deadlock cannot

occuroccur• Many of these are related to system Many of these are related to system

policy choicespolicy choices

Page 18: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Deadlock ModelingDeadlock Modeling• Holt showed in 1972 that the four Holt showed in 1972 that the four

conditions could be modeled using conditions could be modeled using direct graphsdirect graphs

• The graphs have two types of nodes:The graphs have two types of nodes:– Processes – shown as circlesProcesses – shown as circles– Resources – shown as squaresResources – shown as squares

Page 19: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Deadlock ModelingDeadlock Modeling• The arc from process to graph and The arc from process to graph and

vice versa determines if the resource vice versa determines if the resource is held or being waited on:is held or being waited on:– An arc from a resource (square) to a An arc from a resource (square) to a

process (circle) indicates the resource is process (circle) indicates the resource is held by that processheld by that process

– An arc from a process to a resource An arc from a process to a resource indicates the process is blocked waiting indicates the process is blocked waiting for the resourcefor the resource

Page 20: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Deadlock ModelingDeadlock Modeling

Page 21: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money
Page 22: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money
Page 23: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Dealing with DeadlocksDealing with Deadlocks• Ignore the problem, maybe it will ignore Ignore the problem, maybe it will ignore

you?you?– Used by UNIX and WindowsUsed by UNIX and Windows

• Detection and RecoveryDetection and Recovery• Dynamic avoidance by careful resource Dynamic avoidance by careful resource

allocationallocation• Prevention by structurally negating one Prevention by structurally negating one

of the four conditions for deadlocksof the four conditions for deadlocks

Page 24: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Ostrich AlgorithmOstrich Algorithm• Here we stick our head in the sand like Here we stick our head in the sand like

an ostrich and pretend there is no an ostrich and pretend there is no problemproblem

• Different reactionsDifferent reactions– Mathematicians say it is unacceptable and Mathematicians say it is unacceptable and

must be prevented all costsmust be prevented all costs– Engineers might ask how often does it really Engineers might ask how often does it really

occur and how serious the situation isoccur and how serious the situation is

Page 25: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Ostrich AlgorithmOstrich Algorithm• Most operating systems suffer from Most operating systems suffer from

this approachthis approach• Example: Unix Process tableExample: Unix Process table

– Fixed length and finiteFixed length and finite– If fork() fails because the table is full, If fork() fails because the table is full,

program waits and tries again (/bin/login program waits and tries again (/bin/login does this, for example)does this, for example)

Page 26: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Ostrich AlgorithmOstrich Algorithm• However, consider this:However, consider this:• Assume 100 process table slotsAssume 100 process table slots• Ten programs run that need to Ten programs run that need to

create 12 sub-processes eachcreate 12 sub-processes each• After each program creates 9 After each program creates 9

processes, the table is fullprocesses, the table is full• No process finishes because it keeps No process finishes because it keeps

trying to fork!trying to fork!

Page 27: Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money

Ostrich AlgorithmOstrich Algorithm• Do we abandon the processes and the Do we abandon the processes and the

fork calls?fork calls?• Similar to this is the maximum number Similar to this is the maximum number

of open files, which is restricted by the of open files, which is restricted by the size of the i-node tablesize of the i-node table

• The solution is not obvious or easy, The solution is not obvious or easy, which is why most operating systems which is why most operating systems choose this approachchoose this approach