1 / 14

Deadlock

Deadlock. CS 537 - Introduction to Operating Systems. Defining Deadlock. Deadlock is a situation where 2 or more processes are unable to proceed because they are waiting for shared resources. Three necessary conditions for deadlock able to hold more than one resource at a time

badu
Download Presentation

Deadlock

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Deadlock CS 537 - Introduction to Operating Systems

  2. Defining Deadlock • Deadlock is a situation where 2 or more processes are unable to proceed because they are waiting for shared resources. • Three necessary conditions for deadlock • able to hold more than one resource at a time • unwilling to give up resources • cycle • Break any one of these three conditions and deadlock is avoided

  3. Example • Imagine 4 cars at an intersection 1 0 2 3

  4. Example • Lanes are resources. • Deadlock has occurred because • each car is holding 2 resources (lanes) • none of the cars is willing to backup • car 0 waits for car 1 which waits for car 2 which waits for car 3 which waits for car 0 • this is a cycle • If any ONE of the above conditions can be broken, deadlock would be broken

  5. Dealing with Deadlock • Three ways to deal with deadlock • never allow it to occur • allow it to occur, detect it, and break it • ignore it • this is the most common solution • requires programmers to write programs that don’t allow deadlock to occur

  6. Not Allowing Deadlock to Occur • Don’t allow cycles to happen • Force requests in specific order • for example, must requests resources in ascending order • Process A may have to wait for B, but B will never have to wait for A • Must know in advance what resources are going to be used • or be willing and able to give up higher numbered resources to get a lower one

  7. Detecting Deadlock • Basic idea • examine the system for cycles • find any job that can satisfy all of its requests • assume it finishes and gives its resources back to the system • repeat the process until • all processes can be shown to finish - no deadlock • two or more processes can’t finish – deadlocked

  8. Detecting Deadlock • Very expensive to check for deadlock • system has to stop all useful work to run an algorithm • There are several deadlock detection algorithms • not used very often • we won’t cover them

  9. Deadlock Recovery • So what to do if deadlock is discovered? • OS can start deactivating processes • OS can revoke resources from processes • Both of the above solutions will eventually end a deadlock • which processes to deactivate? • which resources to revoke?

  10. Dining Philosophers • Philosophers sitting around a dining table • Philosophers only eat and think • Need two forks to eat • Exactly as many forks as philosophers • Before eating, a philosopher must pick up the fork to his right and left • When done eating, each philosopher sets down both forks and goes back to thinking

  11. P 0 P 1 F 1 F 0 F 2 P 5 P 2 F 5 F 3 F 4 P 4 P 3 Dining Philosophers

  12. Dining Philosophers • Only one philosopher can hold a fork at a time • One major problem • what if all philosophers decide to eat at once? • if they all pick up the right fork first, none of them can get the second fork to eat • deadlock

  13. Philosopher Deadlock Solutions • Make every even numbered philosopher pick up the right fork first and every odd numbered philosopher pick up the left fork first • Don’t let them all eat at once • a philosopher has to enter a monitor to check if it is safe to eat • can only get into the monitor if no one else in it • each philosopher checks and sets some state indicating their condition

  14. Philosopher Deadlock Solution enum { THINKING, HUNGRY, EATING }; monitor diningPhilosopher { int state[5]; condition self[5]; diningPhilosphers { for(int i=0; i<5; i++) state[i] = THINKING; } pickup(int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self[i].wait; } putDown(int i) { state[i] = THINKING; test((i+5) % 6); test((i+1) % 6); } test(int i) { if( (state[(i + 5) % 6] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 6] != EATING) ) { state[i] = EATING; self[i].signal; } } }

More Related