Movatterモバイル変換


[0]ホーム

URL:


Uploaded byNirmalaShinde3
PPTX, PDF8 views

CSE34115 1115 - Ch-06 Concurrency 2.pptx

El capítulo 6 del curso de sistemas operativos se centra en la concurrencia y el manejo de interbloqueos, describiendo las condiciones necesarias para que sucedan y las estrategias para prevenir, evitar, detectar o ignorar estos problemas. Se presentan conceptos como el algoritmo del banquero para la asignación segura de recursos y el ejemplo de los filósofos comensales para ilustrar el manejo de recursos compartidos. La gestión de interbloqueos se considera esencial para mantener la eficiencia y la estabilidad del sistema.

Embed presentation

Download to read offline
Welcome toCS 345 Operating SystemsConcurrency, Chapter 6 (15)
Tip #15: “goes to” Operator -->■ Actually --> is not an operator!■ It’s a combination of two separate operators, i.e. -- and >.■ The conditional code decrements variable x, which returns x’s original (notdecremented) value, and then compares the original value with 0 usingthe > operator.int main(){int x = 10;while( x --> 0 ) // x goes to 0{printf("%d ", x);}printf("n");}9 8 7 6 5 4 3 2 1 0Concurrenty (15)22
Conditions of Deadlock■ Necessary (but not sufficient)■ Mutual exclusion – Everyone abides by the rules■ only one process may use a resource at a time.■ no process may access resource allocated to another.■ Hold-and-wait■ a process may hold allocated resources while awaiting assignment of otherresources.■ No preemption■ no resource can be forced to free a resource.■ Circular wait (sufficient)■ a closed chain of processes exists, such that each process holds at least oneresource needed by the next process in the chain (consequence of the firstthree conditions)■ Other conditions are necessary but not sufficient for deadlock - all fourconditions must hold for deadlock - Unresolvable circular wait is the definitionof deadlock!Concurrenty (15)3
Handling Deadlock■ Four general approaches exist for dealing with deadlock.1. Prevent deadlock■ by adopting a policy that eliminates one of the conditions.2. Avoid deadlock■ by making the appropriate dynamic choices based on the current state ofresource allocation.3. Detect Deadlock■ by checking whether conditions 1 through 4 hold and take action to recover.4. Ignore Deadlock■ System may hang, so??Concurrenty (15)4
1. Prevent Deadlock■ Eliminate Mutual Exclusion■ Use non-sharable resourcesConcurrenty (15)5■ Eliminate Hold and wait■ Guarantee that when a process requests a resource, it does not hold anyother resources■ System calls requesting resources precede all others■ A process can only request resources when it has none■ Usually results in low utilization of resources■ Allow Preemption■ If a process holds resources and requests more thatcannot be allocated, all its other resources are preempted■ If you can’t hold all, you can’t hold any■ Process is restarted only when it can have all■ Works for resources whose state can be easily saved andrestored later such as registers or memory.
1. Prevent Deadlock■ Eliminate Circular Wait■ Impose a total ordering of all resources (transitivity, antisymmetry)■ Require that all processes request resources in increasing order.■ Whenever a process requests a resource, it must release all resources that arelower■ With this rule, the resource allocation graph can never have a cycle.■ May be impossible to find an ordering that satisfies everyone■ Put resources in topological order.1 Card reader≡2 Printer≡3 Plotter≡4 Tape drive≡5 Card punch≡Processes request resources anytime butmust be made in numerical order.A process may request first printer andthen a tape drive (order: 2, 4), but not aplotter and then a printer (order: 3, 2).Concurrenty (15)6
2. Avoid Deadlock■ Allow general requests, but grant only when safe■ Assume we know the maximum requests (claims) for eachprocess■ Process must state it needs■ Ie. max of 5 A objects, 3 B objects, 2 C objects.■ Do not need to use its max claims■ Ie. Ok to set max=5 and only use 3■ Can make requests at any time and in any order■ Process Initiation Denial■ Track current allocations■ Assume all processes may make maximum requests at the same time■ Only start process if it can’t result in deadlock regardless of allocationsConcurrenty (15)17
Resource Allocation Denial■ Safe State – We can finish all processes by some schedulingsequence■ Example: Finish P1, P4, P2, P5, P3■ Banker’s Algorithm (Dijkstra)■ Reject a request if it exceeds theprocesses’ declared maximum claims■ Grant a request if the new state would be safe Determining if a state is safe Find any process Pi for which we can meet it’s maximum requestsDon't forget already allocated resources Mark Pi as “done”, add its resources to available resource pool State is safe if we can mark all processes as “done” Block a process if the resources are not currently available or thenew state is not safeConcurrenty (15)18
Banker’s Algorithm ExampleA B C9 3 6A B C1 1 2A B CP1 3 2 2P2 6 1 3P3 3 1 4P4 4 2 2Claim200P4112P3115P2P1 001CBAAllocationAvailableResource024P4301P3201P2P1 222CBAC - A200P4112P3115P2P1 001CBAAllocation Are we in a safe state? Yes!6 2 3Concurrenty (15)19
XYZ CarpentryCarpentry Company XYZ has 4 employees, 9clamps, 2 drills, and 2 bottles of glue.Chair 4 clamps, 1 drillDesk 6 clamps, 1 drill, 1 gluePicture Frame 4 clamps, 1 drill, 1 glueBook Case 6 clamps, 1 drill, 1 glueConcurrenty (15)20
XYZ CarpentryClamp Drill Glue9 2 2Clamp Drill Glue2 1 1Clamp Drill GlueP1 4 1 0P2 6 1 1P3 4 1 1P4 6 1 1ClaimAvailableResource114P4102P3014P2P1 013CBANeeded1. Are we in a safe state?2. P1 needs a drill. Is it OK to give it to him?3. Then, P4 needs a glue. Would you give it to him?Clamp Drill GlueP1 1 0 0P2 2 0 1P3 2 1 0P4 2 0 0Allotted4 2 1Yes!Concurrenty (15)21
XYZ CarpentryClamp Drill Glue9 2 2Clamp Drill Glue2 0 1Clamp Drill GlueP1 4 1 0P2 6 1 1P3 4 1 1P4 6 1 1ClaimAvailableResource114P4102P3014P2P1 003CBANeeded1. Are we in a safe state?2. P1 needs a drill. Is it OK to give it to him?3. Then, P4 needs a glue. Would you give it to him?Clamp Drill GlueP1 1 1 0P2 2 0 1P3 2 1 0P4 2 0 0Allotted4 2 1Yes!Yes!Concurrenty (15)22
1. Are we in a safe state? Yes2. P1 needs a drill. Is it OK to give it to him? Yes3. Then, P4 needs a glue. Would you give it to him?XYZ CarpentryClamp Drill Glue9 2 2Clamp Drill Glue2 0 1Clamp Drill GlueP1 4 1 0P2 6 1 1P3 4 1 1P4 6 1 1ClaimAvailableResource114P4102P3014P2P1 003CBANeededClamp Drill GlueP1 1 1 0P2 2 0 1P3 2 1 0P4 2 0 0AllottedNo!2 0 001Concurrenty (15)23
3. Detect Deadlock■ Avoidance methods tend to limit access to resources■ Instead, grant arbitrary requests and watch for deadlock■ Can vary how often we check■ Early detection vs. overhead of checks■ Algorithm (Stallings, Figure 6.9)■ Preparation:■ Create table of process requests, current allocations■ Note available resources■ Mark processes with no resources■ Mark any process whose requests can be met (requests £ available resources)■ Include resources from that process as ‘available’ (this process can finish)■ If multiple processes available, pick any■ If any processes cannot be marked, they are part of a deadlockConcurrenty (15)2424
1E0D000CBATemporary AvailableDetection Example■ Mark P4 (not holding anything someone else wants)A B C D E0 0 0 0 1Request AllocationAvailableResourceA B C D EP1 0 1 0 0 1P2 0 0 1 0 1P3 0 0 0 0 1P4 1 0 1 0 1A B C D EP1 1 0 1 1 0P2 1 1 0 0 0P3 0 0 0 1 0P4 0 0 0 0 0A B C D E2 1 1 2 111000 Mark P3, new available: 0 0 0 1 1 Cannot mark P1 or P2, so we are deadlockedConcurrenty (15)2525
000CBATemporary AvailableDeadlocked?Are we deadlocked? (Why or why not?)A B C0 0 0A B CP1 0 0 0P2 2 0 2P3 0 0 0P4 1 0 0P5 0 0 2RequestsAvailableA B CP1 0 1 0P2 2 0 0P3 3 0 3P4 2 1 1P5 0 0 2Allocation ResourceA B C7 2 6Concurrenty (15)2626
000CBATemporary AvailableDeadlocked?Are we deadlocked?A B C0 0 0A B CP1 0 0 0P2 2 0 2P3 0 0 0P4 1 0 0P5 0 0 2RequestsAvailableA B CP1 0 1 0P2 2 0 0P3 3 0 3P4 2 1 1P5 0 0 2Allocation ResourceA B C7 2 6010 313 315 427 627NoConcurrenty (15)2727
Deadlock Recovery■ Several possible approaches■ Abort all deadlocked processes■ Simple but common■ Back up processes to a previously saved checkpoint, then restart■ Assumes we have checkpoints and a rollback mechanism■ Runs risk of repeating deadlock■ Assumes that the deadlock has enough timing dependencies it won’t happen■ Selectively abort processes until deadlock broken■ Preempt resources until deadlock broken■ Must roll back process to checkpoint prior to acquiring key resourceConcurrenty (15)2828
Deadlock Recovery■ Process Termination■ Kill them all■ One at a time■ Consider priority■ Time computing■ Who has most resources■ Resource Preemption■ Who gets preempted■ Do you consider process rollback and starvationConcurrenty (15)2929
4. Ignore Deadlock■ Perhaps not as silly as it sounds - since handling deadlock has a veryhigh cost, it might be better to ignore it.■ Usually, the OS assumes that even if deadlock occurs, the loss of datawill be minimal and the probability of occurrence of such a situation isvery low.■ Use resource prioritization for heavily used resources, otherwise, don’tworry about deadlock.■ Handling deadlock requires an algorithm constantly running inbackground - is 20% of the CPU time worth trying to prevent an eventwith a probability of occurrence less than 0.01%?■ If the alternative is to have a forced reboot once a year, that might beacceptable.■ When is ignoring deadlock unacceptable?Concurrenty (15)3030
Handling Deadlock Strategies■ Questions:■ Does prevention/avoidance/detection really work?■ How often should you run a detection process?■ How often is deadlock likely to occur?■ How expensive is a detection process?■ May group resources into classes and have different deadlockstrategies for each class. Examples:■ Swap Space - Prevent deadlocks by requiring all space to be allocated at once.(Avoidance also possible.)■ Tapes/Files - Avoidance can be effective here, but prevention by orderingresources also possible.■ Main Memory - Preemption a good approach■ Internal Resources (channels, etc.) - Prevention by ordering resourcesConcurrenty (15)3131
Advantages/DisadvantagesApproach Allocation Policy Scheme Advantages DisadvantagesPreventionConservative; undercommits resourcesRequesting allresources at onceWorks well for process that performa single burst of activityNo preemption necessaryInefficientDelays process initiationFuture resource requirementsmust be known by processesPreemptionConvenient when applied toresources whose state can be savedand restored easilyPreempts more often thannecessaryResource orderingFeasible to enforce via compile-timechecksNeeds no run-time computationsince problem is solved in systemdesignDisallows incremental resourcerequestsAvoidanceBurden on systemanalysist; Midwaybetween detection andpreventionManipulate to findat least one safepathNo preemption necessaryFuture resource requirementsmust be known by OSProcesses can be blocked for longperiodsDetectionVery liberal; requestedresources are grantedwhere possibleInvoke periodicallyto test for deadlockNever delays process initiationFacilitates online handlingInherent preemption lossesIgnoranceExtremely liberal;requested resources arealways grantedInfrequent deadlockacceptableNo CPU overheadNot safety criticalInherent preemption lossesConcurrenty (15)3232
The Dining Philosophers Problem■ 5 philosophers who only eat and think.■ Each need to use 2 forks for eating.■ There are only 5 forks.1. When only two philosophers are eating, are we in a safe state?2. Is deadlock possible?3. How can deadlock be prevented?Concurrenty (15)3333
A B C D E1 1 1 1 1A B C D EP1 0 0 0 0 0P2 0 1 1 0 0P3 0 0 0 0 0P4 0 0 0 1 1P5 1 0 0 0 1A B C D EP1 1 1 0 0 0P2 0 0 0 0 0P3 0 0 1 1 0P4 0 0 0 0 0P5 0 0 0 0 0A B C D EP1 1 1 0 0 0P2 0 1 1 0 0P3 0 0 1 1 0P4 0 0 0 1 1P5 1 0 0 0 1Two Philosophers EatingA B C D E0 0 0 0 1Claim AllocatedAvailableResourcesTwo philosophers are eating. Are we in a safe state?C - A10011 11111YesConcurrenty (15)3434
0E0D000CBATemporary AvailableAre the Philosophers Starving?A B C D E0 0 0 0 0Request AllocationAvailableResourceA B C D EP1 0 1 0 0 0P2 0 0 1 0 0P3 0 0 0 1 0P4 0 0 0 0 1P5 1 0 0 0 0A B C D EP1 1 0 0 0 0P2 0 1 0 0 0P3 0 0 1 0 0P4 0 0 0 1 0P5 0 0 0 0 1A B C D E1 1 1 1 1No philosophers are eating. Are we deadlocked? YesConcurrenty (15)3535
Solution??■ Each philosopher is a process.■ One semaphore per fork:■ forks: array[0..4] of semaphores■ Initialization: forks[i].count:=1 for i:=0..4Process Pi:repeatthink;wait(forks[i]);wait(forks[(i+1)%5]);eat;signal(forks[(i+1)%5]);signal(forks[i]);forever• Deadlock if each philosopher starts by picking left fork!Concurrenty (15)3636
Another Solution■ A solution: admit only 4philosophers at a time thattries to eat■ Then 1 philosopher canalways eat when the other 3are holding 1 fork■ Introduce semaphore T thatlimits to 4 the number ofphilosophers “sitting at thetable”■ Initialize: T.count:=4Process Pi:repeatthink;wait(T);wait(forks[i]);wait(forks[(i+1)%5]);eat;signal(forks[(i+1)%5]);signal(forks[i]);signal(T);foreverConcurrenty (15)3737
Other Solutions…■ Buy more Forks■ Equivalent to increasing resources■ Put fork down if 2ndfork busy■ “livelock” if philosophers stay synchronized■ Room Attendant■ Only let 4 of the philosophers into the room at once■ May have 4 philosophers in room, but only 1 can eat■ Left-Handed Philosophers (asymmetric solution)■ Grab forks in the other order (right fork, then left fork)■ Any mix will avoid deadlock (linear ordering on forks)■ A philosopher may only pick up forks in pairs.■ must allocate all resources at onceConcurrenty (15)Concurrenty (15)3838
Be Safe

