Review: Readers-Writers Problem Multiple threads reading/writing a database Many threads can read simultaneously Only one can be writing at any time When a writer is executing, nobody else can read or write
Review: Readers-Writers While (1) { generate_data(); down(database); write(); up(database); } READER: While (1) { down(protector); rc++; if (rc == 1) //first reader down(database); up(protector); read(); rc--; If (rc == 0) then // last one up(database); …. } Two semaphores: database protector Can we replace database with a mutex? Initial: protector=1, database =1 rc =0
Lecture 17: Deadlock: Conditions, Detection and Avoidance
Review Two Examples (Deadlock) Multiple Mutex Locks Dining Philosopher Problem
Review: Taking Multiple Locks proc1( ) { pthread_mutex_lock(&m1); /* use object 1 */ pthread_mutex_lock(&m2); /* use objects 1 and 2 */ pthread_mutex_unlock(&m2); pthread_mutex_unlock(&m1); } proc2( ) { pthread_mutex_lock(&m2); /* use object 2 */ pthread_mutex_lock(&m1); /* use objects 1 and 2 */ pthread_mutex_unlock(&m1); pthread_mutex_unlock(&m2); } In this example our threads are using two mutexes to control access to two different objects. Thread 1, executing proc1, first takes mutex 1, then, while still holding mutex 1, obtains mutex 2. Thread 2, executing proc2, first takes mutex 2, then, while still holding mutex 2, obtains mutex 1. However, things do not always work out as planned. If thread 1 obtains mutex 1 and, at about the same time, thread 2 obtains mutex 2, then if thread 1 attempts to take mutex 2 and thread 2 attempts to take mutex 1, we have a deadlock. thread a thread b
DEADLOCK
Roadmap for this week Deadlocks Strategies of dealing with deadlocks
In this lecture Introduction to deadlocks
Resources Examples of computer resources printers tape drives tables Processes need access to resources in reasonable order Suppose a process holds resource A and requests resource B at same time another process holds B and requests A both are blocked and remain so
Resources (1) Deadlocks occur when … Preemptable resources processes are granted exclusive access to devices we refer to these devices generally as resources Preemptable resources can be taken away from a process with no ill effects Nonpreemptable resources will cause the process to fail if taken away
Resources (2) Sequence of events required to use a resource request the resource use the resource release the resource Must wait if request is denied requesting process may be blocked may fail with error code
Introduction to Deadlocks Formal definition : A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause Usually the event is release of a currently held resource None of the processes can … run release resources be awakened
Four Conditions for Deadlock Mutual exclusion condition each resource assigned to 1 process or is available Hold and wait condition process holding resources can request additional No preemption condition previously granted resources cannot forcibly taken away Circular wait condition must be a circular chain of 2 or more processes each is waiting for resource held by next member of the chain
Deadlock Modeling (1) Modeled with directed graphs resource R assigned to process A process B is requesting/waiting for resource S process C and D are in deadlock over resources T and U
Deadlock Modeling (2) A B C How deadlock occurs