Chapter 5 process management

1,904 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,904
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
80
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 5 process management

  1. 1. Chapter 5 : Process Management <ul><li>Deadlock </li></ul><ul><ul><li>7 Cases of Deadlock </li></ul></ul><ul><ul><li>Conditions for Deadlock </li></ul></ul><ul><ul><li>Modeling Deadlocks </li></ul></ul><ul><ul><li>Strategies for Handling Deadlocks </li></ul></ul><ul><ul><li>Avoidance </li></ul></ul><ul><ul><li>Detection </li></ul></ul><ul><ul><li>Recovery </li></ul></ul><ul><li>Starvation </li></ul><ul><li>Process Synchronization </li></ul><ul><li>Deadlock Starvation </li></ul><ul><li>Management Management </li></ul>
  2. 2. A Lack of Process Synchronization Causes Deadlock or Starvation <ul><li>Deadlock (“deadly embrace”) -- a system-wide tangle of resource requests that begins when 2+ jobs are put on hold. </li></ul><ul><ul><li>Each job is waiting for a vital resource to become available. </li></ul></ul><ul><ul><li>Needed resources are held by other jobs also waiting to run but can’t because they’re waiting for other unavailable resources. </li></ul></ul><ul><ul><li>The jobs come to a standstill. </li></ul></ul><ul><ul><li>The deadlock is complete if remainder of system comes to a standstill as well. </li></ul></ul><ul><li>Resolved via external intervention. </li></ul>
  3. 3. Deadlock <ul><li>Deadlock is more serious than indefinite postponement or starvation because it affects more than one job. </li></ul><ul><li>Because resources are being tied up, the entire system (not just a few programs) is affected. </li></ul><ul><li>Requires outside intervention (e.g., operators or users terminate a job). </li></ul>
  4. 4. Seven Cases of Deadlocks <ul><li>1. Deadlocks on file requests </li></ul><ul><li>2. Deadlocks in databases </li></ul><ul><li>3. Deadlocks in dedicated device allocation </li></ul><ul><li>4. Deadlocks in multiple device allocation </li></ul><ul><li>5. Deadlocks in spooling </li></ul><ul><li>6. Deadlocks in disk sharing </li></ul><ul><li>7. Deadlocks in a network </li></ul>
  5. 5. Case 1 : Deadlocks on File Requests <ul><li>If jobs can request and hold files for duration of their execution, deadlock can occur. </li></ul><ul><li>Any other programs that require F1 or F2 are put on hold as long as this situation continues. </li></ul><ul><li>Deadlock remains until a programs is withdrawn or forcibly removed and its file is released. </li></ul>
  6. 6. Case 2 : Deadlocks in Databases <ul><li>1. P1 accesses R1 and locks it. </li></ul><ul><li>2. P2 accesses R2 and locks it. </li></ul><ul><li>3. P1 requests R2, which is locked by P2. </li></ul><ul><li>4. P2 requests R1, which is locked by P1. </li></ul><ul><li>Deadlock can occur if 2 processes access & lock records in database. </li></ul><ul><li>3 different levels of locking : </li></ul><ul><ul><li>entire database for duration of request </li></ul></ul><ul><ul><li>a subsection of the database </li></ul></ul><ul><ul><li>individual record until process is completed. </li></ul></ul><ul><li>If don’t use locks, can lead to a race condition . </li></ul>
  7. 7. Case 3: Deadlocks in Dedicated Device Allocation <ul><li>Deadlock can occur when there is a limited number of dedicated devices. </li></ul><ul><li>E.g., printers, plotters or tape drives. </li></ul><ul><li>1. P1 requests tape drive 1 and gets it. </li></ul><ul><li>2. P2 requests tape drive 2 and gets it. </li></ul><ul><li>3. P1 requests tape drive 2 but is blocked. </li></ul><ul><li>4. P2 requests tape drive 1 but is blocked. </li></ul>
  8. 8. Case 4 : Deadlocks in Multiple Device Allocation <ul><li>Deadlocks can happen when several processes request, and hold on to, dedicated devices while other processes act in a similar manner. </li></ul>
  9. 9. Case 5 : Deadlocks in Spooling <ul><li>Most systems have transformed dedicated devices such as a printer into a sharable device by installing a high-speed device, a disk, between it and the CPU. </li></ul><ul><li>Disk accepts output from several users and acts as a temporary storage area for all output until printer is ready to accept it ( spooling ). </li></ul><ul><li>If printer needs all of a job's output before it will begin printing, but spooling system fills available disk space with only partially completed output, then a deadlock can occur. </li></ul>
  10. 10. Case 6 : Deadlocks in Disk Sharing <ul><li>Disks are designed to be shared, so it’s not uncommon for 2 processes access different areas of same disk. </li></ul><ul><li>Without controls to regulate use of disk drive, competing processes could send conflicting commands and deadlock the system. </li></ul>P1 Read records at cylinder 20 P2 Write to file at cylinder 310 I/O Channel Disk control unit
  11. 11. Case 7: Deadlocks in a Network <ul><li>A network that’s congested (or filled large % of its I/O buffer space) can become deadlocked if it doesn’t have protocols to control flow of messages through network. </li></ul>
  12. 12. Four Conditions for Deadlock <ul><li>Deadlock preceded by simultaneous occurrence of four conditions that operating system could have recognized: </li></ul><ul><li>Mutual exclusion </li></ul><ul><li>Resource holding </li></ul><ul><li>No preemption </li></ul><ul><li>Circular wait </li></ul>
  13. 13. <ul><li>Mutual exclusion -- the act of allowing only one process to have access to a dedicated resource. </li></ul><ul><li>Resource holding -- the act of holding a resource and not releasing it; waiting for the other job to retreat. </li></ul><ul><li>No preemption -- the lack of temporary reallocation of resources; once a job gets a resource it can hold on to it for as long as it needs. </li></ul><ul><li>Circular wait -- each process involved in impasse is waiting for another to voluntarily release the resource so that at least one will be able to continue. </li></ul>
  14. 14. Modeling Deadlocks Using Directed Graphs (Holt, 1972) <ul><li>Processes represented by circles. </li></ul><ul><li>Resources represented by squares. </li></ul><ul><li>Solid line from a resource to a process means that process is holding that resource. </li></ul><ul><li>Solid line from a process to a resource means that process is waiting for that resource. </li></ul><ul><li>Direction of arrow indicates flow. </li></ul><ul><li>If there’s a cycle in the graph then there’s a deadlock involving the processes and the resources in the cycle. </li></ul>
  15. 15. Directed Graph Examples Figure 5.7 (a) Figure 5.7 (b) Figure 5.7 (c) R1 R2 P1 P2 R1 P1 R1 P1
  16. 16. Figure 5.8 Figure 5.9 R1 P1 R1 P1 R3 R2 P3 P2 R2 P2 R3 P3 R1 P1 R2 P2 R3 P3 R1 P1 R2 P2 R3 P3
  17. 17.       Figure 5.11 (a) Figure 5.11 (b) P1 P2 P3 P1 P2 P3
  18. 18. Strategies for Handling Deadlocks <ul><li>Prevent one of the four conditions from occurring. </li></ul><ul><li>Avoid the deadlock if it becomes probable. </li></ul><ul><li>Detect the deadlock when it occurs and recover from it gracefully. </li></ul>
  19. 19. Prevention of Deadlock <ul><li>To prevent a deadlock OS must eliminate 1 out of 4 necessary conditions. </li></ul><ul><ul><li>Same condition can’t be eliminated from every resource. </li></ul></ul><ul><li>Mutual exclusion is necessary in any computer system because some resources (memory, CPU, dedicated devices) must be exclusively allocated to 1 user at a time. </li></ul><ul><ul><li>Might be able to use spooling for some devices. </li></ul></ul><ul><ul><li>May trade 1 type of deadlock (Case 3) for another (Case 5). </li></ul></ul>
  20. 20. Prevention of Resource Holding or No Preemption <ul><li>Resource holding can be avoided by forcing each job to request, at creation time, every resource it will need to run to completion. </li></ul><ul><ul><li>Significantly decreases degree of multiprogramming. </li></ul></ul><ul><ul><li>Peripheral devices would be idle because allocated to a job even though they wouldn't be used all the time. </li></ul></ul><ul><li>No preemption could be bypassed by allowing OS to deallocate resources from jobs. </li></ul><ul><ul><li>OK if state of job can be easily saved and restored. </li></ul></ul><ul><ul><li>Bad if preempt dedicated I/O device or files during modification. </li></ul></ul>
  21. 21. Prevention of Circular Wait <ul><li>Circular wait can be bypassed if OS prevents formation of a circle. </li></ul><ul><li>Havender’s solution (1968) is based on a numbering system for resources such as: printer = 1, disk = 2, tape= 3. </li></ul><ul><ul><li>Forces each job to request its resources in ascending order. </li></ul></ul><ul><ul><li>Any “number one” devices required by job requested first; any “number two” devices requested next … </li></ul></ul><ul><li>Require that jobs anticipate order in which they will request resources. </li></ul><ul><ul><li>A best order is difficult to determine. </li></ul></ul>
  22. 22. Avoidance <ul><li>Even if OS can’t remove 1 conditions for deadlock, it can avoid one if system knows ahead of time sequence of requests associated with each of the active processes. </li></ul><ul><li>Dijkstra’s Bankers Algorithm (1965) used to regulate resources allocation to avoid deadlock. </li></ul><ul><li>Safe state -- if there exists a safe sequence of all processes where they can all get the resources needed. </li></ul><ul><li>Unsafe state -- doesn’t necessarily lead to deadlock, but it does indicate that system is an excellent candidate for one. </li></ul>
  23. 23. Banker’s Algorithm <ul><li>Based on a bank with a fixed amount of capital that operates on the following principles: </li></ul><ul><ul><li>No customer will be granted a loan exceeding bank’s total capital. </li></ul></ul><ul><ul><li>All customers will be given a maximum credit limit when opening an account. </li></ul></ul><ul><ul><li>No customer will be allowed to borrow over the limit. </li></ul></ul><ul><ul><li>The sum of all loans won’t exceed the bank’s total capital. </li></ul></ul><ul><li>OS (bank) must be sure never to satisfy a request that moves it from a safe state to an unsafe one. </li></ul><ul><ul><li>Job with smallest number of remaining resources < = number of available resources </li></ul></ul>
  24. 24. A Bank’s Safe and Unsafe States Safe Unsafe
  25. 25. Problems with Banker’s Algorithm <ul><li>1. As they enter system, jobs must state in advance the maximum number of resources needed. </li></ul><ul><li>2. Number of total resources for each class must remain constant. </li></ul><ul><li>3. Number of jobs must remain fixed. </li></ul><ul><li>4. Overhead cost incurred by running the avoidance algorithm can be quite high. </li></ul><ul><li>5. Resources aren’t well utilized because the algorithm assumes the worst case. </li></ul><ul><li>6. Scheduling suffers as a result of the poor utilization and jobs are kept waiting for resource allocation. </li></ul>
  26. 26. Detection <ul><li>Use directed graphs to show circular wait which indicates a deadlock. </li></ul><ul><li>Algorithm used to detect circularity can be executed whenever it is appropriate. </li></ul>
  27. 27. Reducing Directed Resource Graphs <ul><li>1. Find a process that is currently using a resource and not waiting for one. Remove this process from graph and return resource to “available list.” </li></ul><ul><li>2. Find a process that’s waiting only for resource classes that aren’t fully allocated. </li></ul><ul><ul><li>Process isn’t contributing to deadlock since eventually gets resource it’s waiting for, finish its work, and return resource to “available list.” </li></ul></ul><ul><li>3. Go back to Step 1 and continue the loop until all lines connecting resources to processes have been removed. </li></ul>
  28. 28. R1 R2 R3 R1 R2 R3 R1 R2 R3 Figure 5.12 (a) Figure 5.12 (b) Figure 5.12 (c) Figure 5.12 (d) R1 P1 R2 P2 R3 P3 P1 P2 P3 P1 P2 P3 P1 P2 P3
  29. 29. Recovery <ul><li>Once a deadlock has been detected it must be untangled and system returned to normal as quickly as possible. </li></ul><ul><li>There are several recovery algorithms, all requiring at least one victim , an expendable job, which, when removed from deadlock, frees system. </li></ul><ul><li>1. Terminate every job that’s active in system and restart them from beginning. </li></ul><ul><li>2. Terminate only the jobs involved in deadlock and ask their users to resubmit them. </li></ul>
  30. 30. Recovery Algorithms - 2 <ul><li>3. Terminate jobs involved in deadlock one at a time, checking to see if deadlock is eliminated after each removal, until it has been resolved. </li></ul><ul><li>4. Have job keep record (snapshot) of its progress so it can be interrupted and then continued without starting again from the beginning of its execution. </li></ul><ul><li>5. Select a non-deadlocked job, preempt resources it’s holding, and allocate them to a deadlocked process so it can resume execution, thus breaking the deadlock </li></ul><ul><li>6. Stop new jobs from entering system, which allows non-deadlocked jobs to run to completion so they’ll release their resources (no victim). </li></ul>
  31. 31. Select Victim with Least-Negative Effect <ul><li>Priority of job under consideration—high-priority jobs are usually untouched. </li></ul><ul><li>CPU time used by job—jobs close to completion are usually left alone. </li></ul><ul><li>Number of other jobs that would be affected if this job were selected as the victim. </li></ul><ul><li>Programs working with databases deserve special treatment. </li></ul>
  32. 32. Starvation <ul><li>Starvation -- result of conservative allocation of resources where a single job is prevented from execution because it’s kept waiting for resources that never become available. </li></ul><ul><li>“ The dining philosophers” Dijkstra (1968). </li></ul><ul><li>Avoid starvation via algorithm designed to detect starving jobs which tracks how long each job has been waiting for resources ( aging ). </li></ul>
  33. 33. Terminology <ul><li>avoidance </li></ul><ul><li>circular wait </li></ul><ul><li>deadlock </li></ul><ul><li>deadly embrace </li></ul><ul><li>detection </li></ul><ul><li>directed graphs </li></ul><ul><li>locking </li></ul><ul><li>mutual exclusion </li></ul><ul><li>no preemption </li></ul><ul><li>prevention </li></ul><ul><li>process synchronization </li></ul><ul><li>race </li></ul><ul><li>resource holding </li></ul><ul><li>safe state </li></ul><ul><li>spooling </li></ul><ul><li>starvation </li></ul><ul><li>unsafe state </li></ul><ul><li>victim </li></ul>

×