This post introduces the core concepts and implementation methods of CPU scheduling in operating systems.
Basic Concepts
CPU-I/O Burst Cycle
- Preemptive scheduling: running->ready, waiting->ready
- The CPU is allocated to a process for a limited time
- The process is preempted once its time slice runs out
- Non-preemptive scheduling: running->waiting, terminate
- A process voluntarily gives up the CPU when it does I/O or waits for an event
- A process runs on the CPU until it finishes or blocks voluntarily
Dispatch
- Switching context
- Switching to user mode
- Jumping to the proper location in the user program to restart that program
Latency: the time it takes to stop one process and start another
Scheduling Criteria / Metrics
- CPU Utilization
- Throughput
- Turnaround Time
- Formula:
- is the turnaround time of process i, is the waiting time of process i, and is the running time of process i
- Waiting Time
- Formula:
- is the waiting time of process i, and is the running time of process j
- Response Time
- Formula:
- is the response time of process i, is the waiting time of process i, and is the running time of process i
Scheduling Algorithms
First-Come, First-Served Scheduling (FCFS)
- Non-preemptive scheduling
- Processes are scheduled in the order in which they arrive
Shortest-Job-First Scheduling (SJF)
- Pick the process with the shortest estimated running time to execute
- Minimizes the average waiting time
- Can be done by using the length of previous CPU bursts, using exponential moving average
- Shortest Remaining Time First Scheduling(SRT): Preemptive version of SJF
Priority Scheduling (PS)
- Processes are scheduled according to their priority
- May lead to starvation: Low priority processes may never be executed
- Solution: Aging – as time progresses increase the priority of the process
Round-Robin Scheduling (RR)
- Each process runs on the CPU for one time slice
- When the time slice runs out, the process is moved to the end of the queue
- Preemptive scheduling
Multilevel Queue Scheduling (MQS)
- Processes are divided into multiple queues
- Each queue has its own scheduling algorithm
- Queues are scheduled according to their priority
Multilevel Feedback Queue Scheduling (MFQS)
- Processes are divided into multiple queues
- Each queue has its own scheduling algorithm
- Queues are scheduled according to their priority
- If a process runs for too long in its current queue, it is moved to a higher-priority queue
Thread Scheduling
- User-Level Threads
- Implemented by a user-level thread library, independent of the kernel
- Scheduling takes place in user space
- PCS: process-contention scope
- Kernel-Level Threads
- Implemented by the kernel, dependent on the kernel
- Scheduling takes place in kernel space
- SCS: system-contention scope
Pthread Scheduling
1 |
|
Multi-Processor Scheduling
- Asymmetric multiprocessing
- One master processor is dedicated to scheduling
- The other processors are used to execute processes
- Symmetric multiprocessing(SMP)
- Every processor can take part in scheduling
- Requires more complex scheduling algorithms
Multiple-Processor Scheduling – Load Balancing
- Push migration: pushes a process from one processor to another
- Pull migration: pulls a process from one processor over to another
Processor Affinity
- A process tends to run on a particular processor
- Reduces cache invalidation
- Reduces context switches
Real-Time CPU Scheduling
For real-time scheduling, scheduler must support preemptive, priority-based scheduling
- Hard real-time systems
- Deadlines must be met
- Soft real-time systems
- Deadlines are met as far as possible
Rate Monotonic Scheduling (RMS)
- Priorities are assigned according to the inverse of each process’s period
- The shorter the period, the higher the priority
- If a process’s period is less than or equal to its time slice, it will not be preempted
Earliest Deadline First (EDF)
- Priorities are assigned according to each process’s deadline
- The earlier the deadline, the higher the priority
- If a process’s deadline is less than or equal to its time slice, it will not be preempted
Algorithm Evaluation: Deterministic Modeling
Scheduling algorithm criteria:
(1) CPU utilization, (2) throughput, (3) turnaround time, (4) waiting time, and (5) response time.
Translated from the Chinese original.

