SlideShare a Scribd company logo
1 of 53
Download to read offline
Chapter 6

         Deadlocks
6.1. Resources
6.2. Introduction to deadlocks
6.3. The ostrich algorithm
6.4. Deadlock detection and recovery
6.5. Deadlock avoidance
6.6. Deadlock prevention
6.7. Other issues

                                       1
Learning Outcomes
• Understand what deadlock is and how it
  can occur when giving mutually exclusive
  access to multiple resources.
• Understand several approaches to
  mitigating the issue of deadlock in
  operating systems.
  – Including deadlock detection and recovery,
    deadlock avoidance, and deadlock
    prevention.
                                                 2
Resources
• Examples of computer resources
  – printers
  – tape drives
  – Tables in a database
• Processes need access to resources in reasonable
  order
• Preemptable resources
  – can be taken away from a process with no ill effects
• Nonpreemptable resources
  – will cause the process to fail if taken away
                                                           3
Resources & Deadlocks

• 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 - Deadlocked
• Deadlocks occur when …
  – processes are granted exclusive access to devices,
    locks, tables, etc..
  – we refer to these entities generally as resources


                                                         4
Resource Access

•    Sequence of events required to use a resource
    1. request the resource
    2. use the resource
    3. release the resource


•       Must wait if request is denied
    –    requesting process may be blocked
    –    may fail with error code


                                                5
Two example resource usage patterns
semaphore res_1, res_2;   semaphore res_1, res_2;
void proc_A() {           void proc_A() {
   down(&res_1);             down(&res_1);
   down(&res_2);             down(&res_2);
   use_both_res();           use_both_res();
   up(&res_2);               up(&res_2);
   up(&res_1);               up(&res_1);
}                         }
void proc_B() {           void proc_B() {
   down(&res_1);             down(&res_2);
   down(&res_2);             down(&res_1);
   use_both_res();           use_both_res();
   up(&res_2);               up(&res_1);
   up(&res_1);               up(&res_2);
}                         }
                                                    6
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
                                                            7
Four Conditions for Deadlock
1.       Mutual exclusion condition
     •     each resource assigned to 1 process or is available
2.       Hold and wait condition
     •     process holding resources can request additional
3.       No preemption condition
     •     previously granted resources cannot forcibly taken
           away
4.       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
                                                                8
Deadlock Modeling
• 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
                                                   9
Deadlock Modeling
    A        B         C




                           10
 How deadlock occurs
Deadlock Modeling




     (o)          (p)         (q)

                                    11
How deadlock can be avoided
Deadlock
Strategies for dealing with Deadlocks
  1.       just ignore the problem altogether
  2.       detection and recovery
  3.       dynamic avoidance
       •     careful resource allocation
  4.       prevention
       •     negating one of the four necessary conditions



                                                         12
Approach 1: The Ostrich Algorithm
• Pretend there is no problem
• Reasonable if
  – deadlocks occur very rarely
  – cost of prevention is high
     • Example of “cost”, only one process runs at a time
• UNIX and Windows takes this approach for
  some of the more complex resource
  relationships to manage
• It’s a trade off between
  – Convenience (engineering approach)
  – Correctness (mathematical approach)
                                                            13
Approach 2: Detection and
            Recovery
• Need a method to determine if a system is
  deadlocked.
• Assuming deadlocked is detected, we
  need a method of recovery to restore
  progress to the system.




                                         14
Approach 2
Detection with One Resource of Each Type




 • Note the resource ownership and requests
 • A cycle can be found within the graph, denoting
   deadlock
                                                 15
What about resources with
        multiple units?
• We need an approach for dealing with
  resources that consist of more than a
  single unit.




                                          16
Detection with Multiple Resources of Each
                  Type




   Data structures needed by deadlock detection
                      algorithm
                                              17
Note the following invariant
Sum of current resource allocation +
 resources available = resources that exist


        n

     ∑ Cij + A j = E j
      i =1                               18
Detection with Multiple Resources of Each
                  Type




  An example for the deadlock detection algorithm
                                               19
Detection Algorithm
1. Look for an unmarked process Pi, for
   which the i-th row of R is less than or
   equal to A
2. If found, add the i-th row of C to A, and
   mark Pi. Go to step 1
3. If no such process exists, terminate.
Remaining processes are deadlocked

                                               20
Example Deadlock Detection



E = (4 2 3 1)    A = ( 2 1 0 0)

     0 0 1 0        2 0 0 1
                           
C =  2 0 0 1   R =  1 0 1 0
     0 1 2 0        2 1 0 0
                           

                                  21
Example Deadlock Detection



E = (4 2 3 1)    A = ( 2 1 0 0)

     0 0 1 0        2 0 0 1
                           
C =  2 0 0 1   R =  1 0 1 0
     0 1 2 0        2 1 0 0
                           

                                  22
Example Deadlock Detection



