SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
Deadlock Avoidance                                                          1 / 38
Deadlock Avoidance
s   If we can detect deadlocks, can we avoid them?
s   Yes, but. . .
s   We can avoid deadlocks if certain information is available in advance

                                                                            1 / 38




Process Trajectories
    Process B
                              Printer

     Y4

     Y3

     Y2
                          T                           Plotter
     Y1
              R

                          S


                                                                     Process A
          P   Q      X1         X2      X3     X4

A needs the printer from X1 to X3 ; it needs the plotter from X2 to X4 .
B needs the plotter from Y1 to Y3 ; it needs the printer from Y2 to Y4 .
                                                                            2 / 38




                                         1
Problems
s   Warning sign: A and B are asking for resources in a different order
s   Green region: both A and B have the printer — impossible
s   Yellow region: both have the plotter
s   Blue — both have both devices. . .
s   The colored regions represent impossible states and cannot be entered

                                                                            3 / 38




Avoiding Deadlock
s   If the system ever enters the red-bordered state, it will deadlock
s   At time t, cannot schedule B
s   If we do, system will enter deadlock state
s   Must run A until X4

                                                                            4 / 38




                                         2
Alternate Process Trajectory
    Process B
                               Printer

     Y4

     Y3

     Y2
                           T                           Plotter
     Y1
              R

                           S


                                                                       Process A
          P   Q       X1         X2      X3     X4

If A and B ask for the plotter first, there’s no danger. B will clearly block if it’s
scheduled, so A will proceed. The dangerous state was where a process entered a
clear box that would deadlock.
                                                                              5 / 38




Safe and Unsafe States
s   A state is safe if not deadlocked and there is a scheduling order in which all
    processes can complete, even if they all ask for all of their resources at once
s   An unsafe state is not deadlocked, but no such scheduling order exists
s   It may even work, if a process releases resources at the right time

                                                                               6 / 38




                                          3
The Banker’s Algorithm (Dijkstra, 1965)
s   Assume we’re dealing with a single resource — perhaps dollars
s   Every customer has a “line of credit” — a maximum possible resource
    allocation”
s   The banker only has a certain amount of cash on hand
s   Not everyone will need all of their credit at once
s   Solution: only grant requests if they leave us in a safe state

                                                                            7 / 38




Example
      Has Max                           Has Max                         Has Max
  A 0         6                     A 1        6                     A 1        6
  B 0         5                     B 1        5                     B 2        5
  C 0         4                     C 2        4                     C 2        4
  D 0         7                     D 4        7                     D 4        7
     Free: 10                          Free: 2                          Free: 1
The first state is safe; we can grant the requests sequentially. The second state is
safe; we can grant C’s maximum request, let it run to completion, and then have
$4 to give to B or D.
If, after the second state, we give $1 to B, we enter an unsafe state — there isn’t
enough money left to satisfy all possible requests.
                                                                             8 / 38




                                        4
The Banker’s Algorithm for Multiple Resources
s   Build matrices C (currently assigned) and R (and still needed), just as we
    used for deadlock detection
s   Build vectors E (existing resources) and A (available), again as before
s   To see if a state is safe:
    1. Find a row R whose unmet needs are ≤ A
    2. Mark that row; add its resources to A
    3. Repeat until either all rows are marked, in which case the state is safe, or
       some are unmarked, in which case it’s unsafe
s   Run this algorithm any time a resource request is made

                                                                             9 / 38




Why Isn’t This Useful?
s   Every process must state its resource requirements at startup
s   This is rarely possible today
s   Processes come and go
s   Resources vanish as hardware breaks
s   Not really used these days. . .

                                                                            10 / 38




                                         5
Example: File Binding Time
s   Old mainframes: all file name binding is done immediately prior to execution.
    Also makes it easy to move files around
s   Classic Unix: file names on command line (but not clearly identifiable as such)
    or compiled-in to commands. Occasional overrides via environment variables or
    options.
s   GUIs: many files selected via menus
Early versus late binding is a major issuse in system design. Both choices here
have their advantages and disadvantages
                                                                            11 / 38