Recommended

PPTX
Lecture 4 Deadlocks in operating systems.pptx
PPTX
Algorithm 4Chapter Four- Deadlock (5).pptx
PPTX
7308346-Deadlock.pptx
PPTX
Module 3 Deadlocks.pptx
PPTX
Deadlock
PPTX
deadlock in OS.pptx
PPT
Mch7 deadlock
PPTX
Gp1242 007 oer ppt
PPTX
Ch 4 deadlock
PPT
Dead Lock
PPT
Os module 2 d
PDF
Deadlock
PPT
Section07-Deadlocks (1).ppt
PPT
Section07-Deadlocks_operating_system.ppt
PPT
Deadlock Detection in Distributed Systems
PPT
Deadlocks
PPTX
Deadlock - An Operating System Concept.pptx
PDF
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
PPTX
deadlocks.pptx
PPT
Principles of Operating system and types
PPTX
Deadlock and Banking Algorithm
PDF
Deadlock Avoidance - OS
PPT
Lecture5
 
PDF
9 deadlock
PPTX
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
PPTX
OSLec14&15(Deadlocksinopratingsystem).pptx
PPT
14th November - Deadlock Prevention, Avoidance.ppt
PDF
The invasion of Alexander of Macedonia in India
PDF
1. Doing Academic Research: Problems and Issues, 2. Academic Research Writing...