E = (4 2 3 1)    A = ( 2 2 2 0)

     0 0 1 0        2 0 0 1
                           
C =  2 0 0 1   R =  1 0 1 0
     0 1 2 0        2 1 0 0
                           

                                  23
Example Deadlock Detection



E = (4 2 3 1)    A = ( 2 2 2 0)

     0 0 1 0        2 0 0 1
                           
C =  2 0 0 1   R =  1 0 1 0
     0 1 2 0        2 1 0 0
                           

                                  24
Example Deadlock Detection



E = (4 2 3 1)    A = (4 2 2 1)

     0 0 1 0        2 0 0 1
                           
C =  2 0 0 1   R =  1 0 1 0
     0 1 2 0        2 1 0 0
                           

                                  25
Example Deadlock Detection



E = (4 2 3 1)    A = (4 2 2 1)

     0 0 1 0        2 0 0 1
                           
C =  2 0 0 1   R =  1 0 1 0
     0 1 2 0        2 1 0 0
                           

                                  26
Example Deadlock Detection



E = (4 2 3 1)    A = (4 2 2 1)

     0 0 1 0        2 0 0 1
                           
C =  2 0 0 1   R =  1 0 1 0
     0 1 2 0        2 1 0 0
                           

                                  27
Example Deadlock Detection



E = (4 2 3 1)    A = (4 2 3 1)

     0 0 1 0        2 0 0 1
                           
C =  2 0 0 1   R =  1 0 1 0
     0 1 2 0        2 1 0 0
                           

                                  28
Example Deadlock Detection
• Algorithm terminates with no unmarked
  processes
  – We have no dead lock




                                          29
Example 2: Deadlock Detection
• Suppose, P3 needs a CD-ROM as well as
  2 Tapes and a Plotter

  E = (4 2 3 1)       A = ( 2 1 0 0)

      0 0 1 0            2 0 0 1
                                
 C =  2 0 0 1       R =  1 0 1 0
      0 1 2 0            2 1 0 1
                                

                                       30
Recovery from Deadlock
• Recovery through preemption
  – take a resource from some other process
  – depends on nature of the resource
• Recovery through rollback
  – checkpoint a process periodically
  – use this saved state
  – restart the process if it is found deadlocked


                                                31
Recovery from Deadlock

• Recovery through killing processes
  – crudest but simplest way to break a deadlock
  – kill one of the processes in the deadlock cycle
  – the other processes get its resources
  – choose process that can be rerun from the
    beginning



                                                32
Approach 3
          Deadlock Avoidance
• Instead of detecting deadlock, can we
  simply avoid it?
  – YES, but only if enough information is
    available in advance.
     • Maximum number of each resource required




                                                  33
Deadlock Avoidance
     Resource Trajectories




Two process resource trajectories
                                    34
Safe and Unsafe States
• A state is safe if
  – The system is not deadlocked
  – There exists a scheduling order that results in
    every process running to completion, even if
    they all request their maximum resources
    immediately




                                                35
Safe and Unsafe States
                            Note: We have 10 units
                                of the resource




 (a)       (b)      (c)       (d)        (e)




Demonstration that the state in (a) is safe

                                               36
Safe and Unsafe States
 A requests one extra unit resulting in (b)




 (a)               (b)                  (c)   (d)



Demonstration that the state in b is not safe

                                              37
Safe and Unsafe State
• Unsafe states are not necessarily deadlocked
  – With a lucky sequence, all processes may complete
  – However, we cannot guarantee that they will
    complete (not deadlock)
• Safe states guarantee we will eventually
  complete all processes
• Deadlock avoidance algorithm
  – Only grant requests that result in safe states



                                                     38
Bankers Algorithm
• Modelled on a Banker with Customers
   – The banker has a limited amount of money to loan customers
      • Limited number of resources
   – Each customer can borrow money up to the customer’s credit
     limit
      • Maximum number of resources required
• Basic Idea
   – Keep the bank in a safe state
      • So all customers are happy even if they all request to borrow up to
        their credit limit at the same time.
   – Customers wishing to borrow such that the bank would enter an
     unsafe state must wait until somebody else repays their loan
     such that the the transaction becomes safe.


                                                                        39
The Banker's Algorithm for a Single Resource




  (a)                 (b)                    (c)


        • Three resource allocation states
          – safe
          – safe
          – unsafe
                                                   40
Banker's Algorithm for Multiple Resources




    Example of banker's algorithm with multiple
                    resources
        System should start in safe state!
                                             41
Banker's Algorithm for Multiple Resources




   Example of banker's algorithm with multiple
                    resources
   Should we allow a request by B 1 scanner to
                    succeed??                42
Banker's Algorithm for Multiple Resources




   Example of banker's algorithm with multiple
                      resources
Should we allow a request by B and E for 1 scanner
                    to succeed??             43
Bankers Algorithm is not
    commonly used in practice
