什么是抢占/什么是可抢占内核? 到底有什么好处呢?
用您自己的话解释一下,什么是抢占以及它对(linux)内核意味着什么?
拥有可抢占内核有哪些优点和缺点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
抢占式多任务处理 - 在单个处理器上运行多个进程/线程,造成它们同时运行的错觉,而实际上每个进程/线程都分配了小的多路复用时间片来运行。当进程被安排不执行并等待 。
抢占式内核是一种可以在执行代码的过程中中断的内核(例如响应系统调用),以执行其他操作并运行其他线程(可能是那些不在内核中的线程)
抢占式内核的主要优点是系统调用不会阻塞整个系统。 如果系统调用需要很长时间才能完成,那么并不意味着内核在这段时间内不能执行任何其他操作。
主要缺点是,这会给内核代码带来更多复杂性,必须处理更多最终情况,执行更细粒度的锁定或使用无锁结构和算法。
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.
我认为这篇帖子解释了您的问题:
操作系统抢占或停止当前计划任务以支持更高优先级任务的能力。 调度可以是但不限于进程或I/O调度等之一。
在 Linux 下,用户空间程序始终是可抢占的:内核使用常规时钟滴答中断用户空间程序以切换到其他线程。 因此,内核不会等待用户空间程序显式释放处理器(协作多任务处理就是这种情况)。 这意味着用户空间程序中的无限循环不会阻塞系统。
然而,在 2.6 内核之前,内核本身并不是可抢占的:一旦一个线程进入内核,它就不能被抢占来执行其他线程。 当系统调用终止时,或者当当前线程明确要求调度程序使用 Schedule() 函数运行另一个线程时,处理器可用于执行另一个线程。 这意味着内核代码中的无限循环阻塞了整个系统,但这并不是真正的问题:内核代码的设计是为了不存在无限循环。
内核抢占已在 2.6 内核中引入,可以使用 CONFIG_PREEMPT 选项启用或禁用它。 如果启用 CONFIG_PREEMPT,则内核代码可以在任何地方被抢占,除非代码已禁用本地中断。 代码中的无限循环不能再阻塞整个系统。 如果 CONFIG_PREEMPT 被禁用,则恢复 2.4 行为。
优点:抢占式内核可以改善延迟和可扩展性,并且可以使高优先级任务及时运行和响应。
缺点:在抢占式内核中,尤其是在SMP中,编写代码变得困难,必须考虑很多因素。
I think this post explains your questions:
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.
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: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.
其他人已经充分解释了什么是可抢占内核。
到底有什么好处呢?
主要好处是:
对于不可抢占的内核,在单处理器系统上,内核开发人员可能会因为懒惰而在大多数时间没有任何锁定的情况下逃脱——当然,这在 SMP 上是一个很大的失败。 可抢占式内核允许他们在没有更多内核的情况下承受这种痛苦。
Others have adequately explained what a preemptible kernel is.
What is it good for?
Mostly the benefits are:
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.
抢占意味着操作系统支持多个任务(一段单独的、独立的代码),并且将按计划在任务之间切换。 当任务被中断时,称为“抢占”。 现代操作系统支持这一点 - 但例如,简单的嵌入式系统不需要它。 支持任务切换的开销并不总是值得的。
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.