Deadlock Prevention                                                      12 / 38
Preventing Deadlocks
s   Practically speaking, we can’t avoid deadlocks
s   Can we prevent them in the real world?
s   Let’s go back to the four conditions:
    1.   Mutual exclusion
    2.   Hold and Wait
    3.   No preemption
    4.   Circular wait

                                                                           12 / 38




                                        6
Attacking Mutual Exclusion
s   Much less of an issue today — fewer single-user resources
s   Many of the existing ones are dedicated to single machines used by single
    individuals, i.e., CD drives
s   Printers are generally spooled
    ⇒ No contention; only the printer daemon requests it
s   Not a general solution, but useful nevertheless

                                                                          13 / 38




Attacking Holding and Wait
s   Could require processes to state their requirements up front
s   Still done sometimes in the mainframe world
s   Of course, if we could do that, we could use the Banker’s Algorithm

                                                                          14 / 38




                                         7
A Variant is Useful
s   Before requesting a resource, release all currently-held resources
s   Request all new ones at once
s   Doesn’t work if some resources must be held

                                                                         15 / 38




Attacking Circular Wait
s   Number each possible resource:
    1.   Scanner
    2.   Printer
    3.   Tape drive
    4.   ...
s   Resources must be requested in numerical order
s   Can’t deadlock — prevents the out-of-order scenario we saw earlier
s   Used on old mainframes
s   Can combine this with release-and-rerequest

                                                                         16 / 38




                                          8
Mainframe Resources
s   Wait until enough tape drives are available
s   Wait until memory region is available
s   Wait for all disk files to be free
Order based on typical wait time — disk files freed up quickly, while tape drives
waited for operators to find and mount tape reels
                                                                          17 / 38




Two-Phase Locking
s   Frequently used in databases
s   Processes need to lock several records then update them all
s   Phase 1: try locking each record, one at a time
s   On failure, release them all and restart
s   When they’re all locked, do the updates and then the release
s   Effectively the same as “request everything up front”

                                                                         18 / 38




                                         9
What’s Really Done About Deadlocks?
s   In the OS, nothing. . .
s   Overprovision some resources, such as process slots
s   But — still very important in some applications, notably databases

                                                                         19 / 38




What About Linux?
s   No deadlock prevention or detection for applications or threads
s   The kernel does care about deadlocks for itself.


    /*   We can’t just spew out the rules
     *   here because we might fill the
     *   available socket buffer space and
     *   deadlock waiting for auditctl to
     *   read from it... which isn’t ever
     *   going to happen if we’re actually
     *   running in the context of auditctl
     *   trying to _send_ the stuff */


                                                                         20 / 38




                                        10
Scheduling                                                              21 / 38
Scheduling
s   Suppose several processes are runnable?
s   Which one is run next?
s   Many different ways to make this decision

                                                                          21 / 38




Environments
s   Old batch systems didn’t have a scheduler; they just read whatever was next
    on the input tape
s   Actually, they did have a scheduler: the person who loaded the card decks
    onto the tape
s   Hybrid batch/time-sharing systems tend to give priority to short timesharing
    requests
s   Still a policy today: must give priority to interactive requests

                                                                          22 / 38




                                       11
Process Behavior
s   Processes alternate CPU use with I/O requests
s   I/O requests frequently block, either waiting for input or when too much has
    been written and no buffer space is available
s   CPU-bound processes think more than they read or write
s   I/O-bound processes do lots of I/O; it’s (usually) not that the I/O operations
    are so time-consuming
s   Absolute speed of CPU and I/O devices is irrelevant; what matters is the ratio
s   CPUs have been getting much faster relative to disks

                                                                          23 / 38




When to Make Scheduling Decisions
s   After a fork — run the parent or child?
s   On process exit
s   When a process blocks
s   When I/O completes
s   Sometimes, after timer interrupts

                                                                          24 / 38




                                        12
Preemptive vs. Nonpreemptive Schedulers
s   Nonpreemptive scheduler: lets a process run as long as it wants
s   Only switches when it blocks
s   Preemptive: switches after a time quantum

                                                                       25 / 38