• It is difficult (sometime impossible) to know
  in advance
  – the resources a process will require
  – the number of processes in a dynamic system




                                            44
Approach 4: Deadlock Prevention
• Resource allocation rules prevent
  deadlock by prevent one of the four
  conditions required for deadlock from
  occurring
  – Mutual exclusion
  – Hold and wait
  – No preemption
  – Circular Wait

                                          45
Approach 4
       Deadlock Prevention
   Attacking the Mutual Exclusion Condition
• Not feasible in general
  – Some devices/resource are intrinsically not
    shareable.




                                              46
Attacking the Hold and Wait
                    Condition
• Require processes to request resources before starting
   – a process never has to wait for what it needs

• Issues
   – may not know required resources at start of run
       • ⇒ not always possible
   – also ties up resources other processes could be using

• Variations:
   – process must give up all resources if it would block hold a resource
   – then request all immediately needed
   – prone to starvation



                                                                    47
Attacking the No Preemption Condition


• This is not a viable option
• Consider a process given the printer
  – halfway through its job
  – now forcibly take away printer
  – !!??




                                           48
Attacking the Circular Wait Condition




    (a)                      (b)

      • Numerically ordered resources


                                        49
Attacking the Circular Wait
            Condition
• The displayed deadlock
  cannot happen
                                1   2
  – If A requires 1, it must
    acquire it before
    acquiring 2
  – Note: If B has 1, all
    higher numbered
    resources must be free or   A       B
    held by processes who
    doesn’t need 1
• Resources ordering is a
  common technique in
  practice!!!!!                             50
51
Summary of approaches to
     deadlock prevention
Condition            Approach
• Mutual Exclusion   • Not feasible
• Hold and Wait      • Request resources
                       initially
• No Preemption      • Take resources away
• Circular Wait      • Order resources




                                        52
Starvation
• Starvation is where the overall system makes progress, but
  one or more processes never make progress.

   – Example: An algorithm to allocate a resource may be to give to
     shortest job first

   – Works great for multiple short jobs in a system

   – May cause long job to be postponed indefinitely, even though ready
     and not waiting for a resource.

• One solution:
   – First-come, first-serve policy

                                                                      53

More Related Content

What's hot

Operating system Dead lock
Operating system Dead lockOperating system Dead lock
Operating system Dead lockKaram Munir Butt
 
Deadlock detection & prevention
Deadlock detection & preventionDeadlock detection & prevention
Deadlock detection & preventionIkhtiarUddinShaHin
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating systemMidhun Sankar
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating systemSara Ali
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating SystemAUST
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlockwahab13
 
Deadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networkingDeadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networkingTamannaSharma70
 
deadlock detection using Goldman's algorithm by ANIKET CHOUDHURY
deadlock detection using Goldman's algorithm by ANIKET CHOUDHURYdeadlock detection using Goldman's algorithm by ANIKET CHOUDHURY
deadlock detection using Goldman's algorithm by ANIKET CHOUDHURYअनिकेत चौधरी
 

What's hot (20)

Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Deadlock
DeadlockDeadlock
Deadlock
 
OS - Deadlock
OS - DeadlockOS - Deadlock
OS - Deadlock
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 
Deadlock
DeadlockDeadlock
Deadlock
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
Operating system Dead lock
Operating system Dead lockOperating system Dead lock
Operating system Dead lock
 
Deadlock detection & prevention
Deadlock detection & preventionDeadlock detection & prevention
Deadlock detection & prevention
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Dead lock
Dead lockDead lock
Dead lock
 
Ch8
Ch8Ch8
Ch8
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlock
 
Operating system - Deadlock
Operating system - DeadlockOperating system - Deadlock
Operating system - Deadlock
 
Deadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networkingDeadlock avoidance and prevention .. computer networking
Deadlock avoidance and prevention .. computer networking
 
deadlock detection using Goldman's algorithm by ANIKET CHOUDHURY
deadlock detection using Goldman's algorithm by ANIKET CHOUDHURYdeadlock detection using Goldman's algorithm by ANIKET CHOUDHURY
deadlock detection using Goldman's algorithm by ANIKET CHOUDHURY
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
 

Viewers also liked

Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmSouvik Roy
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSKathirvel Ayyaswamy
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlockstech2click
 
Banker customer relationship
Banker customer relationshipBanker customer relationship
Banker customer relationshipShruti Singh
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OSC.U
 
আত্ম সচেতনতা
আত্ম সচেতনতাআত্ম সচেতনতা
আত্ম সচেতনতাAbul Bashar
 
মাধ্যমিক শিক্ষায় সাব ক্লাস্টারভিত্তিক উন্নয়ন চিন্তা
মাধ্যমিক শিক্ষায় সাব ক্লাস্টারভিত্তিক উন্নয়ন চিন্তামাধ্যমিক শিক্ষায় সাব ক্লাস্টারভিত্তিক উন্নয়ন চিন্তা
মাধ্যমিক শিক্ষায় সাব ক্লাস্টারভিত্তিক উন্নয়ন চিন্তাAbul Bashar
 
