67

Explained in your own words, what is preemption and what does it mean to a (linux) kernel?

What are advantages and disadvantages in having a preemptible kernel?

BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
Markus
  • 1,772
  • 1
  • 12
  • 20
  • 37
    Why the downvote and why "homework"? This is not a homework. In fact, there are people like me who are interested in such a question and don't understand the common Wikipedia articles. – Markus May 03 '09 at 13:32
  • 3
    http://en.wikipedia.org/wiki/Preemption_(computing) – Oli May 03 '09 at 13:45

5 Answers5

78

Preemptive multitasking - Running several processes/threads on a single processor, creating the illusion that they run concurrently when actually each is allocated small multiplexed time slices to run in. A process is "preempted" when it is scheduled out of execution and waits for the next time slice to run in.

A preemptive kernel is one that can be interrupted in the middle of executing code - for instance in response for a system call - to do other things and run other threads, possibly those that are not in the kernel.

The main advantage of a preemptive kernel is that sys-calls do not block the entire system. If a sys-call takes a long time to finish then it doesn't mean the kernel can't do anything else in this time. The main disadvantage is that this introduces more complexity to the kernel code, having to handle more end-cases, perform more fine grained locking or use lock-less structures and algorithms.

tshepang
  • 12,111
  • 21
  • 91
  • 136
shoosh
  • 76,898
  • 55
  • 205
  • 325
  • 10
    This is why when a filesystem goes bad, particularly a network filesystem, you might find yourself with a process that can't be killed. Its sitting around waiting for a sys call to read the filesystem to return, but it never will and it can't be interrupted. – Schwern May 03 '09 at 22:03
17

You should really use the term "preemptive." There are different kinds of preemption. Essentially, it is very simple and you probably understand this by another name. A preemptive operating system can switch contexts between user mode threads without any special programming in the preempted application. This allows for multitasking. An OS can switch away and back to a process and this switching is essentially trasnparent. There is also such a thing as preemptive kernel, which allows kernel mode threads to be preempted (most operating systems do not allow this but it is required for certain applications such as in real time systems). Note, this is a very simplified explanation.

BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
  • 1
    I know, your contribution was made some time ago, but anyway, let me ask you a question. Why would you prefer the term "preemtive" over "preemptible"? Especially for the kernel configuration uses the latter it could be a bit more intuitive using this one. – Paul Kertscher May 28 '14 at 07:15
  • about when kernel preemption occurs : this (https://stackoverflow.com/questions/20769768/why-disabling-interrupts-disables-kernel-preemption-and-how-spin-lock-disables-p) question shows the cases when kernel thread can be preempted. – Chan Kim Nov 17 '20 at 11:26
11

I think this post explains your questions:

what is preemption?

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 etc.

what is a preemption kernel?

Under Linux, user-space programs have always been preemptible : the kernel interrupts user-space programs to switch to other threads, using the regular clock tick. So, the kernel doesn't wait for user-space programs to explicitly release the processor (which is the case in cooperative multitasking). This means that an infinite loop in an user-space program cannot block the system.

However, until 2.6 kernels, the kernel itself was not preemtible : as soon as one thread has entered the kernel, it could not be preempted to execute an other thread. The processor could be used to execute another thread when a syscall was terminated, or when the current thread explictly asked the scheduler to run another thread using the schedule() function. This means that an infinite loop in the kernel code blocked the entire system, but this is not really a problem : the kernel code is designed so that there are no infinite loops.

Kernel preemption has been introduced in 2.6 kernels, and one can enable or disable it using the CONFIG_PREEMPT option. If CONFIG_PREEMPT is enabled, then kernel code can be preempted everywhere, except when the code has disabled local interrupts. An infinite loop in the code can no longer block the entire system. If CONFIG_PREEMPT is disabled, then the 2.4 behaviour is restored.

Pros and cons?

Pros:The preemption kernel can improve latency and scalability, and it can make high priority task run and respond timely.

Cons: It make writing code difficult in preemption kernel, especially in SMP, and you must consider many factors.

Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
9

Others have adequately explained what a preemptible kernel is.

What is it good for?

Mostly the benefits are:

  • Lower latency on non-SMP systems - typically used in realtime systems or for other things where latency is important (audio, video apps perhaps)
  • Teaching kernel developers who don't have SMP systems how to write correct code for SMP

With a non-preemptible kernel, on a single processor system it is possible for kernel developers to be lazy and get away without any locking most of the time - of course this is a big FAIL on SMP. Preemptible kernels allow them to get this pain without more cores.

MarkR
  • 62,604
  • 14
  • 116
  • 151
4

Preemption means the OS supports multiple tasks (a separate, stand-alone piece of code) and will switch between tasks on a schedule. When a task is interrupted, it is called "preempting". Modern OS support this - but it's not required for simple embedded systems, for example. The overhead of supporting task switching is not always worth it.

Jeff
  • 1,969
  • 3
  • 21
  • 35