Categories of Scheduling Algorithms
s   Batch — responsiveness isn’t important; preemption moderately important
s   Interactive — must satisfy a human; preemption important
s   Real-time — often nonpreemptive

                                                                       26 / 38




                                       13
Goals
s   Fairness — give each process its share of the CPU
s   Policy and enforcement — give preference to work that is administratively
    favored; prevent subversion of OS scheduling policy
s   Balance — keep all parts of the system busy

                                                                          27 / 38




Goals: Batch Systems
s   Throughput — maximize jobs/hour
s   Turnaround time — return jobs quickly. Often want to finish short jobs very
    quickly
s   CPU utilization

                                                                          28 / 38




                                       14
Interactive Systems
s   Response time — respond quickly to user requests
s   Meet user expectations — psychological
    x   Users have a sense of “cheap” and “expensive” requests
    x   Users are happier if “cheap” requests finish quickly
    x   “Cheap” and “expensive” don’t always correspond to reality!

                                                                           29 / 38




Real-Time Systems
s   Meet deadlines — avoid losing data (or worse!)
s   Predictability — users must know when their requests will finish
s   Requires careful engineering to match priorities to actual completion times and
    available resources

                                                                           30 / 38




                                        15
Batch Schedulers                              31 / 38
Batch Schedulers
s   First-come, first-served
s   Shortest first
s   Shortest remaining time first
s   Three-level scheduler

                                               31 / 38




First-Come, First-Served
s   Run the first process on the run queue
s   Never preempt based on timer
s   Seems simple; just like waiting in line
s   Not very fair

                                               32 / 38




                                        16
First-Come, First-Served
s   Imagine a CPU-bound process A: thinks for 1 second, then reads 1 disk block
s   There’s also an I/O-bound process B that needs to read 1000 blocks
s   A runs for 1 second, then issues an I/O request
s   B runs for almost no time, then issues an I/O request
s   A then runs for another second
s   It takes 1000 seconds for B to finish

                                                                        33 / 38




Shortest First
s   Suppose you know the time requirements of each job
s   A needs 8 seconds, B needs 4, C needs 4, D needs 4
s   Run B, C, D, A
s   Nonpreemptive
s   Provably fair:
    x   Suppose four jobs have runtimes of a, b, c, and d
    x   First finishes at time a, second at a + b, etc
    x   Mean turnaround is (4a + 3b + 2c + d)/4
    x   d contributes less to the mean

                                                                        34 / 38




                                        17
Optimality Requires Simultaneous Availability
s   Jobs don’t all arrive at the same time
s   Can’t make optimal scheduling decision without complete knowledge
s   Example: jobs with times of 2,4,1,1,1 that arrive at times 0,0,3,3,3
s   Shortest-first runs A, B, C, D, E; average wait is 4.6 secs
s   If we run B, C, D, E, A, average wait is 4.4 secs
s   While B is running, more jobs arrive, allowing a better decision for the total
    load

                                                                            35 / 38




Shortest Remaining Time Next
s   Preemptive variant of FCFS
s   Still need to know run-times in advance
s   Helps short jobs get good service
s   May have a problem with indefinite overtaking

                                                                            36 / 38




                                         18
Three-Level Scheduler
 sFirst stage: job queue
s Select different types of jobs (i.e., I/O- or CPU-bound) to balance workload
s Note: relies on humans classify jobs in advance
s Second stage: availability of main memory
⇒ Closely linked to virtual memory system; let’s defer that
s CPU scheduler

                                                                          37 / 38




 User Requirements
 s   Users must be able to specify job characteristics: estimated CPU time, I/O
     versus CPU balance, perhaps memory
 s   Scheduler categories must reflect technical and managerial issues
 s   Lying about characteristics may give better turnaround times, but at hte
     expense of total system throughput
 s   Should the resonse be technical or administrative?

                                                                          38 / 38




                                        19

Más contenido relacionado

Similar a Deadlock

Similar a Deadlock (20)

Dead Lock In Operating Systems
Dead Lock In Operating SystemsDead Lock In Operating Systems
Dead Lock In Operating Systems
 