Presentation for "Provas de Agergação" - 3 licao
Presentation for "Provas de Agergação" - 3 licaoPresentation for "Provas de Agergação" - 3 licao
Presentation for "Provas de Agergação" - 3 licaoAlvaro Barbosa
 
Commemoration of the Great War 1914-1918 by David Langford
Commemoration of the Great War 1914-1918 by David LangfordCommemoration of the Great War 1914-1918 by David Langford
Commemoration of the Great War 1914-1918 by David Langfordonthewight
 
Intro to advertising week 1, class 1 - hmlc
Intro to advertising   week 1, class 1 - hmlcIntro to advertising   week 1, class 1 - hmlc
Intro to advertising week 1, class 1 - hmlcRajdish Singh
 
Chapter11 economics
Chapter11 economicsChapter11 economics
Chapter11 economicsVin Voro
 
ইভটিজিং প্রতিরোধে দক্ষতাভিত্তিক শিক্ষা
ইভটিজিং প্রতিরোধে দক্ষতাভিত্তিক শিক্ষাইভটিজিং প্রতিরোধে দক্ষতাভিত্তিক শিক্ষা
ইভটিজিং প্রতিরোধে দক্ষতাভিত্তিক শিক্ষাAbul Bashar
 
Diseños de google sketchup
Diseños de google sketchupDiseños de google sketchup
Diseños de google sketchupbelloloco698
 
Flexibiliteit als uitgangspunt voor een nieuwe taalmethode: ICT als meerwaarde
Flexibiliteit als uitgangspunt voor een nieuwe taalmethode: ICT als meerwaardeFlexibiliteit als uitgangspunt voor een nieuwe taalmethode: ICT als meerwaarde
Flexibiliteit als uitgangspunt voor een nieuwe taalmethode: ICT als meerwaardewimdboer
 
Tele3113 wk2tue
Tele3113 wk2tueTele3113 wk2tue
Tele3113 wk2tueVin Voro
 
Introducing A Branded Cell Phone To Vietnam
Introducing A Branded Cell Phone To VietnamIntroducing A Branded Cell Phone To Vietnam
Introducing A Branded Cell Phone To Vietnamsedagokoglu
 

Viewers also liked (20)

Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMS
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlocks
 
Banker customer relationship
Banker customer relationshipBanker customer relationship
Banker customer relationship
 
Deadlock ppt
Deadlock ppt Deadlock ppt
Deadlock ppt
 
Ch8 OS
Ch8 OSCh8 OS
Ch8 OS
 
Media Lab Aalto University October 2011
Media Lab Aalto University October 2011Media Lab Aalto University October 2011
Media Lab Aalto University October 2011
 
আত্ম সচেতনতা
আত্ম সচেতনতাআত্ম সচেতনতা
আত্ম সচেতনতা
 
মাধ্যমিক শিক্ষায় সাব ক্লাস্টারভিত্তিক উন্নয়ন চিন্তা
মাধ্যমিক শিক্ষায় সাব ক্লাস্টারভিত্তিক উন্নয়ন চিন্তামাধ্যমিক শিক্ষায় সাব ক্লাস্টারভিত্তিক উন্নয়ন চিন্তা
মাধ্যমিক শিক্ষায় সাব ক্লাস্টারভিত্তিক উন্নয়ন চিন্তা
 
Sand
SandSand
Sand
 
Presentation for "Provas de Agergação" - 3 licao
Presentation for "Provas de Agergação" - 3 licaoPresentation for "Provas de Agergação" - 3 licao
Presentation for "Provas de Agergação" - 3 licao
 
Commemoration of the Great War 1914-1918 by David Langford
Commemoration of the Great War 1914-1918 by David LangfordCommemoration of the Great War 1914-1918 by David Langford
Commemoration of the Great War 1914-1918 by David Langford
 
Intro to advertising week 1, class 1 - hmlc
Intro to advertising   week 1, class 1 - hmlcIntro to advertising   week 1, class 1 - hmlc
Intro to advertising week 1, class 1 - hmlc
 
Chapter11 economics
Chapter11 economicsChapter11 economics
Chapter11 economics
 
CCRMA - 2011
CCRMA - 2011CCRMA - 2011
CCRMA - 2011
 
ইভটিজিং প্রতিরোধে দক্ষতাভিত্তিক শিক্ষা
ইভটিজিং প্রতিরোধে দক্ষতাভিত্তিক শিক্ষাইভটিজিং প্রতিরোধে দক্ষতাভিত্তিক শিক্ষা
ইভটিজিং প্রতিরোধে দক্ষতাভিত্তিক শিক্ষা
 
Diseños de google sketchup
Diseños de google sketchupDiseños de google sketchup
Diseños de google sketchup
 
