SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
5 Concurrent Errors
5.1 Race Condition
 • A race condition occurs when there is missing:
   – synchronization
   – mutual exclusion
 • Two or more tasks race along assuming synchronization or mutual
   exclusion has occurred.
 • Can be very difficult to locate (thought experiments).

5.2 No Progress
5.2.1 Live-lock
 • indefinite postponement: the “You go first” problem on simultaneous
   arrival
 • Caused by poor scheduling:
   Except as otherwise noted, the content of this presentation is licensed under the Creative Com-
mons Attribution 2.5 License.


                                              148
149




• There always exists some scheduling algorithm that can deal effectively
  with live-lock.

5.2.2 Starvation
 • A selection algorithm ignores one or more tasks so they are never
   executed.
 • I.e., lack of long-term fairness.
150
• Long-term (infinite) starvation is extremely rare, but short-term
  starvation can occur and is a problem.

5.2.3 Deadlock
 • Deadlock is the state when one or more processes are waiting for an
   event that will not occur.
5.2.3.1 Synchronization Deadlock
 • Failure in cooperation, so a blocked task is never unblocked (stuck
   waiting):
     void uMain::main() {
         uSemaphore s(0);       // closed
         s.P();                 // wait for lock to open
     }

5.2.3.2 Mutual Exclusion Deadlock
 • Failure to acquire a resource protected by mutual exclusion.
151




  Deadlock, unless one of the cars is willing to backup.
• Simple example using semaphores:
    uSemaphore L1(1), L2(1); // open
         task1                 task2
    L1.P()                 L2.P()         // acquire opposite locks
        R1                     R2         // access resource
        L2.P()                 L1.P()     // acquire opposite locks
            R1 & R2                R2 & R1 // access resource
152
• There are 5 conditions that must occur for a set of processes to get into
  Deadlock.
  1. There exists a shared resource requiring mutual exclusion.
  2. A process holds a resource while waiting for access to a resource held
     by another process (hold and wait).
  3. Once a process has gained access to a resource, the O/S cannot get it
     back (no preemption).
  4. There exists a circular wait of processes on resources.
  5. These conditions must occur simultaneously.

5.3 Deadlock Prevention
• Eliminate one or more of the conditions required for a deadlock from an
  algorithm ⇒ deadlock can never occur.

5.3.1 Synchronization Prevention
 • Eliminate all synchronization from a program
 • ⇒ no communication
153
• all tasks must be completely independent, generating results through
  side-effects.

5.3.2 Mutual Exclusion Prevention
 • Deadlock can be prevented by eliminating one of the 5 conditions:
1. no mutual exclusion: impossible in many cases
2. no hold & wait: do not give any resource, unless all resources can be
   given
   • ⇒ poor resource utilization
   • possible starvation
3. allow preemption:
   • Preemption is dynamic ⇒ cannot apply statically.
4. no circular wait:
   • Control the order of resource allocations to prevent circular wait:
154
    uSemaphore L1(1), L2(1); // open
        task1                  task2
    L1.P()                  L1.P()       //   acquire same locks
        R1                      R1       //   access resource
        L2.P()                  L2.P()   //   acquire same locks
            R2                      R2   //   access resource
• Use an ordered resource policy:
                R1     <       R2        <       R3     ···


                       T1                T2
 – divide all resources into classes R1, R2, R3, etc.
 – rule: can only request a resource from class Ri if holding no
   resources from any class R j for j ≥ i
 – unless each class contains only one resource, requires requesting
   several resources simultaneously
 – denote the highest class number for which T holds a resource by
   h(T )
155
     – if process T1 is requesting a resource of class k and is blocked
       because that resource is held by process T2, then h(T1) < k ≤ h(T2)
     – as the preceding inequality is strict, a circular wait is impossible
     – in some cases there is a natural division of resources into classes
       that makes this policy work nicely
     – in other cases, some processes are forced to acquire resources in an
       unnatural sequence, complicating their code and producing poor
       resource utilization
5. prevent simultaneous occurrence:
   • Show previous 4 rules cannot occur simultaneously.

5.4 Deadlock Avoidance
• Monitor all lock blocking and resource allocation to detect any potential
  formation of deadlock.
156


                            deadlock          unsafe


                                    safe

• Achieve better resource utilization, but additional overhead to avoid
  deadlock.

5.4.1 Banker’s Algorithm
 • Demonstrate a safe sequence of resource allocations to processes that ⇒
   no deadlock.
 • However, to do this requires that a process state its maximum resource
   needs.
