0% found this document useful (0 votes)
22 views

OSC05

Uploaded by

bipruu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

OSC05

Uploaded by

bipruu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Deadlocks

Short Answer Type Questions (2 Marks)


1. What is deadlock?

A: Dead-Lock is a situation where two or more processors are waiting for some event to
happen, but such events that don't happen is a deadlock condition, and the processors are
said to be in a deadlock state.

2. What are goals of system protection?

A: The goals of system protection are:

•Confidentiality: Keeping sensitive data private and accessible only to authorized users.

•Integrity: Ensuring data and systems are accurate and not tampered with.

•Availability: Ensuring systems and data are accessible when needed.

•Authentication: Verifying the identity of users or systems.

•Authorization: Controlling what users or systems can do based on permissions.

3. What are the necessary conditions for deadlock?

A: The necessary conditions for deadlock are:-

•Mutual Exclusion

•Hold and wait

•No Pre-emption

• The circular wait

4. Give the characteristics of a deadlock situation.

A: •Mutual Exclusion: According to this condition, atleast one resource should be non-
shareable (non-shareable resources are those that can be used by one process at a time.)

•Hold and wait: According to this condition, A process is holding atleast one resource and
is waiting for additional resources.

•NO pre-emption: Resources cannot be taken from the process because resources can be
released only voluntarily by the process holding them.

•Circular wait: In this condition, the set of processes are waiting for each other in the
circular form. the above four conditions are not completely independent as the circular
wait condition implies the hold and wait condition. We emphasize on the fact that all four
conditions must hold for a deadlock.

5. Explain the resource allocation graph with an example.

A: A Resource Allocation Graph (RAG) is a graphical representation used in computer


science to model the allocation of resources to processes in a system. It helps in detecting
potential deadlock situations.

Structure:

(i)Nodes: There are two types of nodes: -Process Nodes (P): Represent processes.

(ii)Resource Nodes (R): Represent resources.

(iii)Edges:

•Request Edge: A directed edge from a process node to a resource node, indicating that
the process is requesting that resource.

•Assignment Edge: A directed edge from a resource node to a process node, indicating
that the resource has been allocated to the process.

Example: Consider two processes (P1, P2) and two resources (R1, R2).

•P1 requests R1.

•P2 requests R2.

•R1 is allocated to P1, and R2 is allocated to P2.

The Resource Allocation Graph will look like this:

•A request edge from P1 to R1.

•A request edge from P2 to R2.

• An assignment edge from R1 to P1.

• An assignment edge from R2 to P2.

If a process is holding a resource and requests another, or if multiple processes are


requesting the same resource, a cycle in the graph can indicate a deadlock.

6. What are different methods for handling deadlocks?

A: Ignoring the issue, Deadlock Detection and Recovery, Deadlock Avoidance, Deadlock
Prevention
7. Explain any two methods of deadlock prevention.

A: •Elimination of Mutual Exclusion Condition

•Elimination of Hold and wait Condition

•Elimination of No Pre-emption Condition

•Elimination of The circular wait Condition

8. What is safe state?

A: A state is safe if the system can allocate resources to each process (up to its maximum
requirement) in some order and still avoid a deadlock. Formally, a system is in a safe state
only, if there exists a safe sequence. So a safe state is not a deadlocked state and
conversely a deadlocked state is an unsafe state.

9. Explain Bankers Algorithm

A: The Banker's Algorithm is a resource allocation and deadlock avoidance algorithm used
in operating systems. It helps determine whether a system can safely allocate resources to
processes without leading to a deadlock. It checks if there is enough available resources to
satisfy the maximum future demand of all processes. If so, the system is considered in a
"safe state."

10. Explain deadlock detection

A: Deadlock detection is the process of identifying when a system has entered a state
where two or more processes are unable to proceed because each is waiting for the other
to release resources. In simple terms, it's finding out when processes get "stuck" because
they are waiting on each other indefinitely.

11. Explain the wait-for graph with an example

A: A wait-for graph is a diagram used to detect deadlocks. In this graph, each process is
represented as a node, and a directed edge from one process to another indicates that the
first process is waiting for the second to release a resource. Example:-

•Process P1 is waiting for resource R1, and Process P2 is waiting for resource R2.

•If P1 is holding R2 and P2 is holding R1, tthen

•There will be an edge from P1 to P2 (P1 is waiting for P2).

•There will be an edge from P2 to P1 (P2 is waiting for P1).

This forms a cycle in the graph, which indicates a deadlock.


