Unformatted text preview:

CS162 Operating Systems and Systems Programming Lecture 7 Deadlock CPU Scheduling February 9 2011 Ion Stoica http inst eecs berkeley edu cs162 Read Writer Revisited Writer Reader check into system check into system lock Acquire lock Acquire while AW AR 0 while AW WW 0 WW WR okToWrite wait lock okToRead wait lock WW WR AW lock release AR lock release read write access AccessDbase ReadWrite if we read only What access AccessDbase ReadOnly remove this check out of system lock Acquire line AW check out of system if WW 0 lock Acquire okToWrite signal AR else if WR 0 if AR 0 WW 0 okToRead broadcast okToWrite signal lock Release lock Release 2 9 11 Ion Stoica CS162 UCB Spring 2011 Lec 7 2 Read Writer Revisited Writer Reader check into system check into system lock Acquire lock Acquire while AW AR 0 while AW WW 0 WW WR okToWrite wait lock okToRead wait lock WW WR AW lock release AR lock release read write access AccessDbase ReadWrite read only access What if we AccessDbase ReadOnly check out of system turn signal to lock Acquire AW check out broadcast of system if WW 0 lock Acquire okToWrite signal AR else if WR 0 if AR 0 WW 0 okToRead broadcast okToWrite broadcast lock Release lock Release 2 9 11 Ion Stoica CS162 UCB Spring 2011 Lec 7 3 Read Writer Revisited Writer Reader check into system check into system lock Acquire lock Acquire while AW AR 0 while AW WW 0 WW WR okContinue wait lock okContinue wait lock WW WR AW lock release AR lock release read write access AccessDbase ReadWrite read only access AccessDbase ReadOnly check out of system lock Acquire AW check out of system if WW 0 lock Acquire okToWrite signal AR else if WR 0 if AR 0 WW 0 okContinue broadcast okContinue signal lock Release lock Release What if we turn okToWrite and okToRead into okContinue 2 9 11 Ion Stoica CS162 UCB Spring 2011 Lec 7 4 Read Writer Revisited Writer Reader check into system check into system lock Acquire lock Acquire while AW AR 0 while AW WW 0 WW WR okContinue wait lock okContinue wait lock WW WR AW lock release AR lock release read write access AccessDbase ReadWrite read only access AccessDbase ReadOnly check out of system lock Acquire AW check out of system if WW 0 lock Acquire okToWrite signal AR else if WR 0 if AR 0 WW 0 okContinue broadcast okContinue signal lock Release lock Release R1 arrives W1 R2 arrive while R1 reads 2 9 11 R1 signals R2 Ion Stoica CS162 UCB Spring 2011 Lec 7 5 Read Writer Revisited Writer Reader check into system check into system lock Acquire lock Acquire while AW AR 0 while AW WW 0 WW WR okContinue wait lock okContinue wait lock WW WR AW lock release AR lock release read write access AccessDbase ReadWrite read only access AccessDbase ReadOnly check out of system lock Acquire AW check out of system if WW 0 lock Acquire okToWrite signal AR else if WR 0 if AR 0 WW 0 okContinue broadcast okContinue broadcast lock Release lock Release 2 9 11 Need to change to broadcast Why Ion Stoica CS162 UCB Spring 2011 Lec 7 6 Resources Resources passive entities needed by threads to do their work CPU time disk space memory Two types of resources Preemptable can take it away CPU Embedded security chip Non preemptable must leave it with the thread Disk space printer chunk of virtual address space Critical section Resources may require exclusive access or may be sharable Read only files are typically sharable Printers are not sharable during time of printing One of the major tasks of an operating system is to manage resources 2 9 11 Ion Stoica CS162 UCB Spring 2011 Lec 7 7 Starvation vs Deadlock Starvation vs Deadlock Starvation thread waits indefinitely Example low priority thread waiting for resources constantly in use by high priority threads Deadlock circular waiting for resources Thread A owns Res 1 and is waiting for Res 2 Thread B owns Res 2 and is waiting for Res 1 Owned By Thread A Res 2 Res 1 Wait For Wait For Thread B Owned By Deadlock Starvation but not vice versa 2 9 11 Starvation can end but doesn t have to Deadlock can t end without external intervention Ion Stoica CS162 UCB Spring 2011 Lec 7 8 Conditions for Deadlock Deadlock not always deterministic Example 2 mutexes Thread A Thread B Deadlock A x P B y P A y p B x P Deadlock won t always happen with this code Have to have exactly the right timing wrong timing So you release a piece of software and you tested it and there it is controlling a nuclear power plant x P y P y V x V y P x P x V y V Deadlocks occur with multiple resources Means you can t decompose the problem Can t solve deadlock for each resource independently Example System with 2 disk drives and two threads Each thread needs 2 disk drives to function Each thread gets one disk and waits for another one 2 9 11 Ion Stoica CS162 UCB Spring 2011 Lec 7 9 Bridge Crossing Example Each segment of road can be viewed as a resource Car must own the segment under them Must acquire segment that they are moving into For bridge must acquire both halves Traffic only in one direction at a time Problem occurs when two cars in opposite directions on bridge each acquires one segment and needs next 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 Starvation is possible 2 9 11 East going traffic really fast no one goes west Ion Stoica CS162 UCB Spring 2011 Lec 7 10 Train Example Wormhole Routed Network Circular dependency Deadlock Each train wants to turn right Blocked by other trains Similar problem to multiprocessor networks Fix Imagine grid extends in all four directions Force ordering of channels tracks Protocol Always go east west first then north south Called dimension ordering X then Y 2 9 11 Ion Stoica CS162 UCB Spring 2011 Lec 7 11 Train Example Wormhole Routed Network Circular dependency Deadlock Each train wants to turn right Blocked by other trains Similar problem to multiprocessor networks Fix Imagine grid extends in all four directions Force ordering of channels tracks Protocol Always go east west first then north south Called dimension ordering X then Y A B 2 9 11 Ion Stoica CS162 UCB Spring 2011 Lec 7 12 Dining Philosopher Problem Five chopsticks Five philosopher really cheap restaurant Free for all Philosopher will grab any one they can Need two chopsticks to eat What if all grab at same time Deadlock How to fix deadlock Make one of them give up a chopstick Hah Eventually everyone will get chance to eat How to prevent deadlock Never let philosopher take last chopstick if no hungry


View Full Document

Berkeley COMPSCI 162 - Lecture 7 Deadlock, CPU Scheduling

Documents in this Course
Lecture 1

Lecture 1

12 pages

Nachos

Nachos

41 pages

Security

Security

39 pages

Load more
Loading Unlocking...
Login

Join to view Lecture 7 Deadlock, CPU Scheduling and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7 Deadlock, CPU Scheduling and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?