Docsity

Deadlock: Understanding and Prevention in Computer Science - Prof. Samuel T. King, Study notes of Operating Systems

Partial preview of the text

Download Deadlock: Understanding and Prevention in Computer Science - Prof. Samuel T. King and more Study notes Operating Systems in PDF only on Docsity!

CS 423: Deadlock

Sam King / Klara Nahrstedt

Deadlock

  • Resources
    • Something needed bya thread
    • A thread waits for resources
    • E.g., locks, disk space, memory, CPU
  • Deadlock
    • A circular waiting for resources leading to the threads involved not being able to make progress

Deadlock

  • Example Thread A Thread B Lock(x) lock(y) Lock(y) lock(x) … … Unlock(y) unlock(x) Unlock(x) unlock(y)
  • Can deadlock occur with our code?
  • Will deadlock always occur?

Deadlock

  • General structure of thread code
    • Phase 1 while(not done) { acquire resources work }
    • Phase 2: release resources
  • Assume phase 1 has finite amount of work

Handling deadlock

  • Ignore (“Ostrich algorithm”)
  • Detect and fix
  • Prevent
  • Detect and fix
    • Can detect by looking for cycles in the wait- for graph
    • How to fix once detected?

Deadlock prevention

  • Idea: eliminate one of the four

necessary conditions

  • Increase resources
    • E.g., large classrooms
  • Eliminate hold and wait

Eliminate hold and wait

  • Wait until all resources are free, grab all

at once

  • Lock around picking up of forks
  • Problem?
  • If you find busy resource, release all
  • Problems?

Allow preemption

  • Works well for CPU
    • Memory also
  • Does this work for locks?

Banker’s algorithm

  • Similar to reserving all resources at

beginning, more efficient

  • State max resources in advance
    • When thread tries to acquire, banker’s algorithms determines when it’s safe
    • Blocks when unsafe

Banker’s algorithm

  • General structure of thread code Phase 1a. State max resources Phase 1b. while(not done) { acquire resources work } Phase 2. Release all resources
  • Preventing deadlock by requesting all resources at beginning would block thread in phase 1

Banker’s algorithm

  • In Banker’s algorithm, phase 1a

provides the information needed to

determine when it’s safe to satisfy each

resource

  • “Safe” means guaranteeing the ability

for all threads to finish

  • E.g., no deadlock

Banker’s algorithm

  • Example: use banker’s algorithm to

model a bank loaning money

  • Bank has $6000. Customers sign up

with bank and establish a credit limit,

and borrow in stages.

  • When done, return all money (repay loan)

Solution #2: banker’s algorithm

  • Bank approves credit limits, may have to wait when asking for money Anne takes out $1000 ($5000 left) Bob takes out $2000 ($3000 left) Cory asks for $

Solution

  • Allow iff, after giving money, there

exists some sequential order of fulfilling

all maximum resources

  • Will previous scenario work?

Banker’s algorithm

  • Problems
    • Difficult to anticipate maximum resources
    • Locks != money in bank
      • What is the difference in this context?