Preemption (computing)
From Wikipedia, the free encyclopedia
Pre-emption as used with respect to operating systems means the ability of the operating system to preempt or stop a currently scheduled task in favour of a higher priority task. The scheduling may be one of, but not limited to, process or I/O scheduling, among others.
Non-preemptability arises, for instance, when handling an interrupt. In this case, scheduling is avoided until the interrupt is handled. Making a scheduler preemptible has the advantage of better system responsiveness and scalability.
The schedulers used in most modern operating systems, such as various flavours of Unix, can preempt user processes. This is called preemptive multitasking, and is in contrast to cooperative multitasking wherein a process "gives away" its time by utilizing kernel resources or by specifically calling a kernel routine to allow other processes time to run. Some operating systems' schedulers (including Linux as of the 2.6 series) have the ability to preempt a process while it is processing a system call as well (a preemptible kernel).
Windows XP, Linux, Unix, AmigaOS, *BSD, Mac OS X, and Windows NT are all examples of operating systems that utilize preemptive multitasking; NetWare, Windows for Workgroups, and Macintosh System 9 are all examples of cooperative multitasking operating systems.
[edit] Pre-emptive multitasking
Pre-emptive multitasking is a form of multitasking. To understand the concept, compare to cooperative multiprocessing, in which only the active task (also known as process) may initiate a context switch because the task has:
- completed processing.
- become blocked on a shared resource.
- yielded the processor to another, similarly cooperative, task.
In pre-emptive multitasking, the operating system kernel can also initiate a context switch to satisfy the scheduling policy's priority constraint, thus pre-empting the active task.
Pre-emptive multitasking is sometimes mistakenly used when the intended meaning is more specific, referring instead to the class of scheduling policies known as time-shared scheduling, or time-sharing.
Preemptive multitasking allows the computer system to more reliably guarantee each process a regular "slice" of operating time. It also allows the system to rapidly deal with important external events like incoming data, which might require the immediate attention of one or another process.
At any specific time, processes can be grouped into two categories: those that are waiting for input or output (called "I/O bound"), and those that are fully utilizing the CPU ("CPU bound"). In early systems, processes would often "poll", or "busywait" while waiting for requested input (such as disk, keyboard or network input). During this time, the process was not performing useful work, but still maintained complete control of the CPU. With the advent of interrupts and preemptive multitasking, these I/O bound processes could be "blocked", or put on hold, pending the arrival of the necessary data, allowing other processes to utilize the CPU. As the arrival of the requested data would generate an interrupt, blocked processes could be guaranteed a timely return to execution.
Although multitasking techniques were originally developed to allow multiple users to share a single machine, it soon became apparent that multitasking was useful regardless of the number of users. Many operating systems, from mainframes down to single-user personal computers, have recognized the usefulness of multitasking support for a variety of reasons. Multitasking makes it possible for a single user to run multiple applications at the same time, or to run "background" processes while retaining control of the computer.
In simple terms: Pre-emptive multitasking involves the use of an interrupt mechanism which suspends the currently executing process and invokes a scheduler to determine which process should execute next. Therefore all processes will get some amount of CPU time at any given time.
[edit] Time slice
The period of time for which a process is allowed to run in a preemptive multitasking system is generally called the "time slice". The scheduler is run once every time slice to choose the next process to run. If the time slice is too short then the scheduler will consume too much processing time but if it is too long then processes may not be able to respond to external events quickly enough.
An interrupt is scheduled to allow the operating system kernel to switch between processes when their time slice expires, effectively allowing the processor’s time to be shared between a number of tasks, giving the illusion that it is dealing with these tasks simultaneously, or concurrently. The operating system which controls such a design is called a multi-tasking system.