Consider the process keventd. It spends all it's lifetime in kernel mode. Now, as far as I know, Linux checks if a context switch is due, while the process is switching from kernel mode to user mode, and as far as I know, keventd will never switch from kernel mode to user mode, so, how will the Linux kernel know when to switch it off?
1 Answers
If the kernel were to do as you say, and only check whether a process is due to be switched out on an explicit user-to-kernel-mode transition, then the following loop would lock up a core of your computer:
while (1);
Obviously, this does not happen on normal desktop operating systems. The reason why is preemption, where after a process has run for its time slice the kernel gets an alarm, steps in, and forcibly switches contexts as necessary.
Preemption could in principle work for kernel processes too. However, I'm not sure that's what keventd
does - it's more likely that it voluntarily relinquishes its time slice on a regular basis (see sched_yield
, a userspace call for the same effect), especially since the kernel can be configured to be non-preemptible. That is a kernel process' prerogative.

- 95,191
- 9
- 106
- 122
-
while(1) would switch to kernel mode every scheduler_tick(), so the kernel will find out that the process's timeslice is over while it is in the kernel mode, and would do p->need_resched = 1,and while trying to switch to user-mode, it will call to schedule().. – Shmoopy Jan 28 '12 at 17:00
-
@Shmoopy Correct! I didn't get from your original post that you understood that there were implicit kernel-mode transitions that happen when the program is not making a system call, so I clarified. I'll add the word "explicit" to my answer. Of course, `scheduler_tick` still fires even when you're in kernel space, and you can have tickless kernels... – Borealid Jan 28 '12 at 17:02
-
There will still be calls to scheduler_tick() for keventd (or any kernel process for that matter), but what scheduler_tick does (among other things) is : if (!--p->time_slice) { p->need_recsched = 1 }, so my question is, when will the linux kernel check for that flag if there's never a kernel-to-user transition for a kernel process (keventd in our example). – Shmoopy Jan 28 '12 at 17:07
-
@Shmoopy Read the last paragraph of my answer. As a general rule, kernel processes give up their own slices. – Borealid Jan 28 '12 at 17:15