说“Linux内核是抢占式的”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5283501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does it mean to say "linux kernel is preemptive"?
提问by smwikipedia
I read that Linux kernel is preemptive, which is different from most Unix kernels. So, what does it really mean for a kernal to be preemptive?
我读到 Linux 内核是抢占式的,这与大多数 Unix 内核不同。那么,内核具有抢占性究竟意味着什么呢?
Some analogies or examples would be better than pure theoretical explanation.
一些类比或例子会比纯理论解释更好。
ADD 1 -- 11:00 AM 12/7/2018
添加 1 -- 11:00 AM 12/7/2018
Preemptiveis just one paradigm of multi-tasking. There are others like Cooperative Multi-tasking. A better understanding can be achieved by comparing them.
采纳答案by Eric Seppanen
Imagine the simple view of preemptive multi-tasking. We have two user tasks, both of which are running all the time without using any I/O or performing kernel calls. Those two tasks don't have to do anything special to be able to run on a multi-tasking operating system. The kernel, typically based on a timer interrupt, simply decides that it's time for one task to pause to let another one run. The task in question is completely unaware that anything happened.
想象一下抢占式多任务处理的简单视图。我们有两个用户任务,它们都一直在运行,而不使用任何 I/O 或执行内核调用。这两个任务无需执行任何特殊操作即可在多任务操作系统上运行。内核,通常基于定时器中断,简单地决定是时候暂停一个任务让另一个任务运行。有问题的任务完全不知道发生了什么。
However, most tasks make occasional requests of the kernel via syscalls. When this happens, the same user context exists, but the CPU is running kernel code on behalf of that task.
然而,大多数任务偶尔会通过系统调用向内核发出请求。发生这种情况时,存在相同的用户上下文,但 CPU 正在代表该任务运行内核代码。
Older Linux kernels would never allow preemption of a task while it was busy running kernel code. (Note that I/O operations always voluntarily re-schedule. I'm talking about a case where the kernel code has some CPU-intensive operation like sorting a list.)
较旧的 Linux 内核永远不会允许在忙于运行内核代码时抢占任务。(请注意,I/O 操作总是自动重新调度。我说的是内核代码有一些 CPU 密集型操作,例如对列表进行排序的情况。)
If the system allows that task to be preempted while it is running kernel code,then we have what is called a "preemptive kernel." Such a system is immune to unpredictable delays that can be encountered during syscalls, so it might be better suited for embedded or real-time tasks.
如果系统允许该任务在运行内核代码时被抢占,那么我们就有了所谓的“抢占内核”。这样的系统不受系统调用期间可能遇到的不可预测的延迟的影响,因此它可能更适合嵌入式或实时任务。
For example, if on a particular CPU there are two tasks available, and one takes a syscall that takes 5ms to complete, and the other is an MP3 player application that needs to feed the audio pipe every 2ms, you might hear stuttering audio.
例如,如果在特定 CPU 上有两个任务可用,一个需要 5 毫秒才能完成的系统调用,另一个是需要每 2 毫秒馈送音频管道的 MP3 播放器应用程序,您可能会听到断断续续的音频。
The argument against preemption is that all kernel code that might be called in task context must be able to survive preemption-- there's a lot of poor device driver code, for example, that might be better off if it's always able to complete an operation before allowing some other task to run on that processor. (With multi-processor systems the rule rather than the exception these days, all kernel code must be re-entrant, so that argument isn't as relevant today.) Additionally, if the same goal could be met by improving the syscalls with bad latency, perhaps preemption is unnecessary.
反对抢占的论点是所有可能在任务上下文中调用的内核代码都必须能够在抢占后幸存下来——例如,有很多糟糕的设备驱动程序代码,如果它总是能够在之前完成一个操作,那可能会更好允许其他任务在该处理器上运行。(如今,对于多处理器系统而言,规则而不是例外,所有内核代码都必须是可重入的,因此该论点在今天不再相关。)此外,如果可以通过改进系统调用来实现相同的目标延迟,也许抢占是不必要的。
A compromise is CONFIG_PREEMPT_VOLUNTARY, which allows a task-switch at certain points inside the kernel, but not everywhere. If there are only a small number of places where kernel code might get bogged down, this is a cheap way of reducing latency while keeping the complexity manageable.
一个折衷方案是 CONFIG_PREEMPT_VOLUNTARY,它允许在内核内部的某些点进行任务切换,但不能在任何地方进行。如果只有少数地方内核代码可能会陷入困境,这是一种减少延迟同时保持复杂性可管理的廉价方法。
回答by Amirali Sanatinia
I think it became preemptive from 2.6. preemptive means when a new process is ready to run, the cpu will be allocated to the new process, it doesn't need the running process be co-operative and give up the cpu.
我认为它是从 2.6 开始的。抢占意味着当一个新进程准备运行时,cpu将分配给新进程,它不需要正在运行的进程合作并放弃cpu。
回答by Ben
Traditional unix kernels had a single lock, which was held by a thread while kernel code was running. Therefore no other kernel code could interrupt that thread.
传统的 Unix 内核只有一个锁,在内核代码运行时由一个线程持有。因此没有其他内核代码可以中断该线程。
This made designing the kernel easier, since you knew that while one thread using kernel resources, no other thread was. Therefore the different threads cannot mess up each others work.
这使得设计内核更容易,因为您知道当一个线程使用内核资源时,没有其他线程使用。因此不同的线程不能搞乱彼此的工作。
In single processor systems this doesn't cause too many problems.
在单处理器系统中,这不会引起太多问题。
However in multiprocessor systems, you could have a situation where several threads on different processors or cores all wanted to run kernel code at the same time. This means that depending on the type of workload, you could have lots of processors, but all of them spend most of their time waiting for each other.
但是,在多处理器系统中,您可能会遇到不同处理器或内核上的多个线程都希望同时运行内核代码的情况。这意味着根据工作负载的类型,您可能有很多处理器,但它们大部分时间都在等待对方。
In Linux 2.6, the kernel resources were divided up into much smaller units, protected by individual locks, and the kernel code was reviewed to make sure that locks were only held while the corresponding resources were in use. So now different processors only have to wait for each other if they want access to the same resource (for example hardware resource).
在 Linux 2.6 中,内核资源被划分为更小的单元,由单独的锁保护,并检查内核代码以确保仅在使用相应资源时才持有锁。因此,现在不同的处理器如果想要访问相同的资源(例如硬件资源),只需相互等待即可。
回答by Kevin
The preemption allows the kernel to give the IMPRESSION of parallelism: you've got only one processor (let's say a decade ago), but you feel like all your processes are running simulaneously. That's because the kernel preempts (ie, take the execution out of) the execution from one process to give it to the next one (maybe according to their priority).
抢占允许内核提供并行性的印象:您只有一个处理器(假设是十年前),但您感觉所有进程都在同时运行。那是因为内核抢占(即,取消执行)一个进程的执行以将其交给下一个进程(可能根据它们的优先级)。
EDITNot preemptive kernels wait for processes to give back the hand(ie, during syscalls), so if your process computes a lot of data and doesn't call any kind of yield
function, the other processes won't be able to execute to execute their calls. Such systems are said to be cooperativebecause they ask for the cooperation of the processes to ensure the equity of the execution time
编辑不是抢占式内核等待进程交还手(即,在系统调用期间),因此如果您的进程计算大量数据并且不调用任何类型的yield
函数,则其他进程将无法执行他们的电话。这样的系统被称为合作的,因为它们要求进程的合作以确保执行时间的公平
EDIT 2The main goal of preemption is to improve the reactivity of the system among multiple tasks, so that's good for end-users, whereas on the other-hand, servers want to achieve the highest througput, so they don't need it: (from the Linux kernel configuration)
编辑 2抢占的主要目标是提高系统在多个任务之间的反应性,这对最终用户有好处,而另一方面,服务器想要实现最高吞吐量,所以他们不需要它: (来自Linux内核配置)
- Preemptible kernel(low-latency desktop)
- Voluntary kernel preemption(desktop)
- No forced preemption(server)
回答by Damien
The linux kernel is monolithic and give a little computing timespan to all the running process sequentially. It means that the processes (eg. the programs) do not run concurrently, but they are given a give timespan regularly to execute their logic. The main problem is that some logic can take longer to terminate and prevent the kernel to allow time for the next process. This results in system "lags".
linux 内核是单片的,并为所有正在运行的进程顺序提供了一点计算时间跨度。这意味着进程(例如程序)不会同时运行,但会定期给它们一个给定的时间跨度来执行它们的逻辑。主要问题是某些逻辑可能需要更长时间才能终止并阻止内核为下一个进程留出时间。这会导致系统“滞后”。
A preemtive kernel has the ability to switch context. It means that it can stop a "hanging" process even if it is not finished, and give the computing time to the next process as expected. The "hanging" process will continue to execute when its time has come without any problem.
抢先内核具有切换上下文的能力。这意味着即使它没有完成,它也可以停止“挂起”的进程,并按预期将计算时间交给下一个进程。“挂起”进程将在没有任何问题的时间到来时继续执行。
Practically, it means that the kernel has the ability to achieve tasks in realtime, which is particularly interesting for audio recording and editing.
实际上,这意味着内核具有实时完成任务的能力,这对于音频录制和编辑来说尤其有趣。
The ubuntu studio districutionpackages a preemptive kernel as well as a buch of quality free software devoted to audio and video edition.
在Ubuntu的工作室districution包一个抢占的内核,以及专门的音频和视频版的品质的免费软件布赫。
回答by Matteo Italia
It means that the operating system scheduler is free to suspend the execution of the running processes to give the CPU to another process whenever it wants; the normal way to do this is to give to each process that is waiting for the CPU a "quantum" of CPU time to run. After it has expired the scheduler takes back the control (and the running process cannot avoid this) to give another quantum to another process.
这意味着操作系统调度程序可以自由地暂停正在运行的进程的执行,以便在需要时将 CPU 交给另一个进程;执行此操作的正常方法是为每个等待 CPU 的进程提供一个“量子”的 CPU 运行时间。在它到期后,调度程序收回控制权(并且正在运行的进程无法避免这种情况)以将另一个量程分配给另一个进程。
This method is often compared with the cooperative multitasking, in which processes keep the CPU for all the time they need, without being interrupted, and to let other applications run they have to call explicitly some kind of "yield" function; naturally, to avoid giving the feeling of the system being stuck, well-behaved applications will yield the CPU often. Still,if there's a bug in an application (e.g. an infinite loop without yield calls) the whole system will hang, since the CPU is completely kept by the faulty program.
这种方法通常与协作多任务处理相比,在协作多任务处理中,进程在它们需要的所有时间都保持 CPU,不被中断,并且为了让其他应用程序运行,它们必须显式调用某种“yield”函数;自然地,为了避免给人一种系统卡住的感觉,表现良好的应用程序会经常让出 CPU。尽管如此,如果应用程序中存在错误(例如,没有yield 调用的无限循环)整个系统将挂起,因为CPU 完全由错误程序保留。
Almost all recent desktop OSes use preemptive multitasking, that, even if it's more expensive in terms of resources, is in general more stable (it's more difficult for a sigle faulty app to hang the whole system, since the OS is always in control). On the other hand, when the resources are tight and the application are expected to be well-behaved, cooperative multitasking is used. Windows 3 was a cooperative multitasking OS; a more recent example can be RockBox, an opensource PMP firmware replacement.
几乎所有最近的桌面操作系统都使用抢占式多任务处理,即使它在资源方面更昂贵,通常也更稳定(单个故障应用程序更难以挂起整个系统,因为操作系统始终处于控制之中)。另一方面,当资源紧张并且期望应用程序表现良好时,使用协作多任务处理。Windows 3 是一个协作式多任务操作系统;最近的一个例子是 RockBox,一个开源 PMP 固件替代品。
回答by pradeepchhetri
Prior to Linux kernel version 2.5.4, Linux Kernel was not preemptive which means a process running in kernel mode cannot be moved out of processor until it itself leaves the processor or it starts waiting for some input output operation to get complete.
在 Linux 内核版本 2.5.4 之前,Linux 内核不是抢占式的,这意味着在内核模式下运行的进程无法移出处理器,直到它自己离开处理器或开始等待某些输入输出操作完成。
Generally a process in user mode can enter into kernel mode using system calls. Previously when the kernel was non-preemptive, a lower priority process could priority invert a higher priority process by denying it access to the processor by repeatedly calling system calls and remaining in the kernel mode. Even if the lower priority process' timeslice expired, it would continue running until it completed its work in the kernel or voluntarily relinquished control. If the higher priority process waiting to run is a text editor in which the user is typing or an MP3 player ready to refill its audio buffer, the result is poor interactive performance. This way non-preemptive kernel was a major drawback at that time.
通常,处于用户模式的进程可以使用系统调用进入内核模式。以前,当内核是非抢占式时,较低优先级的进程可以通过重复调用系统调用并保持内核模式来拒绝较高优先级进程访问处理器的优先级。即使较低优先级进程的时间片过期,它也会继续运行,直到它完成在内核中的工作或自愿放弃控制权。如果等待运行的优先级较高的进程是用户正在输入的文本编辑器或准备重新填充其音频缓冲区的 MP3 播放器,则结果是交互性能不佳。这种方式非抢占式内核在当时是一个主要缺点。
回答by BHS
Linux kernel is preemptive means that The kernel supports preemption.
Linux 内核是抢占式意味着内核支持抢占式。
For example, there are two processes P1(higher priority) and P2(lower priority) which are doing read system calls and they are running in kernel mode. Suppose P2 is running and is in the kernel mode and P2 is scheduled to run.
例如,有两个进程 P1(较高优先级)和 P2(较低优先级)正在执行读取系统调用并且它们在内核模式下运行。假设 P2 正在运行并且处于内核模式,并且 P2 计划运行。
If kernel preemption is available, then preemption can happen at the kernel level i.e P2 can get preempted and but to sleep and the P1 can continue to run.
如果内核抢占可用,那么抢占可以发生在内核级别,即 P2 可以被抢占,但进入睡眠状态,P1 可以继续运行。
If kernel preemption is not available, since P2 is in kernel mode, system simply waits till P2 is complete and then
如果内核抢占不可用,由于 P2 处于内核模式,系统只需等待 P2 完成,然后
回答by grepit
I think everyone did a good job of explaining this but I'm just gonna add little more info. in context of Linux IRQ, interrupt and kernel scheduler.
我认为每个人都很好地解释了这一点,但我只想添加更多信息。在 Linux IRQ、中断和内核调度程序的上下文中。
Process scheduler is the component of the OS that is responsible for deciding if current running job/process should continue to run and if not which process should run next.
进程调度程序是操作系统的一个组件,负责决定当前正在运行的作业/进程是否应该继续运行,如果不是,接下来应该运行哪个进程。
preemptive scheduleris a scheduler which allows to be interrupted and a running process then can change it's state and then let another process to run (since the current one was interrupted).
抢占式调度程序是一个允许被中断的调度程序,一个正在运行的进程可以改变它的状态,然后让另一个进程运行(因为当前进程被中断了)。
On the other hand, non-preemptivescheduler can't take away CPU away from a process (aka cooperative) FYI, the name word "cooperative" can be confusing because the word's meaning does not clearly indicate what scheduler actually does.
另一方面,非抢占式调度程序不能从进程(又名合作)中夺走 CPU 仅供参考,“合作”这个名称可能会令人困惑,因为该词的含义并没有清楚地表明调度程序实际做什么。
For example, Older Windows like 3.1 had cooperative schedulers.
例如,像 3.1 这样的旧 Windows 具有协作调度程序。
Full credit to wonderful article here