Lecture5
Lecture5Lecture5
Lecture5
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
Deadlock Detection Algorithm
Deadlock Detection AlgorithmDeadlock Detection Algorithm
Deadlock Detection Algorithm
 
OS-Part-06.pdf
OS-Part-06.pdfOS-Part-06.pdf
OS-Part-06.pdf
 
Dead Lock
Dead LockDead Lock
Dead Lock
 
Os case study word
Os case study wordOs case study word
Os case study word
 
Section07-Deadlocks.pdf
Section07-Deadlocks.pdfSection07-Deadlocks.pdf
Section07-Deadlocks.pdf
 
Deadlocks
 Deadlocks Deadlocks
Deadlocks
 
Os6 2
Os6 2Os6 2
Os6 2
 
A petri-net
A petri-netA petri-net
A petri-net
 
9 deadlock
9 deadlock9 deadlock
9 deadlock
 
Deadlock Detection in Distributed Systems
Deadlock Detection in Distributed SystemsDeadlock Detection in Distributed Systems
Deadlock Detection in Distributed Systems
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
3 (2).ppt
3 (2).ppt3 (2).ppt
3 (2).ppt
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
Deadlock
DeadlockDeadlock
Deadlock
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Deadlock in Operating System
Deadlock in Operating SystemDeadlock in Operating System
Deadlock in Operating System
 

Más de Mohd Tousif

Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statementsMohd Tousif
 
SQL practice questions set
SQL practice questions setSQL practice questions set
SQL practice questions setMohd Tousif
 
Introduction to Databases
Introduction to DatabasesIntroduction to Databases
Introduction to DatabasesMohd Tousif
 
Entity Relationship Model - An Example
Entity Relationship Model - An ExampleEntity Relationship Model - An Example
Entity Relationship Model - An ExampleMohd Tousif
 
Entity Relationship (ER) Model Questions
Entity Relationship (ER) Model QuestionsEntity Relationship (ER) Model Questions
Entity Relationship (ER) Model QuestionsMohd Tousif
 
Entity Relationship (ER) Model
Entity Relationship (ER) ModelEntity Relationship (ER) Model
Entity Relationship (ER) ModelMohd Tousif
 
SQL Practice Question set
SQL Practice Question set SQL Practice Question set
SQL Practice Question set Mohd Tousif
 
Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1Mohd Tousif
 
Data Definition Language (DDL)
Data Definition Language (DDL) Data Definition Language (DDL)
Data Definition Language (DDL) Mohd Tousif
 
Data Warehouse Concepts and Architecture
Data Warehouse Concepts and ArchitectureData Warehouse Concepts and Architecture
Data Warehouse Concepts and ArchitectureMohd Tousif
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2Mohd Tousif
 
SQL practice questions - set 3
SQL practice questions - set 3SQL practice questions - set 3
SQL practice questions - set 3Mohd Tousif
 
SQL practice questions for beginners
SQL practice questions for beginnersSQL practice questions for beginners
SQL practice questions for beginnersMohd Tousif
 
Oracle sql tutorial
Oracle sql tutorialOracle sql tutorial
Oracle sql tutorialMohd Tousif
 
Sql (Introduction to Structured Query language)
Sql (Introduction to Structured Query language)Sql (Introduction to Structured Query language)
Sql (Introduction to Structured Query language)Mohd Tousif
 
System components of windows xp
System components of windows xpSystem components of windows xp
System components of windows xpMohd Tousif
 

Más de Mohd Tousif (20)

Sql commands
Sql commandsSql commands
Sql commands
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statements
 
SQL practice questions set
SQL practice questions setSQL practice questions set
SQL practice questions set
 
Introduction to Databases
Introduction to DatabasesIntroduction to Databases
Introduction to Databases
 
Entity Relationship Model - An Example
Entity Relationship Model - An ExampleEntity Relationship Model - An Example
Entity Relationship Model - An Example
 
Entity Relationship (ER) Model Questions
Entity Relationship (ER) Model QuestionsEntity Relationship (ER) Model Questions
Entity Relationship (ER) Model Questions
 