12. Briefly explain dead lock recovery

A: When a deadlock has been detected in the system by deadlock detection algorithms,
then it has to be recovered by using some recovery mechanism. The brute force approach
is to reboot the computer, but it is inefficient because it may lose complete data and waste
computing time. Hence, other techniques are used to recover from deadlock. They are
broadly classified into two types. They are, Process Termination and Resource Preemption.

13. List the methods in the resource pre-emption.

A:

Long Answer Type Questions (4 Marks & 8 Marks)


1. Explain the methods of handling deadlock.

A: Deadlock detection, deadlock prevention and deadlock avoidance are the main
methods for handling deadlocks. Details about these are given as follows –

(i) Ignoring the Issue: It might be preferable to ignore the issue if the risk of a deadlock is
sufficiently low and the cost of avoiding one is sufficiently high. For instance, if a deadlock
occurs on a PC once every 100 years, a reboot may be less painful than the limitations
required to stop it.

(ii) Deadlock Detection and Recovery: Deadlock can be detected by the resource
scheduler as it keeps track of all the resources that are allocated to different processes.
After a deadlock is detected, it can be handled using recovery methods –

•All the processes that are involved in the deadlock are terminated. This approach is not
that useful as all the progress made by the processes is destroyed.

•Resources can be pre-empted from some processes and given to others until the
deadlock situation is resolved.

(iii) Deadlock Avoidance: Avoid deadlock by careful resource scheduling

(iv)Deadlock Prevention: It is important to prevent a deadlock before it can occur. So, the
system checks each transaction before it is executed to make sure it does not lead to
deadlock. If there is even a slight possibility that a transaction may lead to deadlock, it is
never allowed to execute.
2. Explain deadlock prevention.

A: (i) Elimination of “Mutual Exclusion” Condition: The "mutual exclusion" condition


means that some resources, like printers or tape drives, can’t be shared by multiple
processes at the same time. While some resources (like read-only files) can be accessed
simultaneously by multiple processes without issue, non-sharable resources require
exclusive access, which makes them prone to deadlock. Preventing mutual exclusion won't
always stop deadlocks because some resources must be used by only one process at a
time.

(ii) Elimination of "Hold and Wait" Condition: There are two possibilities for elimination of
the hold and wait condition.

•The first alternative is that a process request be granted all of the resources it needs at
once, prior to execution.

•The second alternative is to disallow a process from requesting resources whenever it has
previously allocated resources

This strategy requires a process to request all the resources it needs at once. If all
resources aren’t available, the process must wait until they are, without holding any
resources in the meantime. This prevents deadlocks because no process is in a “wait for”
state. However, it can waste resources, as processes may have to wait for resources they
don’t need immediately. This can lead to delays (starvation) if not all resources are
available at the same time.

(iii) Elimination of “No-pre-emption” Condition: The non-preemption condition can be


solved by forcing a process to give up its resources if it can't immediately get more. This
allows other processes to use those resources. However, when a process holds resources
but can't get more, it can cause deadlock, where processes are stuck waiting for each
other. In this strategy, if a process is denied additional resources, it must release what it's
holding and request everything it needs again. While this prevents deadlock, it can lead to
starvation, where a process is repeatedly blocked from getting the resources it needs and
may never proceed.

(iv) Elimination of “Circular Wait” Condition: To prevent circular wait, resources are given
a strict order, and processes must request them in that order. For example, if a process
holds resource R5, it can only request resources numbered higher than R5 next. This rule
ensures that there are no cycles in the resource allocation, preventing deadlock.

3. Explain deadlock avoidance with an example

A: Deadlock avoidance is a strategy where the system ensures that deadlock conditions
are never allowed to occur by carefully controlling how resources are allocated to
processes. It requires that, before granting a resource request, the system checks if the
allocation would lead to a potential deadlock. If it does, the request is denied, and the
process must wait until the system can safely allocate resources without causing a
deadlock.

Key Concepts of Deadlock Avoidance:

(i) Safe State: The system is in a "safe state" if there is at least one sequence of processes
that can complete without causing deadlock. If a process requests resources, the system
checks if allocating those resources would leave the system in a safe or unsafe state.

(ii) Resource Allocation Decision: When a process requests resources, the system
checks if granting that request would still allow for a safe sequence of execution. If yes, the
request is granted; if no, the process must wait.

Example of Deadlock Avoidance: Consider a system with two types of resources: R1 (e.g.,
printers) and R2 (e.g., tape drives), and two processes: P1 and P2.