More Related Content

PPTX
Lecture 4 Deadlocks in operating systems.pptx
PPTX
Algorithm 4Chapter Four- Deadlock (5).pptx
PPTX
7308346-Deadlock.pptx
PPTX
Module 3 Deadlocks.pptx
PPTX
Deadlock
PPTX
deadlock in OS.pptx
PPT
Mch7 deadlock
PPTX
Gp1242 007 oer ppt
Lecture 4 Deadlocks in operating systems.pptx
Algorithm 4Chapter Four- Deadlock (5).pptx
7308346-Deadlock.pptx
Module 3 Deadlocks.pptx
Deadlock
deadlock in OS.pptx
Mch7 deadlock
Gp1242 007 oer ppt

Similar to CSE34115 1115 - Ch-06 Concurrency 2.pptx

PPTX
Ch 4 deadlock
PPT
Dead Lock
PPT
Os module 2 d
PDF
Deadlock
PPT
Section07-Deadlocks (1).ppt
PPT
Section07-Deadlocks_operating_system.ppt
PPT
Deadlock Detection in Distributed Systems
PPT
Deadlocks
PPTX
Deadlock - An Operating System Concept.pptx
PDF
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
PPTX
deadlocks.pptx
PPT
Principles of Operating system and types
PPTX
Deadlock and Banking Algorithm
PDF
Deadlock Avoidance - OS
PPT
Lecture5
 