Flexibiliteit als uitgangspunt voor een nieuwe taalmethode: ICT als meerwaarde
Flexibiliteit als uitgangspunt voor een nieuwe taalmethode: ICT als meerwaardeFlexibiliteit als uitgangspunt voor een nieuwe taalmethode: ICT als meerwaarde
Flexibiliteit als uitgangspunt voor een nieuwe taalmethode: ICT als meerwaarde
 
Tele3113 wk2tue
Tele3113 wk2tueTele3113 wk2tue
Tele3113 wk2tue
 
Introducing A Branded Cell Phone To Vietnam
Introducing A Branded Cell Phone To VietnamIntroducing A Branded Cell Phone To Vietnam
Introducing A Branded Cell Phone To Vietnam
 

Similar to Lect05

Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptamadayshwan
 
Deadlock Detection Algorithm
Deadlock Detection AlgorithmDeadlock Detection Algorithm
Deadlock Detection AlgorithmMohammad Qureshi
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3rohassanie
 
Lecture5
Lecture5Lecture5
Lecture5jntu
 
Chapter 03
Chapter 03Chapter 03
Chapter 03 Google
 
3 (1) [Autosaved].ppt
3 (1) [Autosaved].ppt3 (1) [Autosaved].ppt
3 (1) [Autosaved].pptamadayshwan
 
Deadlocks by wani zahoor
Deadlocks by wani zahoorDeadlocks by wani zahoor
Deadlocks by wani zahoorWani Zahoor
 
OS Presentation 1 (1).pptx
OS Presentation 1 (1).pptxOS Presentation 1 (1).pptx
OS Presentation 1 (1).pptxFahadAbdullah84
 
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptxOS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptxGovindJha93
 
Errors (Concurrency)
Errors (Concurrency)Errors (Concurrency)
Errors (Concurrency)Sri Prasanna
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptxsheraz7288
 
Falcon: Fault Localization in Concurrent Programs
Falcon: Fault Localization in Concurrent ProgramsFalcon: Fault Localization in Concurrent Programs
Falcon: Fault Localization in Concurrent ProgramsSangmin Park
 

Similar to Lect05 (20)

OS-Part-06.pdf
OS-Part-06.pdfOS-Part-06.pdf
OS-Part-06.pdf
 
Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).ppt
 
Section07-Deadlocks.pdf
Section07-Deadlocks.pdfSection07-Deadlocks.pdf
Section07-Deadlocks.pdf
 
Deadlock Detection Algorithm
Deadlock Detection AlgorithmDeadlock Detection Algorithm
Deadlock Detection Algorithm
 
3 (2).ppt
3 (2).ppt3 (2).ppt
3 (2).ppt
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
Lecture5
Lecture5Lecture5
Lecture5
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Deadlocks Part- III.pdf
Deadlocks Part- III.pdfDeadlocks Part- III.pdf
Deadlocks Part- III.pdf
 
ikh311-05
ikh311-05ikh311-05
ikh311-05
 
3 (1) [Autosaved].ppt
3 (1) [Autosaved].ppt3 (1) [Autosaved].ppt
3 (1) [Autosaved].ppt
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlocks by wani zahoor
Deadlocks by wani zahoorDeadlocks by wani zahoor
Deadlocks by wani zahoor
 
OS Presentation 1 (1).pptx
OS Presentation 1 (1).pptxOS Presentation 1 (1).pptx
OS Presentation 1 (1).pptx
 
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptxOS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
 
Errors (Concurrency)
Errors (Concurrency)Errors (Concurrency)
Errors (Concurrency)
 
chapter06-new.pptx
chapter06-new.pptxchapter06-new.pptx
chapter06-new.pptx
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
Falcon: Fault Localization in Concurrent Programs
Falcon: Fault Localization in Concurrent ProgramsFalcon: Fault Localization in Concurrent Programs
Falcon: Fault Localization in Concurrent Programs
 
Deadlocks.ppt
Deadlocks.pptDeadlocks.ppt
Deadlocks.ppt
 

More from Vin Voro

Tele3113 tut6
Tele3113 tut6Tele3113 tut6
Tele3113 tut6Vin Voro
 
Tele3113 tut5
Tele3113 tut5Tele3113 tut5
Tele3113 tut5Vin Voro
 
Tele3113 tut4
Tele3113 tut4Tele3113 tut4
Tele3113 tut4Vin Voro
 
Tele3113 tut1
Tele3113 tut1Tele3113 tut1
Tele3113 tut1Vin Voro
 
Tele3113 tut3
Tele3113 tut3Tele3113 tut3
Tele3113 tut3Vin Voro
 
Tele3113 tut2
Tele3113 tut2Tele3113 tut2
Tele3113 tut2Vin Voro
 
Tele3113 wk11tue
Tele3113 wk11tueTele3113 wk11tue
Tele3113 wk11tueVin Voro
 
Tele3113 wk10wed
Tele3113 wk10wedTele3113 wk10wed
Tele3113 wk10wedVin Voro
 