157
                        R1   R2   R3   R4
                   T1   4    10    1    1   maximum needed
                   T2   2     4    1    2   for execution
                   T3   5     9    0    1   (M)
                   T1   23    5    1    0   currently
                   T2   1     2    1    0   allocated
                   T3   1     2    0    0   (C)

                   resource request (T1, R1) 2 → 3

                   T1 1      5    0    1 needed to
                   T2 1      2    0    2 execute
                   T3 4      7    0    1 (N = M −C)
• Is there a safe order of execution that avoids deadlock should each
  process require its maximum resource allocation?
158
                         total available resources
                      R1 R2 R3 R4
                       6 12 4 2 (TR)

                  current available resources
                       1 3 2 2 (CR = T R − ∑ Ccols)
                  T2 0 1 2 0 (CR = CR − NT 2)
                       2 5 3 2 (CR = CR + MT 2)
                  T1 1 0 3 1 (CR = CR − NT 1)
                       5 10 4 2 (CR = CR + MT 1)
                  T3 1 3 4 1 (CR = CR − NT 3)
                       6 12 4 2 (CR = CR + MT 3)
• If there is a choice of processes to choose for execution, it does not
  matter which path is taken.
• Example: If T1 or T3 could go to their maximum with the current
  resources, then choose either. A safe order starting with T1 exists if and
  only if a safe order starting with T3 exists.
• So a safe order exists (the left column in the table above) and hence the
  Banker’s Algorithm allows the resource request.
159
• The check for a safe order is performed for every allocation of resource
  to a process and then process scheduling is adjusted appropriately.

5.4.2 Allocation Graphs
 • One method to check for potential deadlock is to graph processes and
   resource usage at each moment a resource is allocated.

            resource              with multiple instances

            task

                        task is waiting for a resource instance

                        task is holding a resource instance


• Multiple instances are put into a resource so that a specific resource does
  not have to be requested. Instead, a generic request is made.
160
                      R1                             R3
                                                                  T4



       T1                            T2

                      R2

                                                     T3
• If a graph contains no cycles, no process in the system is deadlocked.
• If each resource has one instance, a cycle ⇒ deadlock.
 T1 → R1 → T2 → R3 → T3 → R2 → T1 (deadlock)
 T2 → R3 → T3 → R2 → T2 (deadlock)
• If any resource has several instances, a cycle ⇒ deadlock.
   – If T4 releases its resource, the cycle is broken.
161
• Create isomorphic graph without multiple instances (expensive and
  difficult):
                    R1                          R31 R32
                                                                 T4



     T1                            T2

                R21 R22
                                                 T3


• Use graph reduction to locate deadlocks:
162
     R1        R3
                    T4



T1        T2

     R2

               T3
163
     R1        R3




T1        T2

     R2

               T3
164
     R1   R3




T1

     R2

          T3
165
R1   R3




R2
     T3
166
                    R1                             R3




                    R2


• Problems:
  – for large numbers of processes and resources, detecting cycles is
    expensive.
  – there may be large number of graphs that must be examined, one for
    each particular allocation state of the system.
167
5.5 Detection and Recovery
• Instead of avoiding deadlock let it happen and recover.
   – ⇒ ability to discover deadlock
   – ⇒ preemption
• Discovering deadlock is not easy, e.g., build and check for cycles in
  allocation graph.
   – not on each resource allocation, but every T seconds or every time a
     resource cannot be immediately allocated
• Recovery involves preemption of one or more processes in a cycle.
   – decision is not easy and must prevent starvation
   – The preemption victim must be restarted, from beginning or some
     previous checkpoint state, if you cannot guarantee all resources have
     not changed.
   – even that is not enough as the victim may have made changes before
     the preemption.

5.6 Which Method To Chose?
• Maybe “none of the above”: just ignore the problem
168
  – if some process is blocked for rather a long time, assume it is
    deadlocked and abort it
  – do this automatically in transaction-processing systems, manually
    elsewhere
• Of the techniques studied, only the ordered-resource policy turns out to
  have much practical value.

Mais conteúdo relacionado

Mais procurados

Os6 2
Os6 2Os6 2
Os6 2
issbp
 

Mais procurados (20)

Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Deadlocks
 Deadlocks Deadlocks
Deadlocks
 
deadlock
deadlockdeadlock
deadlock
 
Deadlock Detection in Distributed Systems
Deadlock Detection in Distributed SystemsDeadlock Detection in Distributed Systems
Deadlock Detection in Distributed Systems
 
Deadlock
DeadlockDeadlock
Deadlock
 
Ch 4 deadlock
Ch 4 deadlockCh 4 deadlock
Ch 4 deadlock
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlock- Operating System
Deadlock- Operating SystemDeadlock- Operating System
Deadlock- Operating System
 