PDF
9 deadlock
PPTX
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
PPTX
OSLec14&15(Deadlocksinopratingsystem).pptx
PPT
14th November - Deadlock Prevention, Avoidance.ppt
Ch 4 deadlock
Dead Lock
Os module 2 d
Deadlock
Section07-Deadlocks (1).ppt
Section07-Deadlocks_operating_system.ppt
Deadlock Detection in Distributed Systems
Deadlocks
Deadlock - An Operating System Concept.pptx
deadlock.pdfdkfglknalkdnglqgjlejgooooo;o
deadlocks.pptx
Principles of Operating system and types
Deadlock and Banking Algorithm
Deadlock Avoidance - OS
Lecture5
 
9 deadlock
osvzjsjjdndnnssnnsnsndndndnndeadlock.pptx
OSLec14&15(Deadlocksinopratingsystem).pptx
14th November - Deadlock Prevention, Avoidance.ppt

Recently uploaded

PDF
The invasion of Alexander of Macedonia in India
PDF
1. Doing Academic Research: Problems and Issues, 2. Academic Research Writing...
PDF
Unit 4_ small scale industries & Entrepreneurship
PDF
AI Chatbots and Prompt Engineering - by Ms. Oceana Wong
PDF
AI and ICT for Teaching and Learning, Induction-cum-Training Programme, 5th 8...
PDF
IMPATT Diodes: Theory, Construction, Operation, and Microwave Applications"
PDF
CXC-AD Associate Degree Handbook (Revised)
PPTX
Chapter 3. Pharmaceutical Aids (pharmaceutics)
PDF
Cattolica University - Lab Generative and Agentic AI - Mario Bencivinni
PDF
Capitol Webinar November 2025 Emily Barnes.pdf
PPTX
Time Series Analysis - Meaning, Definition, Components and Application
PDF
UGC NET Paper 1 Syllabus | 10 Units Complete Guide for NTA JRF
PPTX
Time Series Analysis - Weighted (Unequal) Moving Average Method
PPTX
Finals - History and Geography Quiz - Around the World in 80 Questions - IITK
PPTX
Organize order into course in Odoo 18.2 _ Odoo 19
PPTX
G-Protein-Coupled Receptors (GPCRs): Structure, Mechanism, and Functions
PDF
Unit 1- Basics of Management Cha. of Magmt
PDF
Hybrid Electric Vehicles Descriptive Questions
PPTX
Quarter 3 lesson 2 of English Grade 8.pptx
PPTX
LYMPHATIC SYSTEM.pptx it includes lymph, lymph nodes, bone marrow, spleen
The invasion of Alexander of Macedonia in India
1. Doing Academic Research: Problems and Issues, 2. Academic Research Writing...
Unit 4_ small scale industries & Entrepreneurship
AI Chatbots and Prompt Engineering - by Ms. Oceana Wong
AI and ICT for Teaching and Learning, Induction-cum-Training Programme, 5th 8...
IMPATT Diodes: Theory, Construction, Operation, and Microwave Applications"
CXC-AD Associate Degree Handbook (Revised)
Chapter 3. Pharmaceutical Aids (pharmaceutics)
Cattolica University - Lab Generative and Agentic AI - Mario Bencivinni
Capitol Webinar November 2025 Emily Barnes.pdf
Time Series Analysis - Meaning, Definition, Components and Application
UGC NET Paper 1 Syllabus | 10 Units Complete Guide for NTA JRF
Time Series Analysis - Weighted (Unequal) Moving Average Method
Finals - History and Geography Quiz - Around the World in 80 Questions - IITK
Organize order into course in Odoo 18.2 _ Odoo 19
G-Protein-Coupled Receptors (GPCRs): Structure, Mechanism, and Functions
Unit 1- Basics of Management Cha. of Magmt
Hybrid Electric Vehicles Descriptive Questions
Quarter 3 lesson 2 of English Grade 8.pptx
LYMPHATIC SYSTEM.pptx it includes lymph, lymph nodes, bone marrow, spleen

