Sub topic 1
The Resource Model: Processes, Resources, and Instances
Prerequisites
This chapter assumes you know these topics:
- Processes and threads, and the idea of a critical section.
- Mutual exclusion with locks or semaphores.
- Directed graphs, including nodes, edges, and cycles.
Processes and resources
A process is a running program. A resource is anything a process needs to do its work, such as a printer, a file, or a region of memory. Some resources can be shared, but many must be used by one process at a time.
A resource type is a category of resource, such as "printer." Each type can have one or more instances. A system with three identical printers has one resource type with three instances.
A process uses a resource in three steps: it requests the resource, uses it, and then releases it. If the requested resource is busy, the process waits.
What a deadlock is, and what it is not
A deadlock happens when a set of processes each wait for a resource that another process in the set holds. None of them can continue, so they wait forever.
Two similar problems are different:
- Starvation means one process waits indefinitely while others keep running. The system still makes progress.
- Livelock means processes keep changing state in response to each other but never finish. They are busy, but they make no progress.
Deadlocked processes are blocked, not busy.
The four necessary conditions
A deadlock can occur only if all four Coffman conditions hold at the same time:
| Condition | Meaning |
|---|---|
| Mutual exclusion | At least one resource can be used by only one process at a time. |
| Hold and wait | A process holds one resource while it waits for another. |
| No preemption | A resource can be released only by the process holding it. |
| Circular wait | A chain of processes exists in which each waits for the next. |
If any one condition is absent, deadlock cannot happen.
Resource-allocation graphs: when a cycle means deadlock
A resource-allocation graph draws processes as circles and resources as boxes. An edge from a process to a resource is a request. An edge from a resource to a process is an assignment.
For resources with a single instance, a cycle means deadlock. For resources with several instances, a cycle is necessary but not enough. A cycle may exist while another process can still release a resource and break it.
> Note: this is a common GATE trap. Check the instance count before you conclude anything from a cycle.
Four ways to handle deadlock
- Ignore it. Most general-purpose operating systems take this approach and restart a system that freezes.
- Prevention. Break one Coffman condition in the design.
- Avoidance. Grant a request only if the system stays in a safe state.
- Detection and recovery. Look for deadlocks at run time, then terminate a process or take a resource away.
Exercise
Exercise
A system has one printer and one scanner, each with a single instance. P1 holds the printer and requests the scanner. P2 holds the scanner and requests the printer. Is the system deadlocked?
Show answer
Yes. The resource-allocation graph has a cycle, P1 to scanner to P2 to printer to P1. Each resource has a single instance, so the cycle means deadlock.