Tele3113 wk10tue
Tele3113 wk10tueTele3113 wk10tue
Tele3113 wk10tueVin Voro
 
Tele3113 wk11wed
Tele3113 wk11wedTele3113 wk11wed
Tele3113 wk11wedVin Voro
 
Tele3113 wk7wed
Tele3113 wk7wedTele3113 wk7wed
Tele3113 wk7wedVin Voro
 
Tele3113 wk9tue
Tele3113 wk9tueTele3113 wk9tue
Tele3113 wk9tueVin Voro
 
Tele3113 wk8wed
Tele3113 wk8wedTele3113 wk8wed
Tele3113 wk8wedVin Voro
 
Tele3113 wk9wed
Tele3113 wk9wedTele3113 wk9wed
Tele3113 wk9wedVin Voro
 
Tele3113 wk7wed
Tele3113 wk7wedTele3113 wk7wed
Tele3113 wk7wedVin Voro
 
Tele3113 wk7wed
Tele3113 wk7wedTele3113 wk7wed
Tele3113 wk7wedVin Voro
 
Tele3113 wk7tue
Tele3113 wk7tueTele3113 wk7tue
Tele3113 wk7tueVin Voro
 
Tele3113 wk6wed
Tele3113 wk6wedTele3113 wk6wed
Tele3113 wk6wedVin Voro
 
Tele3113 wk6tue
Tele3113 wk6tueTele3113 wk6tue
Tele3113 wk6tueVin Voro
 
Tele3113 wk5tue
Tele3113 wk5tueTele3113 wk5tue
Tele3113 wk5tueVin Voro
 

More from Vin Voro (20)

Tele3113 tut6
Tele3113 tut6Tele3113 tut6
Tele3113 tut6
 
Tele3113 tut5
Tele3113 tut5Tele3113 tut5
Tele3113 tut5
 
Tele3113 tut4
Tele3113 tut4Tele3113 tut4
Tele3113 tut4
 
Tele3113 tut1
Tele3113 tut1Tele3113 tut1
Tele3113 tut1
 
Tele3113 tut3
Tele3113 tut3Tele3113 tut3
Tele3113 tut3
 
Tele3113 tut2
Tele3113 tut2Tele3113 tut2
Tele3113 tut2
 
Tele3113 wk11tue
Tele3113 wk11tueTele3113 wk11tue
Tele3113 wk11tue
 
Tele3113 wk10wed
Tele3113 wk10wedTele3113 wk10wed
Tele3113 wk10wed
 
Tele3113 wk10tue
Tele3113 wk10tueTele3113 wk10tue
Tele3113 wk10tue
 
Tele3113 wk11wed
Tele3113 wk11wedTele3113 wk11wed
Tele3113 wk11wed
 
Tele3113 wk7wed
Tele3113 wk7wedTele3113 wk7wed
Tele3113 wk7wed
 
Tele3113 wk9tue
Tele3113 wk9tueTele3113 wk9tue
Tele3113 wk9tue
 
Tele3113 wk8wed
Tele3113 wk8wedTele3113 wk8wed
Tele3113 wk8wed
 
Tele3113 wk9wed
Tele3113 wk9wedTele3113 wk9wed
Tele3113 wk9wed
 
Tele3113 wk7wed
Tele3113 wk7wedTele3113 wk7wed
Tele3113 wk7wed
 
Tele3113 wk7wed
Tele3113 wk7wedTele3113 wk7wed
Tele3113 wk7wed
 
Tele3113 wk7tue
Tele3113 wk7tueTele3113 wk7tue
Tele3113 wk7tue
 
Tele3113 wk6wed
Tele3113 wk6wedTele3113 wk6wed
Tele3113 wk6wed
 
Tele3113 wk6tue
Tele3113 wk6tueTele3113 wk6tue
Tele3113 wk6tue
 
Tele3113 wk5tue
Tele3113 wk5tueTele3113 wk5tue
Tele3113 wk5tue
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

