关于抢占式与非抢占式内核、调度的区别技术问询
Let's cut through the jargon to clarify these two related (but distinct) OS concepts, using definitions from Operating System Concepts (9th Edition) by Silberschatz, Galvin, and Gagne as a foundation.
Kernels: Can Kernel-Mode Processes Be Interrupted?
Preemptive Kernel
"Preemptive kernels allow a process to be removed and replaced while it is running in kernel mode."
In plain terms: Even when a process is doing privileged work (like handling a system call, accessing hardware registers, or managing memory), the OS scheduler can step in, pause that process, and switch to another one. This is critical for keeping the system responsive—think about a kernel task that's taking longer than expected (say, a slow disk read holding up the CPU). The scheduler can interrupt it so other processes don't get starved of CPU time.
Non-Preemptive Kernel
"Non-preemptive kernels do not allow kernel-mode processes to be preempted; a kernel-mode process runs until it exits kernel mode, blocks, or voluntarily yields the CPU."
Here, once a process enters kernel mode, it has exclusive control of the CPU until it decides to give it up. That means if a kernel process gets stuck (or just takes a long time), the entire system might freeze up because no other process can run until it's done. This setup is simpler to implement (fewer race conditions to debug), but it's far less responsive for interactive systems.
Scheduling: How CPU Time Is Allocated to Processes
Scheduling is the broader mechanism that decides which process gets the CPU next. The preemptive/non-preemptive distinction here applies to both user-mode and kernel-mode processes (though kernel type dictates constraints for kernel tasks).
Preemptive Scheduling
The scheduler can take the CPU away from a running user-mode process at any time—usually after a fixed time slice expires, or when a higher-priority process becomes ready. This is what most modern OSes use: your browser, code editor, and music player can all run simultaneously because the scheduler switches between them quickly. Even if one process is hogging the CPU, the OS can interrupt it to let others run.
Non-Preemptive Scheduling
Once a user-mode process starts running, it keeps the CPU until it either finishes, blocks (e.g., waiting for user input), or voluntarily gives up the CPU. This was common in early OSes, but it's terrible for interactive systems—if a process enters an infinite loop, the system becomes unresponsive until that process ends.
Key Distinctions at a Glance
- Kernel type focuses solely on whether kernel-mode execution can be interrupted. Preemptive kernels allow this; non-preemptive ones don't.
- Scheduling type covers the entire CPU allocation strategy, applying to both user and kernel modes (non-preemptive kernels inherently use non-preemptive scheduling for kernel tasks).
- Most modern OSes use preemptive kernels + preemptive scheduling to balance responsiveness and efficiency. Non-preemptive setups are still used in embedded systems where simplicity and predictability matter more than multitasking.
内容的提问来源于stack exchange,提问作者Physco111