•Available resources: 2 printers (R1) and 3 tape drives (R2).

•P1 needs 1 printer and 2 tape drives to finish.

• P2 needs 1 printer and 1 tape drive to finish.

Step-by-Step Process:

(i) Initial Allocation: Suppose P1 is given 1 printer and 1 tape drive, and P2 is given 1 printer
and 1 tape drive. Now:

•Available resources: 1 printer and 1 tape drive.


(ii) Request: P1 then requests 1 more tape drive. Before granting this request, the system
checks if it can safely allocate the tape drive.

(iii) Safety Check:

•If the request is granted, P1 will hold 1 printer and 2 tape drives, leaving P2 with 1 printer
and 1 tape drive.

•P2 needs 1 printer and 1 tape drive to finish. If P1 holds the remaining tape drive, P2
cannot proceed. Therefore, P1's request would lead to an unsafe state and a potential
deadlock.

(iv) Decision: Since granting P1's request would cause a deadlock, the system denies the
request. P1 must wait until resources are freed up by other processes.

(v) Safe Allocation: If P1 had instead requested resources in a way that would leave P2
with enough resources to finish first, the system could have granted the request without
causing deadlock.

4. Explain Banker's algorithm.

A: • For resource categories that contain more than one instance the resource-allocation
graph method does not work, and more complex ( and less efficient ) methods must be
chosen.

• The Banker's Algorithm gets its name because it is a method that bankers could use to
assure that when they lend out resources they will still be able to satisfy all their clients. ( A
banker won't loan out a little money to start building a house unless they are assured that
they will later be able to loan out the rest of the money to finish the house. )

• When a process starts up, it must state in advance the maximum allocation of resources
it may request, up to the amount available on the system.

• When a request is made, the scheduler determines whether granting the request would
leave the system in a safe state. If not, then the process must wait until the request can be
granted safely.

• The banker's algorithm relies on several key data structures: ( where n is the number of
processes and m is the number of resource categories. )

>Available[ m ] indicates how many resources are currently available of each type.

>Max[ n ][ m ] indicates the maximum demand of each process of each resource.


>Allocation[ n ][ m ] indicates the number of each resource category allocated to each
process.

>Need[ n ][ m ] indicates the remaining resources needed of each type for each

process. ( Note that Need[ i ][ j ] = Max[ i ][ j ] - Allocation[ i ][ j ] for all i, j. )

•For simplification of discussions, we make the following notations / observations:

>One row of the Need vector, Need[ i ], can be treated as a vector corresponding to

the needs of process i, and similarly for Allocation and Max.

>A vector X is considered to be <= a vector Y if X[ i ] <= Y[ i ] for all i.

5. Explain the deadlock detection algorithm for single instance resource type.

A: Deadlock detection is the process of identifying when a system has entered a state
where two or more processes are unable to proceed because each is waiting for the other
to release resources. In simple terms, it's finding out when processes get "stuck" because
they are waiting on each other indefinitely.

Single Instance of Each Resource Type: If each resource has only one instance, a
deadlock detection algorithm can use a wait-for graph. This graph is created by removing
the resource nodes from the resource-allocation graph and collapsing the edges between
processes. In a wait-for graph, an edge from process P1 to process P2 means that P1 is
waiting for P2 to release a resource. A cycle in this graph indicates a deadlock. To detect
deadlock, the system periodically checks the wait-for graph for cycles.

In the above diagram, resource 1 and resource 2 have single instances. Is Confirmed.

There is a cycle R1->P1->R2->P2. So, Deadlock is Confirmed. The algorithm that is used to
detect the cycle in the graph mainly requires n² operations; where n indicates the number
of vertices in the graph.
6. Explain the deadlock detection algorithm for multiple instance resource type.

A: The wait-for graph can't be used for a resource allocation system containing multiple
instances of each resource type. Hence, a different algorithm is employed which carries
certain data structures.

The data structures in the algorithm are,

(i) Available: It is an array of length m. It represents the number of available resources of


each type.

(ii) Allocation: It is an n x m matrix which represents the number of resources of each type
currently allocated to each process.

(iii) Request: It is an n*m matrix that is used to indicate the request of each process; if
Request[i] [j] equals to k,then process P, is requesting k more instances of resource type Ri.

Allocation and Request are taken as vectors and referred to as Allocation and Request. The
Given detection algorithm is simply used to investigate every possible allocation sequence
for the processes that remain to be completed.

Step1: Let Work and Finish be vectors of length m and n, respectively. Initialize:

Work = Available

