OS Chapter 7 Simple Notes_072134
OS Chapter 7 Simple Notes_072134
1. System Model
In an operating system, processes interact with various resources (like memory, CPU, I/O
devices). A deadlock happens when a set of processes are blocked because each process is
waiting for a resource that another process in the set is holding. Deadlocks are caused by
limitations in resources, imperfect inter-process communication, and restrictions on how
processes can access resources.
2. What is Deadlock?
Deadlock is a situation in which two or more processes cannot proceed because each is waiting
for resources held by the others. Essentially, the system reaches a state where no process can
continue its execution because of the waiting.
Example:
Consider two tape drives and two processes, P1 and P2. If P1 holds one tape drive and
needs the other, and P2 holds the second one and also needs the first, neither process can
complete. This is a classic example of deadlock.
3. Deadlock Characteristics
1. Mutual Exclusion: Only one process can use a resource at a time (e.g., a printer).
2. Hold and Wait: A process holding one resource is waiting for another resource that is
currently held by another process.
3. No Preemption: Resources cannot be forcibly taken from a process once it holds them.
4. Circular Wait: A circular chain of processes exists, where each process is waiting for a
resource held by the next process in the chain.
4. Types of Resources
Reusable Resources: These can be used by one process at a time but can be reused after
being released (e.g., CPU, memory).
Consumable Resources: These are used and then destroyed by processes (e.g.,
messages, signals).
In a deadlock scenario, processes typically compete for reusable resources.
In a system, there are multiple resources and processes that request, hold, and release them. The
system’s state is defined by the resources available and the processes holding or requesting
resources.
A cycle in the RAG suggests a potential deadlock, but a cycle alone isn't enough to confirm
deadlock—it depends on whether there is one instance or multiple instances of each resource
type.
1. Deadlock Prevention: Ensure that the system never enters a deadlock state by
disallowing one of the four deadlock conditions.
2. Deadlock Avoidance: Carefully allocate resources in a way that avoids situations where
deadlock could occur.
3. Deadlock Detection: Allow deadlocks to occur but periodically check for them and take
action to resolve the deadlock.
9. Deadlock Prevention
Deadlock prevention works by ensuring that at least one of the four conditions for deadlock is
not met. There are two ways to do this:
Mutual Exclusion: This condition cannot be disallowed because certain resources like
printers must be used by only one process at a time.
Hold and Wait: This can be prevented by ensuring that processes request all the
resources they will need before they begin execution.
No Preemption: If a process holding some resources requests another resource, the
system can preempt the resources from the process and give them to others.
Circular Wait: This can be prevented by imposing a strict ordering of resource requests,
so a process can only request resources in a predefined order.
Deadlock avoidance involves dynamically checking resource allocation to ensure that a system
does not enter an unsafe state where deadlock might occur. There are two approaches to avoid
deadlocks:
A safe state is one where a process can always finish by requesting resources in a way that
avoids deadlock.
The Banker's Algorithm is one method of deadlock avoidance. It works by checking if the
system can allocate resources without leading to a deadlock. The algorithm ensures that at any
point, there is a sequence of processes that can run to completion.
In deadlock detection, the system is allowed to enter a deadlock state, but a detection algorithm
is periodically invoked to check if deadlock has occurred. This algorithm identifies the cycles in
the system that indicate a deadlock.
If there is only one instance of a resource type, a wait-for graph can be used to track which
processes are waiting for which others. A cycle in the graph indicates a deadlock.
Once a deadlock is detected, the system must recover. There are two main strategies:
1. Process Termination: One or more processes involved in the deadlock are aborted. The
decision on which process to abort can be based on factors such as priority, resource
usage, or completion time.
2. Resource Preemption: Resources are preempted from processes involved in the
deadlock. This may involve rolling back processes to an earlier safe state.
Recovery might also involve starvation, where the same process may always be chosen as the
victim and never get a chance to proceed.
The Dining Philosophers Problem is a classic example used to illustrate the issues of deadlock,
starvation, and resource allocation. Five philosophers sit around a table, each with a plate of food
and a chopstick on each side. Each philosopher needs both chopsticks to eat. If all philosophers
pick up the left chopstick simultaneously, they will each wait forever for the right chopstick,
creating a deadlock.
Conclusion
Deadlocks are a significant problem in operating systems where resources are shared by
processes. By using techniques like deadlock prevention, avoidance, and detection, operating
systems ensure that processes can access resources efficiently without being stuck in deadlock
situations. Understanding the system model, resource allocation, and methods for handling
deadlocks is crucial for designing robust and efficient operating systems.