Resource preemption is a technique used to break a deadlock in a computer system. In other words, it is a technique that an operating system uses to break up a situation in which every resource is used up by a variety of processes, each of which are waiting on more resources.
To eliminate deadlocks using resource preemption, we successively take away some resources from processes and give these resources to other processes until the deadlock is broken; this usually occurs when one process is given enough resources to be able to complete, then frees up all of the resources it was using.
Resources, in terms of resource preemption, refer to anything that might be of importance in a computer: memory, a particular piece of data, CPU time, use of devices such as your hard drive or your printer, and so forth. Managing these resources is a fundamental part of what your operating system does to keep your computer running.
If resource preemption is used to deal with deadlocks, three issues must be considered.
- Selection of a victim Which processes can have resources taken away from them easily? The goal here is to minimize cost; if a resource can be taken away from one process much more easily than an identical resource can be taken away from another process, we select the victim that we will have the easiest time removing the resource from.
- Rollback If we preempt a resource from a process, what should be done with that process? If a resource is taken away, it obviously cannot continue in its current state; it would be much as if I chopped off one of your legs and expected you to continue to walk normally. One thing we can do is roll the process back to a safe state; one where the resource hasn't been taken up by the process yet. Unfortunately, most operating systems don't have the capability to store states of processes as they progress normally, and it is often impossible to determine a safe state given just the current state. The solution then is a total rollback; aborting the process and starting from scratch. A total rollback is usually a last ditch resort, only usable if a deadlock can't be resolved without a total rollback.
- Starvation This happens when the same process is chosen again and again as a victim, making sure that that process can never finish. To prevent starvation, an operating system usually limits the number of times a specific process can be chosen as a victim.
Most modern operating systems attempt resource preemption as a way to solve deadlocks. The alternative method of killing off processes often means that a much greater amount of computational work is lost; it's not a preferable method of dealing with deadlocks simply because of the lost time.
Implementing resource preemption is often one of the trickiest parts of writing an operating system. Many operating systems use a very simple method of selecting processes for preemption: the one with the lowest priority receives a total rollback, then on up the chain until the highest priority process can run. Other, more complex schemes exist, but most operating systems in wide use use some form of the method just described to break up deadlocks.
Resource preemption is an integral part of the operating system that runs your computer. Every time you start an application, resource preemption has to decide what program gets use the memory and CPU of your computer. It's a vital task that your operating system deals with for you.