CSE34115 1115 - Ch-06 Concurrency 2.pptx

  • 1.
    Welcome toCS 345Operating SystemsConcurrency, Chapter 6 (15)
  • 2.
    Tip #15: “goesto” Operator -->■ Actually --> is not an operator!■ It’s a combination of two separate operators, i.e. -- and >.■ The conditional code decrements variable x, which returns x’s original (notdecremented) value, and then compares the original value with 0 usingthe > operator.int main(){int x = 10;while( x --> 0 ) // x goes to 0{printf("%d ", x);}printf("n");}9 8 7 6 5 4 3 2 1 0Concurrenty (15)22
  • 3.
    Conditions of Deadlock■Necessary (but not sufficient)■ Mutual exclusion – Everyone abides by the rules■ only one process may use a resource at a time.■ no process may access resource allocated to another.■ Hold-and-wait■ a process may hold allocated resources while awaiting assignment of otherresources.■ No preemption■ no resource can be forced to free a resource.■ Circular wait (sufficient)■ a closed chain of processes exists, such that each process holds at least oneresource needed by the next process in the chain (consequence of the firstthree conditions)■ Other conditions are necessary but not sufficient for deadlock - all fourconditions must hold for deadlock - Unresolvable circular wait is the definitionof deadlock!Concurrenty (15)3
  • 4.
    Handling Deadlock■ Fourgeneral approaches exist for dealing with deadlock.1. Prevent deadlock■ by adopting a policy that eliminates one of the conditions.2. Avoid deadlock■ by making the appropriate dynamic choices based on the current state ofresource allocation.3. Detect Deadlock■ by checking whether conditions 1 through 4 hold and take action to recover.4. Ignore Deadlock■ System may hang, so??Concurrenty (15)4
  • 5.
    1. Prevent Deadlock■Eliminate Mutual Exclusion■ Use non-sharable resourcesConcurrenty (15)5■ Eliminate Hold and wait■ Guarantee that when a process requests a resource, it does not hold anyother resources■ System calls requesting resources precede all others■ A process can only request resources when it has none■ Usually results in low utilization of resources■ Allow Preemption■ If a process holds resources and requests more thatcannot be allocated, all its other resources are preempted■ If you can’t hold all, you can’t hold any■ Process is restarted only when it can have all■ Works for resources whose state can be easily saved andrestored later such as registers or memory.
  • 6.
    1. Prevent Deadlock■Eliminate Circular Wait■ Impose a total ordering of all resources (transitivity, antisymmetry)■ Require that all processes request resources in increasing order.■ Whenever a process requests a resource, it must release all resources that arelower■ With this rule, the resource allocation graph can never have a cycle.■ May be impossible to find an ordering that satisfies everyone■ Put resources in topological order.1 Card reader≡2 Printer≡3 Plotter≡4 Tape drive≡5 Card punch≡Processes request resources anytime butmust be made in numerical order.A process may request first printer andthen a tape drive (order: 2, 4), but not aplotter and then a printer (order: 3, 2).Concurrenty (15)6
  • 7.
    2. Avoid Deadlock■Allow general requests, but grant only when safe■ Assume we know the maximum requests (claims) for eachprocess■ Process must state it needs■ Ie. max of 5 A objects, 3 B objects, 2 C objects.■ Do not need to use its max claims■ Ie. Ok to set max=5 and only use 3■ Can make requests at any time and in any order■ Process Initiation Denial■ Track current allocations■ Assume all processes may make maximum requests at the same time■ Only start process if it can’t result in deadlock regardless of allocationsConcurrenty (15)17
  • 8.
    Resource Allocation Denial■Safe State – We can finish all processes by some schedulingsequence■ Example: Finish P1, P4, P2, P5, P3■ Banker’s Algorithm (Dijkstra)■ Reject a request if it exceeds theprocesses’ declared maximum claims■ Grant a request if the new state would be safe Determining if a state is safe Find any process Pi for which we can meet it’s maximum requestsDon't forget already allocated resources Mark Pi as “done”, add its resources to available resource pool State is safe if we can mark all processes as “done” Block a process if the resources are not currently available or thenew state is not safeConcurrenty (15)18
  • 9.
    Banker’s Algorithm ExampleAB C9 3 6A B C1 1 2A B CP1 3 2 2P2 6 1 3P3 3 1 4P4 4 2 2Claim200P4112P3115P2P1 001CBAAllocationAvailableResource024P4301P3201P2P1 222CBAC - A200P4112P3115P2P1 001CBAAllocation Are we in a safe state? Yes!6 2 3Concurrenty (15)19
  • 10.
    XYZ CarpentryCarpentry CompanyXYZ has 4 employees, 9clamps, 2 drills, and 2 bottles of glue.Chair 4 clamps, 1 drillDesk 6 clamps, 1 drill, 1 gluePicture Frame 4 clamps, 1 drill, 1 glueBook Case 6 clamps, 1 drill, 1 glueConcurrenty (15)20
  • 11.
    XYZ CarpentryClamp DrillGlue9 2 2Clamp Drill Glue2 1 1Clamp Drill GlueP1 4 1 0P2 6 1 1P3 4 1 1P4 6 1 1ClaimAvailableResource114P4102P3014P2P1 013CBANeeded1. Are we in a safe state?2. P1 needs a drill. Is it OK to give it to him?3. Then, P4 needs a glue. Would you give it to him?Clamp Drill GlueP1 1 0 0P2 2 0 1P3 2 1 0P4 2 0 0Allotted4 2 1Yes!Concurrenty (15)21
  • 12.
    XYZ CarpentryClamp DrillGlue9 2 2Clamp Drill Glue2 0 1Clamp Drill GlueP1 4 1 0P2 6 1 1P3 4 1 1P4 6 1 1ClaimAvailableResource114P4102P3014P2P1 003CBANeeded1. Are we in a safe state?2. P1 needs a drill. Is it OK to give it to him?3. Then, P4 needs a glue. Would you give it to him?Clamp Drill GlueP1 1 1 0P2 2 0 1P3 2 1 0P4 2 0 0Allotted4 2 1Yes!Yes!Concurrenty (15)22
  • 13.
    1. Are wein a safe state? Yes2. P1 needs a drill. Is it OK to give it to him? Yes3. Then, P4 needs a glue. Would you give it to him?XYZ CarpentryClamp Drill Glue9 2 2Clamp Drill Glue2 0 1Clamp Drill GlueP1 4 1 0P2 6 1 1P3 4 1 1P4 6 1 1ClaimAvailableResource114P4102P3014P2P1 003CBANeededClamp Drill GlueP1 1 1 0P2 2 0 1P3 2 1 0P4 2 0 0AllottedNo!2 0 001Concurrenty (15)23
  • 14.
    3. Detect Deadlock■Avoidance methods tend to limit access to resources■ Instead, grant arbitrary requests and watch for deadlock■ Can vary how often we check■ Early detection vs. overhead of checks■ Algorithm (Stallings, Figure 6.9)■ Preparation:■ Create table of process requests, current allocations■ Note available resources■ Mark processes with no resources■ Mark any process whose requests can be met (requests £ available resources)■ Include resources from that process as ‘available’ (this process can finish)■ If multiple processes available, pick any■ If any processes cannot be marked, they are part of a deadlockConcurrenty (15)2424
  • 15.
    1E0D000CBATemporary AvailableDetection Example■Mark P4 (not holding anything someone else wants)A B C D E0 0 0 0 1Request AllocationAvailableResourceA B C D EP1 0 1 0 0 1P2 0 0 1 0 1P3 0 0 0 0 1P4 1 0 1 0 1A B C D EP1 1 0 1 1 0P2 1 1 0 0 0P3 0 0 0 1 0P4 0 0 0 0 0A B C D E2 1 1 2 111000 Mark P3, new available: 0 0 0 1 1 Cannot mark P1 or P2, so we are deadlockedConcurrenty (15)2525
  • 16.
    000CBATemporary AvailableDeadlocked?Are wedeadlocked? (Why or why not?)A B C0 0 0A B CP1 0 0 0P2 2 0 2P3 0 0 0P4 1 0 0P5 0 0 2RequestsAvailableA B CP1 0 1 0P2 2 0 0P3 3 0 3P4 2 1 1P5 0 0 2Allocation ResourceA B C7 2 6Concurrenty (15)2626
  • 17.
    000CBATemporary AvailableDeadlocked?Are wedeadlocked?A B C0 0 0A B CP1 0 0 0P2 2 0 2P3 0 0 0P4 1 0 0P5 0 0 2RequestsAvailableA B CP1 0 1 0P2 2 0 0P3 3 0 3P4 2 1 1P5 0 0 2Allocation ResourceA B C7 2 6010 313 315 427 627NoConcurrenty (15)2727
  • 18.
    Deadlock Recovery■ Severalpossible approaches■ Abort all deadlocked processes■ Simple but common■ Back up processes to a previously saved checkpoint, then restart■ Assumes we have checkpoints and a rollback mechanism■ Runs risk of repeating deadlock■ Assumes that the deadlock has enough timing dependencies it won’t happen■ Selectively abort processes until deadlock broken■ Preempt resources until deadlock broken■ Must roll back process to checkpoint prior to acquiring key resourceConcurrenty (15)2828
  • 19.
    Deadlock Recovery■ ProcessTermination■ Kill them all■ One at a time■ Consider priority■ Time computing■ Who has most resources■ Resource Preemption■ Who gets preempted■ Do you consider process rollback and starvationConcurrenty (15)2929
  • 20.
    4. Ignore Deadlock■Perhaps not as silly as it sounds - since handling deadlock has a veryhigh cost, it might be better to ignore it.■ Usually, the OS assumes that even if deadlock occurs, the loss of datawill be minimal and the probability of occurrence of such a situation isvery low.■ Use resource prioritization for heavily used resources, otherwise, don’tworry about deadlock.■ Handling deadlock requires an algorithm constantly running inbackground - is 20% of the CPU time worth trying to prevent an eventwith a probability of occurrence less than 0.01%?■ If the alternative is to have a forced reboot once a year, that might beacceptable.■ When is ignoring deadlock unacceptable?Concurrenty (15)3030
  • 21.
    Handling Deadlock Strategies■Questions:■ Does prevention/avoidance/detection really work?■ How often should you run a detection process?■ How often is deadlock likely to occur?■ How expensive is a detection process?■ May group resources into classes and have different deadlockstrategies for each class. Examples:■ Swap Space - Prevent deadlocks by requiring all space to be allocated at once.(Avoidance also possible.)■ Tapes/Files - Avoidance can be effective here, but prevention by orderingresources also possible.■ Main Memory - Preemption a good approach■ Internal Resources (channels, etc.) - Prevention by ordering resourcesConcurrenty (15)3131
  • 22.
    Advantages/DisadvantagesApproach Allocation PolicyScheme Advantages DisadvantagesPreventionConservative; undercommits resourcesRequesting allresources at onceWorks well for process that performa single burst of activityNo preemption necessaryInefficientDelays process initiationFuture resource requirementsmust be known by processesPreemptionConvenient when applied toresources whose state can be savedand restored easilyPreempts more often thannecessaryResource orderingFeasible to enforce via compile-timechecksNeeds no run-time computationsince problem is solved in systemdesignDisallows incremental resourcerequestsAvoidanceBurden on systemanalysist; Midwaybetween detection andpreventionManipulate to findat least one safepathNo preemption necessaryFuture resource requirementsmust be known by OSProcesses can be blocked for longperiodsDetectionVery liberal; requestedresources are grantedwhere possibleInvoke periodicallyto test for deadlockNever delays process initiationFacilitates online handlingInherent preemption lossesIgnoranceExtremely liberal;requested resources arealways grantedInfrequent deadlockacceptableNo CPU overheadNot safety criticalInherent preemption lossesConcurrenty (15)3232
  • 23.
    The Dining PhilosophersProblem■ 5 philosophers who only eat and think.■ Each need to use 2 forks for eating.■ There are only 5 forks.1. When only two philosophers are eating, are we in a safe state?2. Is deadlock possible?3. How can deadlock be prevented?Concurrenty (15)3333
  • 24.
    A B CD E1 1 1 1 1A B C D EP1 0 0 0 0 0P2 0 1 1 0 0P3 0 0 0 0 0P4 0 0 0 1 1P5 1 0 0 0 1A B C D EP1 1 1 0 0 0P2 0 0 0 0 0P3 0 0 1 1 0P4 0 0 0 0 0P5 0 0 0 0 0A B C D EP1 1 1 0 0 0P2 0 1 1 0 0P3 0 0 1 1 0P4 0 0 0 1 1P5 1 0 0 0 1Two Philosophers EatingA B C D E0 0 0 0 1Claim AllocatedAvailableResourcesTwo philosophers are eating. Are we in a safe state?C - A10011 11111YesConcurrenty (15)3434
  • 25.
    0E0D000CBATemporary AvailableAre thePhilosophers Starving?A B C D E0 0 0 0 0Request AllocationAvailableResourceA B C D EP1 0 1 0 0 0P2 0 0 1 0 0P3 0 0 0 1 0P4 0 0 0 0 1P5 1 0 0 0 0A B C D EP1 1 0 0 0 0P2 0 1 0 0 0P3 0 0 1 0 0P4 0 0 0 1 0P5 0 0 0 0 1A B C D E1 1 1 1 1No philosophers are eating. Are we deadlocked? YesConcurrenty (15)3535
  • 26.
    Solution??■ Each philosopheris a process.■ One semaphore per fork:■ forks: array[0..4] of semaphores■ Initialization: forks[i].count:=1 for i:=0..4Process Pi:repeatthink;wait(forks[i]);wait(forks[(i+1)%5]);eat;signal(forks[(i+1)%5]);signal(forks[i]);forever• Deadlock if each philosopher starts by picking left fork!Concurrenty (15)3636
  • 27.
    Another Solution■ Asolution: admit only 4philosophers at a time thattries to eat■ Then 1 philosopher canalways eat when the other 3are holding 1 fork■ Introduce semaphore T thatlimits to 4 the number ofphilosophers “sitting at thetable”■ Initialize: T.count:=4Process Pi:repeatthink;wait(T);wait(forks[i]);wait(forks[(i+1)%5]);eat;signal(forks[(i+1)%5]);signal(forks[i]);signal(T);foreverConcurrenty (15)3737
  • 28.
    Other Solutions…■ Buymore Forks■ Equivalent to increasing resources■ Put fork down if 2ndfork busy■ “livelock” if philosophers stay synchronized■ Room Attendant■ Only let 4 of the philosophers into the room at once■ May have 4 philosophers in room, but only 1 can eat■ Left-Handed Philosophers (asymmetric solution)■ Grab forks in the other order (right fork, then left fork)■ Any mix will avoid deadlock (linear ordering on forks)■ A philosopher may only pick up forks in pairs.■ must allocate all resources at onceConcurrenty (15)Concurrenty (15)3838
  • 29.

Editor's Notes

  • #36 Philosophers eat and think for a random amount of time.(They are not self-aware. ha ha)Can’t proceed if my right fork has been picked up.Mutual exclusionBounded wait problemProgressPossible Solutions:Only one allowed to pick up forks. If not available, put back down. (All or nothing)2. Only allow four people to pick up – guarantees at least one to succeed.
  • #37 T allows only 4 people in.Maybe not the most efficient

[8]ページ先頭

©2009-2025 Movatter.jp