What is preemption / What is a preemtible kernel? What is it good for?

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?


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.

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.

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.

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.

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.

Need Your Help

How do you run unit tests for a specific target framework in Visual Studio 2017/2019?

c# .net visual-studio visual-studio-2017 multitargeting

I am really loving the new .csproj format. It is so much better than that dreaded (limited) project.json.

Getting Absolute URL fron an ASP.NET MVC Action

c# asp.net-mvc asp.net-mvc-3

This probably is a dummy question but I cannot find a clear indication. I have a POCO class in a MVC3 web application whose only purpose is managing the backup of some files in the server. Typicall...