Corporative Scheduling
- Rather than the Kernel decides when to preempt a Process (进程) and pass the CPU to another process. Process passes control back to kernel for it to perform Process Management
CPU Hogging
Process can hog to CPU forever, modern OS adapts to Preemptive Scheduling instead.
Preemptive Scheduling
- Before Kernel set the Program Counter to the Instruction of a selected Process (进程), the kernel sets the Timer Chip to trigger an Hardware interrupts (外中断) after some period of time(Time Slice)
- The kernel switches the Privilege Level to User Mode and set the program counter to the instruction of a selected process, so the process can start executing
- When the timer chip elapses, it triggers a Hardware interrupts
- The hardware interrupt invokes Trap Interrupt (陷入) which triggers the corresponding Interrupt Handler
- Interrupt handler passes control to Process Scheduler when it completes
- Process Scheduler selects a process to run by restoring the state of the CPU for that process from the process’s Process Control Block (PCB)
- Repeat step 1 to step 6
No CPU Hogging
The hardware interrupt generated by timer chip ensures the kernel obtain control to perform Process Management on a configured interval. The eliminates any process from hogging the CPU forever which may happen in the case of Corporative Scheduling.
Fixed Timeslice Round-Robin Preemptive Scheduling
- Give every Process (进程) the same Time Slice, and the processes are cycled through in order
Laggy Situation
When there is a lot of Process (进程) like
100
and the time slice is a fixed10ms
, one process needs to wait for1000ms
before it gets to run again.
Dynamic Timeslice Round-Robin Preemptive Scheduling
- Time Slice = Target Latency / the number of Process (进程)
- Used in modern Process Scheduler
Important
Modern process scheduler also take in Process Priority to ensure critical processes get more CPU time and run more often.
Helps to make each process more responsive
Ensures each Process will get to run again before it seems laggy to the user. As long as the Minimum Granularity is ensured and Target Latency is not exceeded.
Process gets to run faster when there is less Process
The Time Slice is a ratio of Target Latency and total Process. Less process means more time for each process.