Docsity

Resource Management and Deadlock Handling in Operating Systems, Lecture notes of Operating Systems

Toggle sidebar
bg1
Classification of Resources
•Sharable resources. Can be used by several users at the
same time.
→Program code (if reentrant)
→Data areas if read only.
•Non-sharable (exclusive) resources. Can only be used
by one user at a time.
→Data areas that need to be written.
→Most external devices like printers.
→The processor.
1
Non-sharable Resources
•preemtable. Use of resource may be preempted and
restarted later on.
→The processor
→The primary memory
•non-preemptable
→Printers. (A new document cannot be printed until the
previous is completed.)
2
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Resource Management and Deadlock Handling in Operating Systems and more Lecture notes Operating Systems in PDF only on Docsity!

Classification of Resources

•^

Sharable resources.

Can be used by several users at the

same time. Õ

Program code (if reentrant) Õ

Data areas if read only.

-^

Non-sharable (exclusive) resources.

Can only be used

by one user at a time. Õ

Data areas that need to be written. Õ

Most external devices like printers. Õ

The processor.

1

Non-sharable Resources

•^

preemtable.

Use of resource may be preempted and

restarted later on. Õ

The processor Õ

The primary memory

-^

non-preemptable Õ

Printers. (A new document cannot be printed until theprevious is completed.)

Problems with Resource Administration

Deadlock

  • The processes may block each other. Nobody

can use the resource.

Starvation

  • Some process is prevented from using a

resource because there is always another (higher priority)process that is using it. Deadlock can only occur if the processes need more than 1resource at the same time.Starvation is possible in all resource allocation situationsunless a suitable policy is used to prevent it.Starvation can be prevented by assigning the resources instrict FIFO order or by increasing the priority for a processthat have been waiting for a long time.

3

Deadlock

Deadlock may arise if the following four conditions holdsimultaneously:1.

Mutual exclusion.

Non-sharable resources are used.

Hold and wait.

A process holds at least one resource and

requests another resource that is held by another process.

No preemption.

Resources that a process holds, can

only be voluntarily released by the process itself.

Circular wait.

A circular chain of processes exist, in

which each process holds a resource requested by thenext process in the chain.

Deadlock Prevention

Hold and Wait. This condition can be prevented by requiring the processes torequest all resources at the same occasion.A variant is that a process that want to allocate additionalresources first must release all resources that it already holdsand then request both the old and the new resources at thesame time.Disadvantages:^ •

May lead to poor resource utilization. • May create starvation.

7

Deadlock Prevention

No preemption. The third condition can be prevented with the followingmethod:If a process holds a resource and requests a new resourcethat cannot be immediately allocated, then all resourcescurrently being held are preempted. The Process is restartedfirst then both the new and the old resources are available.This method is often applied to resources whose state can besaved and restored as for example CPU registers and primarymemory. It cannot be used for nonpreemptable resources.

Deadlock Prevention

Circular Wait. One way to ensure that this condition never holds is toimpose a total ordering on the resources. Every resource isgiven a unique number. The resources must then berequested in an increasing order of enumerationIn order to get good resource utilization, the resources shouldbe numbered in the same order that they are normallyneeded.

9

Deadlock Avoidance

•^

If all four conditions for deadlock is met, deadlock can stillbe avoided by using additional information about howresources are to be requested.

-^

The most common method is that each process mustdeclare the maximum number of resources of each typethat it may need.

-^

The processes may demand new resources in an arbitraryorder.

-^

The resource allocator uses a deadlock avoidancealgorithm to evaluate each new resource application andgrants the allocation only if it leaves the system in a

safe

state

•^

The system is in a

safe state

, if the resource allocator can

guarantee that all processes will be completed.

Banker’s Algorithm

Not all unsafe states are deadlocks, but an unsafe state maylead to deadlock. As long as the state is safe the system canavoid unsafe states (and deadlocks).Banker’s algorithm is the most well-known deadlockavoidance algorithm.^ •

The processes may demand new resources in arbitraryorder but must declare the maximum number of eachresource type that they may need. • The system grants a new application only if it results in asafe state. • Otherwise, the process must wait until some otherprocess releases sufficiently many resources. Problems with banker’s algorithm:^ •

There must be a fixed number of resources to allocate.This may be difficult to meet since external resources maybreak down and become unavailable. • The algorithm requires that the processes state theirmaximum resource needs. This is not always known.

13

Deadlock Detection

An alternative to preventing deadlock can be to

detect

deadlock and then use some recovery method. Deadlock canbe detected with the aid of resource-allocation graphs.If there only exists one resource of each type, deadlock hasoccurred if there exists a loop in the resource-allocationgraph. Reduction of resource-allocation graphs^ •

If the exists several resources of each type, a loop in thegraph need not mean deadlock. • In this case, the method with reduction of the graph canbe used. • If the resource requirements for a certain process can bemet, we say that the graph can be reduced with thatprocess. The reduced graph is the graph with this processremoved. • If the graph can be reduced with all its processes there isno deadlock. If certain processes cannot be reduced, adeadlock exists and the the remaining loop in the graphconstitutes the deadlocked processes.

Detection Algorithm Usage

Although it is relatively simple to detect a deadlock by lookingat the graph, algorithms for deadlock detection arecomputationally expensive.An algorithm for detection of a loop in a graph has complexityO(

n

2 ), there n is number of nodes in the graph. When should we invoke the detection algorithm?^ •

Deadlock may occur each time a process makes aresource request that cannot be granted immediately. • In principle, one should therefore run the detectionalgorithm at each such request. • Due to the algorithm’s high complexity this will use tomuch CPU time, if there are many processes. • Therefore, a compromise is needed. How often to run thealgorithm is dependent on the judged risk for deadlock anthe consequence if deadlock do occur.

15

Deadlock Recovery

Unfortunately, no good method exists for deadlock recoveryThe following methods are possible Recovery through killing processes.^ •

Kill one or more processes until the deadlock cycle isbroken. • The killed processes lose all calculated results and haveto be restarted. • Processes that leave permanent changes in the filesystem, may give incorrect result if they are restarted. Recovery through preemption.^ •

With preemtable resources, one can temporary takeresources away from a process and give them to anotherprocess, in order to resolve a deadlock. • With non-preemptable resources this is equivalent tokilling the process. Checkpointing^ •

If check points are used, only part of the process must bererun if it is killed. • This must be programmed into the processes.