Linux中的上下文切换内核进程

时间:2021-12-28 18:12:54

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?

考虑keventd的过程。它在内核模式下花费了所有的生命周期。现在,据我所知,Linux检查上下文切换是否到期,同时进程从内核模式切换到用户模式,据我所知,keventd永远不会从内核模式切换到用户模式,所以,如何Linux内核知道何时关闭它?

1 个解决方案

#1


4  

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.

Preemption原则上也可以用于内核进程。但是,我不确定keventd是做什么的 - 它更可能是它定期自愿放弃它的时间片(参见sched_yield,用户空间调用相同的效果),特别是因为内核可以配置为非抢占。这是一个内核进程的特权。

#1


4  

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.

Preemption原则上也可以用于内核进程。但是,我不确定keventd是做什么的 - 它更可能是它定期自愿放弃它的时间片(参见sched_yield,用户空间调用相同的效果),特别是因为内核可以配置为非抢占。这是一个内核进程的特权。