Finish[i]=false for I = 0, 1,…, n-1.

If Allocationi is not equal to 0,then Finish[i] = false; else Finish[i] = true

Step 2: Find an index I such that both

Finish[i] ==false

Requesti <= Work

If no such I exist then go to step 4.

Step 3: Perform the following:

Work =Work + Allocationi

Finish[i] = true

Go to step 2.

Step 4: If Finish[i] == false for some I, 0<=i<n, then it means the system is in a deadlocked
state. Moreover, if Finish[i]==false, then process P, is deadlocked.
This algorithm may require an order of mxn² operations in order to determine whether the
system is in a deadlocked state.

7. What is a deadlock? Explain deadlock detection and deadlock recovery method.

A: Dead-Lock is a situation where two or more processors are waiting for some event to
happen, but such events that don't happen is a deadlock condition, and the processors are
said to be in a deadlock state.

For Deadlock detection refer 5th,6th question

Deadlock recovery method: Once deadlock is detected, recovery is necessary. While


rebooting the system can be a brute force solution, it’s inefficient as it may result in data
loss and wasted time. Instead, two main recovery techniques are used:

(i) Process Termination: To recover from deadlock, processes can be terminated:

•Abort All Deadlocked Processes: All deadlocked processes are terminated at once,
breaking the deadlock quickly, but it's costly as it wastes CPU time of long-running
processes.

•Abort One Process at a Time: One process is terminated at a time until the deadlock is
resolved. This method is slower due to the overhead of repeatedly checking for deadlock
after each termination.

(ii) Resource Preemption: In resource pre-emption, resources are taken from some
processes and given to others to resolve deadlock. The key issues are:

•Selection of Victim Process: Decide which process to pre-empt based on factors like
resource usage and CPU time.

•Rollback: Pre-empted processes must be rolled back to a previous state since they can't
continue normally.

•Starvation: Ensure that no process is repeatedly pre-empted and starved of resources.

8. Briefly explain deadlock recovery.

A: For Deadlock detection refer 7 th question

9. Write the resource allocation algorithm for dead lock.

A: • If resource categories have only single instances of their resources, then deadlock
states can be detected by cycles in the resource-allocation graphs.
• In this case, unsafe states can be recognized and avoided by augmenting the resource
allocation graph with claim edges, noted by dashed lines, which point from a process to a
resource that it may request in the future.

• In order for this technique to work, all claim edges must be added to the graph for any
particular process before that process is allowed to request any resources. ( Alternatively,
processes may only make requests for resources for which they have already established
claim edges, and claim edges cannot be added to any process that is currently holding
resources. )

• When a process makes a request, the claim edge Pi->Rj is converted to a request edge.
Similarly when a resource is released, the assignment reverts back to a claim edge.

• This approach works by denying requests that would produce cycles in the resource
allocation graph, taking claim edges into effect.

• Consider for example what happens when process P2 requests resource R2:

• The resulting resource-allocation graph would have a cycle in it, and so the request
cannot be granted.

10. Explain about necessary conditions of deadlock.

A: A deadlock happens in operating system when two or more processes need some
resource to complete their execution that is held by the other process. A deadlock occurs if
the four Coffman conditions hold true. But these conditions are not mutually exclusive.
They are given as follows:
(i) Mutual Exclusion: At least one resource must be non-sharable, meaning only one
process can use it at a time. If another process needs that resource, it must wait until it's
released. For example, if Resource 1 is held by Process 1, no other process can use it until
Process 1 releases it.

(ii) Hold and Wait: A process holds at least one resource and is waiting for more resources
that are held by other processes. For example, Process 2 holds Resource 2 and Resource 3
and is waiting for Resource 1, which is held by Process 1.

(iii) No Pre-emption: Resources cannot be forcibly taken from a process. A resource is only
released when the process holding it finishes using it voluntarily. For example, Process 2
cannot take Resource 1 from Process 1; Resource 1 will only be released when Process 1
finishes using it.

(iv) Circular Wait: A circular chain of processes exists, where each process is waiting for a
resource held by the next process in the chain. For example, Process P0 is waiting for a
resource held by P1, P1 is waiting for P2, and so on, until Pn-1 is waiting for a resource held
by P0, forming a cycle.

For example: Process 1 is allocated Resource 2 and it is requesting Resource 1. Similarly,


Process 2 is allocated Resource 1 and it is requesting Resource 2. This forms a circular wait
loop as shown in diagram below.
11. Explain about resource allocation graph(RAG).

A: Refer 9th question

You might also like