几件事情都可能发生 -
a. The current process (p1) can finish up its timeslice and then the scheduler will check is there is any other process that could be run. If there's no other process, the scheduler will put itself in the idle state. The scheduler will assign p1 to the CPU if p1 is a CPU hoggy task or p1 didn't leave the CPU voluntarily. b. Another possibility is - a high priority task has jumped in. On every scheduler tick, the scheduler will check if there's any process which needs the CPU badly and is likely to preempt the current task.
换句话说,进程可以以两种方式离开CPU - 自愿或非自愿。在第一种情况下,进程将自己置于睡眠状态,从而释放CPU(大小写) a )。在另一种情况下,进程被抢占了更高优先级的任务。
a
(注意:此答案基于CFS任务计划程序 当前的Linux内核)
简而言之,它是一个中断,它将控制权交还给内核。中断可能由于任何原因而出现。 大多数情况下,内核由于定时器中断而获得控制权,或者按键中断可能会唤醒内核。 中断通知外围系统IO或几乎任何改变系统状态的内容都可能 唤醒内核。
的 更多关于中断的信息: 强>
这样的中断分为上半部分和下半部分。底部半部分用于从中断上下文中推迟工作。
上半部分: 运行中断禁用因此应该是超高速,通常尽快放弃CPU
1) stores interrupt state flag and disables the interrupts(reset some pin on the processor), 2) communicates with the hardware, stores state information, delegates remaining responsibility to bottom-half, 3) restores the interrupt state flag and enables the interrupt((set some pin on the processor).
下半区: 处理延迟的工作(上半部分的委托工作)在启用中断的情况下运行,因此可能需要一段时间才能完成。
两种机制用于实现下半部分处理。
1) Tasklets 2) Work queues
。
If timer is the interrupt to switch back to kernel, is the interrupt a hardware interrupt???
在我们讨论的上下文中感兴趣的定时器中断是硬件定时器中断,
在内核中,字定时器中断可能意味着(与架构相关的)硬件定时器中断或软件定时器中断。
读 这个 简要概述。
更多关于 计时器
记住“计时器”是一个高级话题,难以理解。
是中断硬件中断???如果它是硬件 中断,定时器的频率是多少?
读 第10章定时器和时间管理
if the interval of the timer is shorter than time slice, will kernel give the CPU back the same process, which was running early?
这取决于许多因素:使用sheduler,加载系统,处理优先级,类似的东西。 最受欢迎 CFS 并不真正依赖于时间片的概念来抢占! CFS选择的下一个合适的进程将获得CPU时间。
定时器滴答,时间片和上下文切换之间的关系不是那么直截了当。
每个进程都有自己的(动态计算的)时间片。内核跟踪进程使用的时间片。
在SMP上,CPU特定活动(例如监视当前正在运行的进程的执行时间)由本地APIC计时器引发的中断完成。 本地APIC定时器仅向其处理器发送中断。
但是,默认时间片定义在 在include / linux /附表/ rt.h
读 这个 。