Linux 中的上下文切换内核进程
考虑 keventd 进程。它的整个生命周期都处于内核模式。 现在,据我所知,Linux 在进程从内核模式切换到用户模式时检查上下文切换是否到期,并且据我所知,keventd 永远不会从内核模式切换到用户模式,那么,如何Linux 内核知道何时关闭它吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果内核按照您所说的进行操作,并且仅检查进程是否应在显式用户模式到内核模式转换时切换出,那么以下循环将锁定计算机的核心:
显然,这在普通桌面操作系统上不会发生这种情况。原因是抢占,在进程运行完其时间片后,内核会收到警报,介入并根据需要强制切换上下文。
抢占原则上也适用于内核进程。但是,我不确定这就是 keventd 的作用 - 它更有可能定期自愿放弃其时间片(请参阅 sched_yield,这是对相同内容的用户空间调用效果),特别是因为内核可以配置为不可抢占。这是内核进程的特权。
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:
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 (seesched_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.