Lect05

  • 1. Chapter 6 Deadlocks 6.1. Resources 6.2. Introduction to deadlocks 6.3. The ostrich algorithm 6.4. Deadlock detection and recovery 6.5. Deadlock avoidance 6.6. Deadlock prevention 6.7. Other issues 1
  • 2. Learning Outcomes • Understand what deadlock is and how it can occur when giving mutually exclusive access to multiple resources. • Understand several approaches to mitigating the issue of deadlock in operating systems. – Including deadlock detection and recovery, deadlock avoidance, and deadlock prevention. 2
  • 3. Resources • Examples of computer resources – printers – tape drives – Tables in a database • Processes need access to resources in reasonable order • Preemptable resources – can be taken away from a process with no ill effects • Nonpreemptable resources – will cause the process to fail if taken away 3
  • 4. Resources & Deadlocks • 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 - Deadlocked • Deadlocks occur when … – processes are granted exclusive access to devices, locks, tables, etc.. – we refer to these entities generally as resources 4
  • 5. Resource Access • Sequence of events required to use a resource 1. request the resource 2. use the resource 3. release the resource • Must wait if request is denied – requesting process may be blocked – may fail with error code 5
  • 6. Two example resource usage patterns semaphore res_1, res_2; semaphore res_1, res_2; void proc_A() { void proc_A() { down(&res_1); down(&res_1); down(&res_2); down(&res_2); use_both_res(); use_both_res(); up(&res_2); up(&res_2); up(&res_1); up(&res_1); } } void proc_B() { void proc_B() { down(&res_1); down(&res_2); down(&res_2); down(&res_1); use_both_res(); use_both_res(); up(&res_2); up(&res_1); up(&res_1); up(&res_2); } } 6
  • 7. 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 7
  • 8. Four Conditions for Deadlock 1. Mutual exclusion condition • each resource assigned to 1 process or is available 2. Hold and wait condition • process holding resources can request additional 3. No preemption condition • previously granted resources cannot forcibly taken away 4. 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 8
  • 9. Deadlock Modeling • 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 9
  • 10. Deadlock Modeling A B C 10 How deadlock occurs
  • 11. Deadlock Modeling (o) (p) (q) 11 How deadlock can be avoided
  • 12. Deadlock Strategies for dealing with Deadlocks 1. just ignore the problem altogether 2. detection and recovery 3. dynamic avoidance • careful resource allocation 4. prevention • negating one of the four necessary conditions 12
  • 13. Approach 1: The Ostrich Algorithm • Pretend there is no problem • Reasonable if – deadlocks occur very rarely – cost of prevention is high • Example of “cost”, only one process runs at a time • UNIX and Windows takes this approach for some of the more complex resource relationships to manage • It’s a trade off between – Convenience (engineering approach) – Correctness (mathematical approach) 13
  • 14. Approach 2: Detection and Recovery • Need a method to determine if a system is deadlocked. • Assuming deadlocked is detected, we need a method of recovery to restore progress to the system. 14
  • 15. Approach 2 Detection with One Resource of Each Type • Note the resource ownership and requests • A cycle can be found within the graph, denoting deadlock 15
  • 16. What about resources with multiple units? • We need an approach for dealing with resources that consist of more than a single unit. 16
  • 17. Detection with Multiple Resources of Each Type Data structures needed by deadlock detection algorithm 17
  • 18. Note the following invariant Sum of current resource allocation + resources available = resources that exist n ∑ Cij + A j = E j i =1 18
  • 19. Detection with Multiple Resources of Each Type An example for the deadlock detection algorithm 19
  • 20. Detection Algorithm 1. Look for an unmarked process Pi, for which the i-th row of R is less than or equal to A 2. If found, add the i-th row of C to A, and mark Pi. Go to step 1 3. If no such process exists, terminate. Remaining processes are deadlocked 20
  • 21. Example Deadlock Detection E = (4 2 3 1) A = ( 2 1 0 0)  0 0 1 0  2 0 0 1     C =  2 0 0 1 R =  1 0 1 0  0 1 2 0  2 1 0 0     21
  • 22. Example Deadlock Detection E = (4 2 3 1) A = ( 2 1 0 0)  0 0 1 0  2 0 0 1     C =  2 0 0 1 R =  1 0 1 0  0 1 2 0  2 1 0 0     22
  • 23. Example Deadlock Detection E = (4 2 3 1) A = ( 2 2 2 0)  0 0 1 0  2 0 0 1     C =  2 0 0 1 R =  1 0 1 0  0 1 2 0  2 1 0 0     23
  • 24. Example Deadlock Detection E = (4 2 3 1) A = ( 2 2 2 0)  0 0 1 0  2 0 0 1     C =  2 0 0 1 R =  1 0 1 0  0 1 2 0  2 1 0 0     24
  • 25. Example Deadlock Detection E = (4 2 3 1) A = (4 2 2 1)  0 0 1 0  2 0 0 1     C =  2 0 0 1 R =  1 0 1 0  0 1 2 0  2 1 0 0     25
  • 26. Example Deadlock Detection E = (4 2 3 1) A = (4 2 2 1)  0 0 1 0  2 0 0 1     C =  2 0 0 1 R =  1 0 1 0  0 1 2 0  2 1 0 0     26
  • 27. Example Deadlock Detection E = (4 2 3 1) A = (4 2 2 1)  0 0 1 0  2 0 0 1     C =  2 0 0 1 R =  1 0 1 0  0 1 2 0  2 1 0 0     27
  • 28. Example Deadlock Detection E = (4 2 3 1) A = (4 2 3 1)  0 0 1 0  2 0 0 1     C =  2 0 0 1 R =  1 0 1 0  0 1 2 0  2 1 0 0     28
  • 29. Example Deadlock Detection • Algorithm terminates with no unmarked processes – We have no dead lock 29
  • 30. Example 2: Deadlock Detection • Suppose, P3 needs a CD-ROM as well as 2 Tapes and a Plotter E = (4 2 3 1) A = ( 2 1 0 0)  0 0 1 0  2 0 0 1     C =  2 0 0 1 R =  1 0 1 0  0 1 2 0  2 1 0 1     30
  • 31. Recovery from Deadlock • Recovery through preemption – take a resource from some other process – depends on nature of the resource • Recovery through rollback – checkpoint a process periodically – use this saved state – restart the process if it is found deadlocked 31
  • 32. Recovery from Deadlock • Recovery through killing processes – crudest but simplest way to break a deadlock – kill one of the processes in the deadlock cycle – the other processes get its resources – choose process that can be rerun from the beginning 32
  • 33. Approach 3 Deadlock Avoidance • Instead of detecting deadlock, can we simply avoid it? – YES, but only if enough information is available in advance. • Maximum number of each resource required 33
  • 34. Deadlock Avoidance Resource Trajectories Two process resource trajectories 34
  • 35. Safe and Unsafe States • A state is safe if – The system is not deadlocked – There exists a scheduling order that results in every process running to completion, even if they all request their maximum resources immediately 35
  • 36. Safe and Unsafe States Note: We have 10 units of the resource (a) (b) (c) (d) (e) Demonstration that the state in (a) is safe 36
  • 37. Safe and Unsafe States A requests one extra unit resulting in (b) (a) (b) (c) (d) Demonstration that the state in b is not safe 37
  • 38. Safe and Unsafe State • Unsafe states are not necessarily deadlocked – With a lucky sequence, all processes may complete – However, we cannot guarantee that they will complete (not deadlock) • Safe states guarantee we will eventually complete all processes • Deadlock avoidance algorithm – Only grant requests that result in safe states 38
  • 39. Bankers Algorithm • Modelled on a Banker with Customers – The banker has a limited amount of money to loan customers • Limited number of resources – Each customer can borrow money up to the customer’s credit limit • Maximum number of resources required • Basic Idea – Keep the bank in a safe state • So all customers are happy even if they all request to borrow up to their credit limit at the same time. – Customers wishing to borrow such that the bank would enter an unsafe state must wait until somebody else repays their loan such that the the transaction becomes safe. 39
  • 40. The Banker's Algorithm for a Single Resource (a) (b) (c) • Three resource allocation states – safe – safe – unsafe 40
  • 41. Banker's Algorithm for Multiple Resources Example of banker's algorithm with multiple resources System should start in safe state! 41
  • 42. Banker's Algorithm for Multiple Resources Example of banker's algorithm with multiple resources Should we allow a request by B 1 scanner to succeed?? 42
  • 43. Banker's Algorithm for Multiple Resources Example of banker's algorithm with multiple resources Should we allow a request by B and E for 1 scanner to succeed?? 43
  • 44. Bankers Algorithm is not commonly used in practice • It is difficult (sometime impossible) to know in advance – the resources a process will require – the number of processes in a dynamic system 44
  • 45. Approach 4: Deadlock Prevention • Resource allocation rules prevent deadlock by prevent one of the four conditions required for deadlock from occurring – Mutual exclusion – Hold and wait – No preemption – Circular Wait 45
  • 46. Approach 4 Deadlock Prevention Attacking the Mutual Exclusion Condition • Not feasible in general – Some devices/resource are intrinsically not shareable. 46
  • 47. Attacking the Hold and Wait Condition • Require processes to request resources before starting – a process never has to wait for what it needs • Issues – may not know required resources at start of run • ⇒ not always possible – also ties up resources other processes could be using • Variations: – process must give up all resources if it would block hold a resource – then request all immediately needed – prone to starvation 47
  • 48. Attacking the No Preemption Condition • This is not a viable option • Consider a process given the printer – halfway through its job – now forcibly take away printer – !!?? 48
  • 49. Attacking the Circular Wait Condition (a) (b) • Numerically ordered resources 49
  • 50. Attacking the Circular Wait Condition • The displayed deadlock cannot happen 1 2 – If A requires 1, it must acquire it before acquiring 2 – Note: If B has 1, all higher numbered resources must be free or A B held by processes who doesn’t need 1 • Resources ordering is a common technique in practice!!!!! 50
  • 51. 51
  • 52. Summary of approaches to deadlock prevention Condition Approach • Mutual Exclusion • Not feasible • Hold and Wait • Request resources initially • No Preemption • Take resources away • Circular Wait • Order resources 52
  • 53. Starvation • Starvation is where the overall system makes progress, but one or more processes never make progress. – Example: An algorithm to allocate a resource may be to give to shortest job first – Works great for multiple short jobs in a system – May cause long job to be postponed indefinitely, even though ready and not waiting for a resource. • One solution: – First-come, first-serve policy 53