Os6 2
Os6 2Os6 2
Os6 2
 
Deadlock detection and recovery by saad symbian
Deadlock detection and recovery by saad symbianDeadlock detection and recovery by saad symbian
Deadlock detection and recovery by saad symbian
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
 
O ssvv62015
O ssvv62015O ssvv62015
O ssvv62015
 
Deadlock Presentation
Deadlock PresentationDeadlock Presentation
Deadlock Presentation
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
 
Lect05
Lect05Lect05
Lect05
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Operating system - Deadlock
Operating system - DeadlockOperating system - Deadlock
Operating system - Deadlock
 
Cs problem [repaired]
Cs problem [repaired]Cs problem [repaired]
Cs problem [repaired]
 

Destaque

Indirect Communications (Concurrency)
Indirect Communications (Concurrency)Indirect Communications (Concurrency)
Indirect Communications (Concurrency)
Sri Prasanna
 
Locks (Concurrency)
Locks (Concurrency)Locks (Concurrency)
Locks (Concurrency)
Sri Prasanna
 
Introduction to Concurrency
Introduction to ConcurrencyIntroduction to Concurrency
Introduction to Concurrency
Sri Prasanna
 
Direct Communications (Concurrency)
Direct Communications (Concurrency)Direct Communications (Concurrency)
Direct Communications (Concurrency)
Sri Prasanna
 
Other Approaches (Concurrency)
Other Approaches (Concurrency)Other Approaches (Concurrency)
Other Approaches (Concurrency)
Sri Prasanna
 
Coroutine (Concurrency)
Coroutine (Concurrency)Coroutine (Concurrency)
Coroutine (Concurrency)
Sri Prasanna
 
Qr codes para tech radar
Qr codes para tech radarQr codes para tech radar
Qr codes para tech radar
Sri Prasanna
 

Destaque (8)

Indirect Communications (Concurrency)
Indirect Communications (Concurrency)Indirect Communications (Concurrency)
Indirect Communications (Concurrency)
 
Locks (Concurrency)
Locks (Concurrency)Locks (Concurrency)
Locks (Concurrency)
 
Introduction to Concurrency
Introduction to ConcurrencyIntroduction to Concurrency
Introduction to Concurrency
 
Direct Communications (Concurrency)
Direct Communications (Concurrency)Direct Communications (Concurrency)
Direct Communications (Concurrency)
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Other Approaches (Concurrency)
Other Approaches (Concurrency)Other Approaches (Concurrency)
Other Approaches (Concurrency)
 
Coroutine (Concurrency)
Coroutine (Concurrency)Coroutine (Concurrency)
Coroutine (Concurrency)
 
Qr codes para tech radar
Qr codes para tech radarQr codes para tech radar
Qr codes para tech radar
 

Semelhante a Errors (Concurrency)

OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
KokilaK25
 
Lecture5
Lecture5Lecture5
Lecture5
jntu
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
rohassanie
 

Semelhante a Errors (Concurrency) (20)

OS Presentation 1 (1).pptx
OS Presentation 1 (1).pptxOS Presentation 1 (1).pptx
OS Presentation 1 (1).pptx
 
OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
 
Deadlock Detection Algorithm
Deadlock Detection AlgorithmDeadlock Detection Algorithm
Deadlock Detection Algorithm
 
Dead Lock In Operating Systems
Dead Lock In Operating SystemsDead Lock In Operating Systems
Dead Lock In Operating Systems
 
Lecture5
Lecture5Lecture5
Lecture5
 
Unit iv: Deadlocks
Unit iv: DeadlocksUnit iv: Deadlocks
Unit iv: Deadlocks
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).ppt
 
Section07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.pptSection07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.ppt
 
3 (1) [Autosaved].ppt
3 (1) [Autosaved].ppt3 (1) [Autosaved].ppt
3 (1) [Autosaved].ppt
 
OS-Part-06.pdf
OS-Part-06.pdfOS-Part-06.pdf
OS-Part-06.pdf
 
3 (2).ppt
3 (2).ppt3 (2).ppt
3 (2).ppt
 
Ipc feb4
Ipc feb4Ipc feb4
Ipc feb4
 
Evening out the uneven: dealing with skew in Flink
Evening out the uneven: dealing with skew in FlinkEvening out the uneven: dealing with skew in Flink
Evening out the uneven: dealing with skew in Flink
 
Real Time Operating System Concepts
Real Time Operating System ConceptsReal Time Operating System Concepts
Real Time Operating System Concepts
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlocks by wani zahoor
Deadlocks by wani zahoorDeadlocks by wani zahoor
Deadlocks by wani zahoor
 
