380 likes | 420 Views
Operating Systems. Operating Systems. Unit 4: Dining Philosophers Deadlock Indefinite postponement. Dining Philosophers. Dining Philosophers Example. monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5]; void pickup(int i) // following slides
E N D
Operating Systems Operating Systems Unit 4: Dining Philosophers Deadlock Indefinite postponement
Dining Philosophers COP 5994 - Operating Systems
Dining Philosophers Example monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5]; void pickup(int i) // following slides void putdown(int i) // following slides void test(int i) // following slides void init() { for (int i = 0; i < 5; i++) state[i] = thinking; } } COP 5994 - Operating Systems
Dining Philosophers void pickup(int i) { state[i] = hungry; test(i); if (state[i] != eating) self[i].wait(); } COP 5994 - Operating Systems
Dining Philosophers void putdown(int i) { state[i] = thinking; // test left and right neighbors test((i+4) % 5); test((i+1) % 5); } COP 5994 - Operating Systems
Dining Philosophers void test(int i) { if ( (state[(i + 4) % 5] != eating) && (state[i] == hungry) && (state[(i + 1) % 5] != eating)) { state[i] = eating; self[i].signal(); } } COP 5994 - Operating Systems
Java Monitors • enables thread mutual exclusion and synchronization • Signal-and-continue monitors • Allow a thread to signal that the monitor will soon become available • Maintain a lock on monitor until thread exits monitor • method keyword synchronized COP 5994 - Operating Systems
Java Monitors • wait method • releases lock on monitor, thread is placed in wait set • condition variable is “this” object • when thread reenters monitor, reason for waiting may not be met • notify and notifyAll • signal waiting thread(s) Homework COP 5994 - Operating Systems
Dining Philosopher with Semaphore ? semaphore chopstick[5]; // Initially set to 1 Philosopher i: do { wait(chopstick[i]) wait(chopstick[(i+1) % 5]) … eat … signal(chopstick[i]); signal(chopstick[(i+1) % 5]); … think … } while (1); Deadlock ? COP 5994 - Operating Systems
Deadlock COP 5994 - Operating Systems
Simple Resource Deadlock COP 5994 - Operating Systems
Related: Indefinite postponement • Also called indefinite blocking or starvation • Occurs due to biases in a system’s resource scheduling policies • Aging • Technique that prevents indefinite postponement by increasing process’s priority as it waits for resource COP 5994 - Operating Systems
Concepts: sharing & preemption • some resources may be shared • files, devices, code, … • preemptible resources • e.g. processors and main memory • can be removed from a process without loss of work • non-preemptible resources • e.g. tape drives and optical scanners • cannot be removed from the process without loss of work COP 5994 - Operating Systems
Conditions for Deadlock • Mutual exclusion • resource accessed by only one process at a time • Hold-and-wait-for • process holds resource(s) while waiting for other resource(s) • No-preemption • system cannot remove resource from the process until the process has finished using the resource • Circular-wait • processes are in a “circular chain” where each process is waiting for resource(s) of next process in chain COP 5994 - Operating Systems
Dealing with Deadlock • Deadlock prevention • Deadlock avoidance • Deadlock detection • Deadlock recovery COP 5994 - Operating Systems
Deadlock Prevention • Condition a system to remove any possibility of deadlocks occurring • avoid the four deadlock conditions • mutual exclusion cannot be avoided COP 5994 - Operating Systems
Denying the “Wait-For” Condition • process must request all resources at once • inefficient resource allocation COP 5994 - Operating Systems
Denying the “No-Preemption” Condition • preempt resource from process • loss of work • substantial overhead for process restart COP 5994 - Operating Systems
Denying the “Circular-Wait” Condition • Use a linear ordering of resources • each resource gets unique number • process requests resources in ascending order • Drawbacks • Requires the programmer to determine the ordering or resources for each system COP 5994 - Operating Systems
Dijkstra’s Banker’s Algorithm • deadlock avoidance • less stringent than deadlock prevention • control resource allocation • yields better resource utilization • idea: • grant resource to process in return for promise to release it in “finite time” • differentiate between safe and unsafe states • allocate resources to processes only if it results in safe state COP 5994 - Operating Systems
Banker’s Algorithm: assumptions • fixed number of identical resources • fixed number of processes • max(Pi) : • maximum number of resources for Process Pi • loan(Pi ) : • current number of resources held by Process Pi • claim(Pi) : • max(Pi) - loan(Pi) COP 5994 - Operating Systems
Example: Safe State • guarantees that all current processes can complete their work within a finite time • P2 may proceed to completion COP 5994 - Operating Systems
Example: Unsafe State • no process can guarantee to complete COP 5994 - Operating Systems
Safe-State-to-Unsafe-State Transition P3 requests an additional resource COP 5994 - Operating Systems
Banker’s Algorithm example safe or unsafe ? unsafe COP 5994 - Operating Systems
Weaknesses in the Banker’s Algorithm • Requires fixed number of resources • Requires fixed population of processes • only requires granting all requests within “finite time” • only requires that clients return resources within “finite time” • Requires processes to state maximum needs in advance COP 5994 - Operating Systems
Deadlock Detection • problem: • has deadlock occurred ? • how to determine if deadlock has occurred • check for circular wait • can incur significant runtime overhead COP 5994 - Operating Systems
Tool: Resource-Allocation Graph COP 5994 - Operating Systems
Tool: Resource-Allocation Graph COP 5994 - Operating Systems
Reduction of Resource-Allocation Graphs • Rules • If a process’s resource requests may be granted, the graph may be reduced by that process • If a graph can be reduced by all its processes, there is no deadlock • If a graph cannot be reduced by all its processes, the irreducible processes constitute the set of deadlocked processes in the graph COP 5994 - Operating Systems
Example: Graph Reduction 1/4 COP 5994 - Operating Systems
Example: Graph Reduction 2/4 COP 5994 - Operating Systems
Example: Graph Reduction 3/4 COP 5994 - Operating Systems
Example: Graph Reduction 4/4 no deadlock ! COP 5994 - Operating Systems
Deadlock Recovery • Process termination • Abort all deadlocked processes • Abort one process at a time until the deadlock cycle is eliminated • considerations: • suspend/resume feature • checkpoint/rollback COP 5994 - Operating Systems
Deadlock Recovery • In which order should we choose to abort? • Priority of the process • How long process has computed, and how much longer to completion • Resources the process has used • Resources process needs to complete • How many processes will need to be terminated • Is process interactive or batch? COP 5994 - Operating Systems
Current Deadlock Strategies • Deadlock is viewed as limited annoyance in personal computer systems • Some systems implement basic prevention methods • Some others ignore the problem, because checking deadlocks would reduce systems’ performance • Deadlock continues to be an important research area COP 5994 - Operating Systems
Agenda for next week: • Chapter 8 • Processor scheduling • Read ahead ! COP 5994 - Operating Systems