Entity Relationship (ER) Model
Entity Relationship (ER) ModelEntity Relationship (ER) Model
Entity Relationship (ER) Model
 
SQL Practice Question set
SQL Practice Question set SQL Practice Question set
SQL Practice Question set
 
Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1Introduction to Databases - Assignment_1
Introduction to Databases - Assignment_1
 
Data Definition Language (DDL)
Data Definition Language (DDL) Data Definition Language (DDL)
Data Definition Language (DDL)
 
Data Warehouse Concepts and Architecture
Data Warehouse Concepts and ArchitectureData Warehouse Concepts and Architecture
Data Warehouse Concepts and Architecture
 
SQL practice questions set - 2
SQL practice questions set - 2SQL practice questions set - 2
SQL practice questions set - 2
 
SQL practice questions - set 3
SQL practice questions - set 3SQL practice questions - set 3
SQL practice questions - set 3
 
SQL practice questions for beginners
SQL practice questions for beginnersSQL practice questions for beginners
SQL practice questions for beginners
 
Oracle sql tutorial
Oracle sql tutorialOracle sql tutorial
Oracle sql tutorial
 
Sql (Introduction to Structured Query language)
Sql (Introduction to Structured Query language)Sql (Introduction to Structured Query language)
Sql (Introduction to Structured Query language)
 
Sql commands
Sql commandsSql commands
Sql commands
 
Virtual box
Virtual boxVirtual box
Virtual box
 
Algorithm o.s.
Algorithm o.s.Algorithm o.s.
Algorithm o.s.
 
System components of windows xp
System components of windows xpSystem components of windows xp
System components of windows xp
 

Último

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
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
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
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
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
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
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 