FreeRTOS Slides annotations.pdf
FreeRTOS Slides annotations.pdfFreeRTOS Slides annotations.pdf
FreeRTOS Slides annotations.pdf
 
Deadlock
DeadlockDeadlock
Deadlock
 

Mais de Sri Prasanna

Mais de Sri Prasanna (20)

Qr codes para tech radar 2
Qr codes para tech radar 2Qr codes para tech radar 2
Qr codes para tech radar 2
 
Test
TestTest
Test
 
Test
TestTest
Test
 
assds
assdsassds
assds
 
assds
assdsassds
assds
 
asdsa
asdsaasdsa
asdsa
 
dsd
dsddsd
dsd
 
About stacks
About stacksAbout stacks
About stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
Network and distributed systems
Network and distributed systemsNetwork and distributed systems
Network and distributed systems
 
Introduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clustersIntroduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clusters
 
Mapreduce: Theory and implementation
Mapreduce: Theory and implementationMapreduce: Theory and implementation
Mapreduce: Theory and implementation
 
Other distributed systems
Other distributed systemsOther distributed systems
Other distributed systems
 
Distributed file systems
Distributed file systemsDistributed file systems
Distributed file systems
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

Errors (Concurrency)

  • 1. 5 Concurrent Errors 5.1 Race Condition • A race condition occurs when there is missing: – synchronization – mutual exclusion • Two or more tasks race along assuming synchronization or mutual exclusion has occurred. • Can be very difficult to locate (thought experiments). 5.2 No Progress 5.2.1 Live-lock • indefinite postponement: the “You go first” problem on simultaneous arrival • Caused by poor scheduling: Except as otherwise noted, the content of this presentation is licensed under the Creative Com- mons Attribution 2.5 License. 148
  • 2. 149 • There always exists some scheduling algorithm that can deal effectively with live-lock. 5.2.2 Starvation • A selection algorithm ignores one or more tasks so they are never executed. • I.e., lack of long-term fairness.
  • 3. 150 • Long-term (infinite) starvation is extremely rare, but short-term starvation can occur and is a problem. 5.2.3 Deadlock • Deadlock is the state when one or more processes are waiting for an event that will not occur. 5.2.3.1 Synchronization Deadlock • Failure in cooperation, so a blocked task is never unblocked (stuck waiting): void uMain::main() { uSemaphore s(0); // closed s.P(); // wait for lock to open } 5.2.3.2 Mutual Exclusion Deadlock • Failure to acquire a resource protected by mutual exclusion.
  • 4. 151 Deadlock, unless one of the cars is willing to backup. • Simple example using semaphores: uSemaphore L1(1), L2(1); // open task1 task2 L1.P() L2.P() // acquire opposite locks R1 R2 // access resource L2.P() L1.P() // acquire opposite locks R1 & R2 R2 & R1 // access resource
  • 5. 152 • There are 5 conditions that must occur for a set of processes to get into Deadlock. 1. There exists a shared resource requiring mutual exclusion. 2. A process holds a resource while waiting for access to a resource held by another process (hold and wait). 3. Once a process has gained access to a resource, the O/S cannot get it back (no preemption). 4. There exists a circular wait of processes on resources. 5. These conditions must occur simultaneously. 5.3 Deadlock Prevention • Eliminate one or more of the conditions required for a deadlock from an algorithm ⇒ deadlock can never occur. 5.3.1 Synchronization Prevention • Eliminate all synchronization from a program • ⇒ no communication
  • 6. 153 • all tasks must be completely independent, generating results through side-effects. 5.3.2 Mutual Exclusion Prevention • Deadlock can be prevented by eliminating one of the 5 conditions: 1. no mutual exclusion: impossible in many cases 2. no hold & wait: do not give any resource, unless all resources can be given • ⇒ poor resource utilization • possible starvation 3. allow preemption: • Preemption is dynamic ⇒ cannot apply statically. 4. no circular wait: • Control the order of resource allocations to prevent circular wait:
  • 7. 154 uSemaphore L1(1), L2(1); // open task1 task2 L1.P() L1.P() // acquire same locks R1 R1 // access resource L2.P() L2.P() // acquire same locks R2 R2 // access resource • Use an ordered resource policy: R1 < R2 < R3 ··· T1 T2 – divide all resources into classes R1, R2, R3, etc. – rule: can only request a resource from class Ri if holding no resources from any class R j for j ≥ i – unless each class contains only one resource, requires requesting several resources simultaneously – denote the highest class number for which T holds a resource by h(T )
  • 8. 155 – if process T1 is requesting a resource of class k and is blocked because that resource is held by process T2, then h(T1) < k ≤ h(T2) – as the preceding inequality is strict, a circular wait is impossible – in some cases there is a natural division of resources into classes that makes this policy work nicely – in other cases, some processes are forced to acquire resources in an unnatural sequence, complicating their code and producing poor resource utilization 5. prevent simultaneous occurrence: • Show previous 4 rules cannot occur simultaneously. 5.4 Deadlock Avoidance • Monitor all lock blocking and resource allocation to detect any potential formation of deadlock.
  • 9. 156 deadlock unsafe safe • Achieve better resource utilization, but additional overhead to avoid deadlock. 5.4.1 Banker’s Algorithm • Demonstrate a safe sequence of resource allocations to processes that ⇒ no deadlock. • However, to do this requires that a process state its maximum resource needs.
  • 10. 157 R1 R2 R3 R4 T1 4 10 1 1 maximum needed T2 2 4 1 2 for execution T3 5 9 0 1 (M) T1 23 5 1 0 currently T2 1 2 1 0 allocated T3 1 2 0 0 (C) resource request (T1, R1) 2 → 3 T1 1 5 0 1 needed to T2 1 2 0 2 execute T3 4 7 0 1 (N = M −C) • Is there a safe order of execution that avoids deadlock should each process require its maximum resource allocation?
  • 11. 158 total available resources R1 R2 R3 R4 6 12 4 2 (TR) current available resources 1 3 2 2 (CR = T R − ∑ Ccols) T2 0 1 2 0 (CR = CR − NT 2) 2 5 3 2 (CR = CR + MT 2) T1 1 0 3 1 (CR = CR − NT 1) 5 10 4 2 (CR = CR + MT 1) T3 1 3 4 1 (CR = CR − NT 3) 6 12 4 2 (CR = CR + MT 3) • If there is a choice of processes to choose for execution, it does not matter which path is taken. • Example: If T1 or T3 could go to their maximum with the current resources, then choose either. A safe order starting with T1 exists if and only if a safe order starting with T3 exists. • So a safe order exists (the left column in the table above) and hence the Banker’s Algorithm allows the resource request.
  • 12. 159 • The check for a safe order is performed for every allocation of resource to a process and then process scheduling is adjusted appropriately. 5.4.2 Allocation Graphs • One method to check for potential deadlock is to graph processes and resource usage at each moment a resource is allocated. resource with multiple instances task task is waiting for a resource instance task is holding a resource instance • Multiple instances are put into a resource so that a specific resource does not have to be requested. Instead, a generic request is made.
  • 13. 160 R1 R3 T4 T1 T2 R2 T3 • If a graph contains no cycles, no process in the system is deadlocked. • If each resource has one instance, a cycle ⇒ deadlock. T1 → R1 → T2 → R3 → T3 → R2 → T1 (deadlock) T2 → R3 → T3 → R2 → T2 (deadlock) • If any resource has several instances, a cycle ⇒ deadlock. – If T4 releases its resource, the cycle is broken.
  • 14. 161 • Create isomorphic graph without multiple instances (expensive and difficult): R1 R31 R32 T4 T1 T2 R21 R22 T3 • Use graph reduction to locate deadlocks:
  • 15. 162 R1 R3 T4 T1 T2 R2 T3
  • 16. 163 R1 R3 T1 T2 R2 T3
  • 17. 164 R1 R3 T1 R2 T3
  • 18. 165 R1 R3 R2 T3
  • 19. 166 R1 R3 R2 • Problems: – for large numbers of processes and resources, detecting cycles is expensive. – there may be large number of graphs that must be examined, one for each particular allocation state of the system.
  • 20. 167 5.5 Detection and Recovery • Instead of avoiding deadlock let it happen and recover. – ⇒ ability to discover deadlock – ⇒ preemption • Discovering deadlock is not easy, e.g., build and check for cycles in allocation graph. – not on each resource allocation, but every T seconds or every time a resource cannot be immediately allocated • Recovery involves preemption of one or more processes in a cycle. – decision is not easy and must prevent starvation – The preemption victim must be restarted, from beginning or some previous checkpoint state, if you cannot guarantee all resources have not changed. – even that is not enough as the victim may have made changes before the preemption. 5.6 Which Method To Chose? • Maybe “none of the above”: just ignore the problem
  • 21. 168 – if some process is blocked for rather a long time, assume it is deadlocked and abort it – do this automatically in transaction-processing systems, manually elsewhere • Of the techniques studied, only the ordered-resource policy turns out to have much practical value.