I'm reading "Understanding the Linux Kernel, 3rd Edition", and in Chapter 5, Section "Kernel Preemption", it says:
All process switches are performed by the
switch_to
macro. In both preemptive and nonpreemptive kernels, a process switch occurs when a process has finished some thread of kernel activity and the scheduler is invoked. However, in nonpreemptive kernels, the current process cannot be replaced unless it is about to switch to User Mode.
I still don't see the difference here between non-preemptive and preemptive kernels, because any way you need to wait for the current process to switch to user mode.
Say there is a process p running in kernel mode, and whose time quantum expires, then the scheduler_tick()
is called, and it sets the NEED_RESCHED
flag of p.
But schedule()
is invoked only when p switch to user mode (right?).
So what if p never switches to user mode?
And if it switched to user mode but it takes a "long" time between the moment scheduler_tick()
set NEED_RESCHED
and the moment p actually switched to user mode - then it used more than its quantum?
In a non-preemptive kernel, schedule()
is called when returning to userspace (and wherever a system call blocks, also on the idle task).
In a preemptive kernel, schedule()
is also called when returning from any interrupt, and also in a few other places, e.g. on mutex_unlock()
slow path, on certain conditions while receiving network packets, ...
As an example, imagine a process A which issues a syscall which is interrupted by a device-generated interrupt, that is then interrupted by a timer interrupt:
process A userspace → process A kernelspace → device ISR → timer ISR
syscall device IRQ timer IRQ
When the timer ISR ends, it returns to another ISR, that then returns to kernelspace, which then returns to userspace. A preemptive kernel checks if it needs to reschedule processes at every return. A non-preemptive kernel only does that check when returning to userspace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With