Último (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
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
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
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
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
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...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 

Deadlock

  • 1. Deadlock Avoidance 1 / 38 Deadlock Avoidance s If we can detect deadlocks, can we avoid them? s Yes, but. . . s We can avoid deadlocks if certain information is available in advance 1 / 38 Process Trajectories Process B Printer Y4 Y3 Y2 T Plotter Y1 R S Process A P Q X1 X2 X3 X4 A needs the printer from X1 to X3 ; it needs the plotter from X2 to X4 . B needs the plotter from Y1 to Y3 ; it needs the printer from Y2 to Y4 . 2 / 38 1
  • 2. Problems s Warning sign: A and B are asking for resources in a different order s Green region: both A and B have the printer — impossible s Yellow region: both have the plotter s Blue — both have both devices. . . s The colored regions represent impossible states and cannot be entered 3 / 38 Avoiding Deadlock s If the system ever enters the red-bordered state, it will deadlock s At time t, cannot schedule B s If we do, system will enter deadlock state s Must run A until X4 4 / 38 2
  • 3. Alternate Process Trajectory Process B Printer Y4 Y3 Y2 T Plotter Y1 R S Process A P Q X1 X2 X3 X4 If A and B ask for the plotter first, there’s no danger. B will clearly block if it’s scheduled, so A will proceed. The dangerous state was where a process entered a clear box that would deadlock. 5 / 38 Safe and Unsafe States s A state is safe if not deadlocked and there is a scheduling order in which all processes can complete, even if they all ask for all of their resources at once s An unsafe state is not deadlocked, but no such scheduling order exists s It may even work, if a process releases resources at the right time 6 / 38 3
  • 4. The Banker’s Algorithm (Dijkstra, 1965) s Assume we’re dealing with a single resource — perhaps dollars s Every customer has a “line of credit” — a maximum possible resource allocation” s The banker only has a certain amount of cash on hand s Not everyone will need all of their credit at once s Solution: only grant requests if they leave us in a safe state 7 / 38 Example Has Max Has Max Has Max A 0 6 A 1 6 A 1 6 B 0 5 B 1 5 B 2 5 C 0 4 C 2 4 C 2 4 D 0 7 D 4 7 D 4 7 Free: 10 Free: 2 Free: 1 The first state is safe; we can grant the requests sequentially. The second state is safe; we can grant C’s maximum request, let it run to completion, and then have $4 to give to B or D. If, after the second state, we give $1 to B, we enter an unsafe state — there isn’t enough money left to satisfy all possible requests. 8 / 38 4
  • 5. The Banker’s Algorithm for Multiple Resources s Build matrices C (currently assigned) and R (and still needed), just as we used for deadlock detection s Build vectors E (existing resources) and A (available), again as before s To see if a state is safe: 1. Find a row R whose unmet needs are ≤ A 2. Mark that row; add its resources to A 3. Repeat until either all rows are marked, in which case the state is safe, or some are unmarked, in which case it’s unsafe s Run this algorithm any time a resource request is made 9 / 38 Why Isn’t This Useful? s Every process must state its resource requirements at startup s This is rarely possible today s Processes come and go s Resources vanish as hardware breaks s Not really used these days. . . 10 / 38 5
  • 6. Example: File Binding Time s Old mainframes: all file name binding is done immediately prior to execution. Also makes it easy to move files around s Classic Unix: file names on command line (but not clearly identifiable as such) or compiled-in to commands. Occasional overrides via environment variables or options. s GUIs: many files selected via menus Early versus late binding is a major issuse in system design. Both choices here have their advantages and disadvantages 11 / 38 Deadlock Prevention 12 / 38 Preventing Deadlocks s Practically speaking, we can’t avoid deadlocks s Can we prevent them in the real world? s Let’s go back to the four conditions: 1. Mutual exclusion 2. Hold and Wait 3. No preemption 4. Circular wait 12 / 38 6
  • 7. Attacking Mutual Exclusion s Much less of an issue today — fewer single-user resources s Many of the existing ones are dedicated to single machines used by single individuals, i.e., CD drives s Printers are generally spooled ⇒ No contention; only the printer daemon requests it s Not a general solution, but useful nevertheless 13 / 38 Attacking Holding and Wait s Could require processes to state their requirements up front s Still done sometimes in the mainframe world s Of course, if we could do that, we could use the Banker’s Algorithm 14 / 38 7
  • 8. A Variant is Useful s Before requesting a resource, release all currently-held resources s Request all new ones at once s Doesn’t work if some resources must be held 15 / 38 Attacking Circular Wait s Number each possible resource: 1. Scanner 2. Printer 3. Tape drive 4. ... s Resources must be requested in numerical order s Can’t deadlock — prevents the out-of-order scenario we saw earlier s Used on old mainframes s Can combine this with release-and-rerequest 16 / 38 8
  • 9. Mainframe Resources s Wait until enough tape drives are available s Wait until memory region is available s Wait for all disk files to be free Order based on typical wait time — disk files freed up quickly, while tape drives waited for operators to find and mount tape reels 17 / 38 Two-Phase Locking s Frequently used in databases s Processes need to lock several records then update them all s Phase 1: try locking each record, one at a time s On failure, release them all and restart s When they’re all locked, do the updates and then the release s Effectively the same as “request everything up front” 18 / 38 9
  • 10. What’s Really Done About Deadlocks? s In the OS, nothing. . . s Overprovision some resources, such as process slots s But — still very important in some applications, notably databases 19 / 38 What About Linux? s No deadlock prevention or detection for applications or threads s The kernel does care about deadlocks for itself. /* We can’t just spew out the rules * here because we might fill the * available socket buffer space and * deadlock waiting for auditctl to * read from it... which isn’t ever * going to happen if we’re actually * running in the context of auditctl * trying to _send_ the stuff */ 20 / 38 10
  • 11. Scheduling 21 / 38 Scheduling s Suppose several processes are runnable? s Which one is run next? s Many different ways to make this decision 21 / 38 Environments s Old batch systems didn’t have a scheduler; they just read whatever was next on the input tape s Actually, they did have a scheduler: the person who loaded the card decks onto the tape s Hybrid batch/time-sharing systems tend to give priority to short timesharing requests s Still a policy today: must give priority to interactive requests 22 / 38 11
  • 12. Process Behavior s Processes alternate CPU use with I/O requests s I/O requests frequently block, either waiting for input or when too much has been written and no buffer space is available s CPU-bound processes think more than they read or write s I/O-bound processes do lots of I/O; it’s (usually) not that the I/O operations are so time-consuming s Absolute speed of CPU and I/O devices is irrelevant; what matters is the ratio s CPUs have been getting much faster relative to disks 23 / 38 When to Make Scheduling Decisions s After a fork — run the parent or child? s On process exit s When a process blocks s When I/O completes s Sometimes, after timer interrupts 24 / 38 12
  • 13. Preemptive vs. Nonpreemptive Schedulers s Nonpreemptive scheduler: lets a process run as long as it wants s Only switches when it blocks s Preemptive: switches after a time quantum 25 / 38 Categories of Scheduling Algorithms s Batch — responsiveness isn’t important; preemption moderately important s Interactive — must satisfy a human; preemption important s Real-time — often nonpreemptive 26 / 38 13
  • 14. Goals s Fairness — give each process its share of the CPU s Policy and enforcement — give preference to work that is administratively favored; prevent subversion of OS scheduling policy s Balance — keep all parts of the system busy 27 / 38 Goals: Batch Systems s Throughput — maximize jobs/hour s Turnaround time — return jobs quickly. Often want to finish short jobs very quickly s CPU utilization 28 / 38 14
  • 15. Interactive Systems s Response time — respond quickly to user requests s Meet user expectations — psychological x Users have a sense of “cheap” and “expensive” requests x Users are happier if “cheap” requests finish quickly x “Cheap” and “expensive” don’t always correspond to reality! 29 / 38 Real-Time Systems s Meet deadlines — avoid losing data (or worse!) s Predictability — users must know when their requests will finish s Requires careful engineering to match priorities to actual completion times and available resources 30 / 38 15
  • 16. Batch Schedulers 31 / 38 Batch Schedulers s First-come, first-served s Shortest first s Shortest remaining time first s Three-level scheduler 31 / 38 First-Come, First-Served s Run the first process on the run queue s Never preempt based on timer s Seems simple; just like waiting in line s Not very fair 32 / 38 16
  • 17. First-Come, First-Served s Imagine a CPU-bound process A: thinks for 1 second, then reads 1 disk block s There’s also an I/O-bound process B that needs to read 1000 blocks s A runs for 1 second, then issues an I/O request s B runs for almost no time, then issues an I/O request s A then runs for another second s It takes 1000 seconds for B to finish 33 / 38 Shortest First s Suppose you know the time requirements of each job s A needs 8 seconds, B needs 4, C needs 4, D needs 4 s Run B, C, D, A s Nonpreemptive s Provably fair: x Suppose four jobs have runtimes of a, b, c, and d x First finishes at time a, second at a + b, etc x Mean turnaround is (4a + 3b + 2c + d)/4 x d contributes less to the mean 34 / 38 17
  • 18. Optimality Requires Simultaneous Availability s Jobs don’t all arrive at the same time s Can’t make optimal scheduling decision without complete knowledge s Example: jobs with times of 2,4,1,1,1 that arrive at times 0,0,3,3,3 s Shortest-first runs A, B, C, D, E; average wait is 4.6 secs s If we run B, C, D, E, A, average wait is 4.4 secs s While B is running, more jobs arrive, allowing a better decision for the total load 35 / 38 Shortest Remaining Time Next s Preemptive variant of FCFS s Still need to know run-times in advance s Helps short jobs get good service s May have a problem with indefinite overtaking 36 / 38 18
  • 19. Three-Level Scheduler sFirst stage: job queue s Select different types of jobs (i.e., I/O- or CPU-bound) to balance workload s Note: relies on humans classify jobs in advance s Second stage: availability of main memory ⇒ Closely linked to virtual memory system; let’s defer that s CPU scheduler 37 / 38 User Requirements s Users must be able to specify job characteristics: estimated CPU time, I/O versus CPU balance, perhaps memory s Scheduler categories must reflect technical and managerial issues s Lying about characteristics may give better turnaround times, but at hte expense of total system throughput s Should the resonse be technical or administrative? 38 / 38 19