gpt4 book ai didi

linux - 切换到用户模式时,非抢占式和抢占式内核有什么区别?

转载 作者:IT王子 更新时间:2023-10-29 00:43:53 24 4
gpt4 key购买 nike

我正在阅读“Understanding the Linux Kernel, 3rd Edition”,在第 5 章“内核抢占”部分,它说:

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.

我在这里仍然看不出非抢占式和抢占式内核之间的区别,因为任何方式都需要等待当前进程切换到用户模式。

假设有一个进程 p 在内核模式下运行,并且其时间量程到期,然后调用 scheduler_tick() 并设置 NEED_RESCHED p 标志。但是 schedule() 仅在 p 切换到用户模式时被调用(对吗?)。

如果 p 永远不会切换到用户模式怎么办?

如果它切换到用户模式,但在 scheduler_tick() 设置 NEED_RESCHEDp 实际上切换到用户模式 ​​- 然后它使用的量超过了它的量程?

最佳答案

在非抢占式内核中,schedule() 在返回到用户空间时被调用(以及系统调用阻塞的任何地方,也在空闲任务上)。

在抢占式内核中,schedule() 也会在从任何中断返回时调用,也会在其他一些地方调用,例如在 mutex_unlock() 慢速路径上,在特定条件下接收网络数据包时,...

举个例子,假设一个进程 A 发出一个系统调用,该系统调用被设备生成的中断中断,然后被定时器中断中断:

 process A userspace → process A kernelspace → device ISR → timer ISR
syscall device IRQ timer IRQ

当计时器 ISR 结束时,它返回到另一个 ISR,然后返回到内核空间,然后返回到用户空间。抢占式内核会在每次返回时检查是否需要重新安排进程。非抢占式内核仅在返回用户空间时进行检查。

关于linux - 切换到用户模式时,非抢占式和抢占式内核有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40204506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com