Operating Systems Lecture 27 Syed Mansoor Sarwar Copyright

  • Slides: 26
Download presentation
Operating Systems Lecture 27 Syed Mansoor Sarwar © Copyright Virtual University of Pakistan

Operating Systems Lecture 27 Syed Mansoor Sarwar © Copyright Virtual University of Pakistan

Agenda for Today n Review of previous lecture n Deadlock handling n Deadlock prevention

Agenda for Today n Review of previous lecture n Deadlock handling n Deadlock prevention n Deadlock avoidance n Recap of lecture 2/23/2021 © Copyright Virtual University of Pakistan

Review of Lecture 26 § Dining philosophers problem with monitor § Deadlocks in computers

Review of Lecture 26 § Dining philosophers problem with monitor § Deadlocks in computers § Resource allocation graph § Deadlock characterization 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Handling § Ensure that the system will never enter a deadlock state. §

Deadlock Handling § Ensure that the system will never enter a deadlock state. § Allow the system to enter a deadlock state and then recover. § Ignore the problem and pretend that deadlocks never occur in the system. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Handling § Deadlock prevention § Deadlock avoidance § Deadlock detection and recovery 2/23/2021

Deadlock Handling § Deadlock prevention § Deadlock avoidance § Deadlock detection and recovery 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Prevention § Restrain the ways resource allocation requests can be made to insure

Deadlock Prevention § Restrain the ways resource allocation requests can be made to insure that at least one of the four necessary conditions is violated. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Prevention § Mutual Exclusion Cannot be prevented for all resources. Some resources are

Deadlock Prevention § Mutual Exclusion Cannot be prevented for all resources. Some resources are inherently non-sharable because their states cannot be saved and restored without ill effects, such as a printer. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Prevention § Hold and Wait – we must guarantee that whenever a process

Deadlock Prevention § Hold and Wait – we must guarantee that whenever a process requests a resource, it does not hold any other resources. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Prevention § Require a process to request and be allocated all its resources

Deadlock Prevention § Require a process to request and be allocated all its resources before it begins execution, or allow a process to request resources only when the process has none. § Low resource utilization; starvation possible. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Prevention § No Preemption If a process that is holding some resources requests

Deadlock Prevention § No Preemption If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Prevention § Preempted resources are added to the list of resources for which

Deadlock Prevention § Preempted resources are added to the list of resources for which the process is waiting for. § Process will be restarted only when it can regain its old resources as well as the new ones that it has requested. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Prevention § Circular Wait – impose a total ordering of all resource types,

Deadlock Prevention § Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Prevention § We assign a unique number to each resource type by using

Deadlock Prevention § We assign a unique number to each resource type by using function F: R → N and make sure that processes request resources in an increasing order of enumeration. § For example, tape drive = 1, disk drive = 5, and printer = 12. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Prevention § Proof Let’s assume that there is a cycle P 0 →

Deadlock Prevention § Proof Let’s assume that there is a cycle P 0 → P 1 → P 2 → … → Pk → P 0 R 1 R 2 Rk R 0 F(R 0) < F(R 1) < … F(Rk) < F(R 0), which is impossible There can be no circular wait. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Avoidance § Requires that the system has additional a priori information available about

Deadlock Avoidance § Requires that the system has additional a priori information available about the use of resources by processes. § Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. 2/23/2021 © Copyright Virtual University of Pakistan

Deadlock Avoidance § The deadlock-avoidance algorithm dynamically examines the resource -allocation state to ensure

Deadlock Avoidance § The deadlock-avoidance algorithm dynamically examines the resource -allocation state to ensure that there can never be a circular-wait condition. § Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. 2/23/2021 © Copyright Virtual University of Pakistan

Safe State § When a process requests an available resource, system must decide if

Safe State § When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. § System is in a safe state if there exists a safe sequence of all processes. 2/23/2021 © Copyright Virtual University of Pakistan

Safe State § Sequence <P 1, P 2, …, Pn> is safe if for

Safe State § Sequence <P 1, P 2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by the currently available resources, plus the resources held by all the Pj, with j<i. In other words, a safe sequence specifies the order in which processes can be finished. 2/23/2021 © Copyright Virtual University of Pakistan

Safe State § If Pi resource needs are not immediately available, then Pi can

Safe State § If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. § When Pj are finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. § When Pi terminates, Pi+1 can obtain its needed resources, and so on. 2/23/2021 © Copyright Virtual University of Pakistan

Basic Facts § If a system is in safe state no deadlocks. § If

Basic Facts § If a system is in safe state no deadlocks. § If a system is in unsafe state possibility of deadlock due to the behavior of processes. § Avoidance ensure that a system never enters an unsafe state. 2/23/2021 © Copyright Virtual University of Pakistan

Safe, Unsafe, and Deadlock States 2/23/2021 © Copyright Virtual University of Pakistan

Safe, Unsafe, and Deadlock States 2/23/2021 © Copyright Virtual University of Pakistan

Example § System with 12 tape drives and three processes § Current system state:

Example § System with 12 tape drives and three processes § Current system state: Process Max Need Allocated P 0 10 5 P 1 4 2 P 2 9 2 2/23/2021 © Copyright Virtual University of Pakistan

Example § System is in a safe state with the safe sequence <P 1,

Example § System is in a safe state with the safe sequence <P 1, P 0, P 2> § P 2 requests and is allocated one more tape drive. Process Max Need Allocated P 0 10 5 P 1 4 2 P 2 9 2 2/23/2021 © Copyright Virtual University of Pakistan

Example § Assuming the tape drive is allocated to P 2, the new system

Example § Assuming the tape drive is allocated to P 2, the new system state will be: Process Max Need Allocated P 0 10 5 P 1 4 2 P 2 9 3 § System gets into an unsafe state. 2/23/2021 © Copyright Virtual University of Pakistan

Recap of Lecture Deadlock handling § Deadlock prevention § Deadlock avoidance § 2/23/2021 ©

Recap of Lecture Deadlock handling § Deadlock prevention § Deadlock avoidance § 2/23/2021 © Copyright Virtual University of Pakistan

Operating Systems Lecture 27 Syed Mansoor Sarwar © Copyright Virtual University of Pakistan

Operating Systems Lecture 27 Syed Mansoor Sarwar © Copyright Virtual University of Pakistan