CPU Scheduling Concepts


CPU Scheduling Concepts

Simon
We’ve been talking about CPU scheduling all semester, so we know what a scheduler does: it decides which process gets access to a CPU. Now we’re turning to how it makes that decision. We’ll start by assuming there’s one CPU, so we can focus on the basic ideas, and later consider scheduling when a system has multiple CPUs.

Michael
And with multiple CPUs, the scheduler has two related decisions: which process gets which CPU, and for how long. But even on a single CPU, it needs to choose among processes that are ready to run.

Simon
To understand those choices, let’s revisit CPU bursts and I/O bursts. A CPU burst is the period when a process is doing work that doesn’t require an I/O request or another service from the operating system. Arithmetic operations like addition and multiplication count, and so do memory operations like loads and stores.

Michael
Even if a load or store makes the CPU stall while the memory system does its work, the operating system still counts that time as CPU time for the process. From the operating system’s point of view, the process has the CPU during that operation, even if it isn’t using the CPU efficiently.

Simon
The boundary comes when the process requests I/O or another operating-system service—for example, reading from a file or from the keyboard. That request ends the CPU burst. The process then waits for the I/O service, which is an I/O burst, rather than continuing to run.

Michael
And when the I/O finishes, the process doesn’t automatically get the CPU. It moves from waiting to ready. Then the scheduler can choose it from the ready processes, alongside any others waiting for a turn.

Simon
A distribution of CPU-burst lengths gives us a useful picture of typical behavior. In the example we’re considering, bursts around two milliseconds are the most common, while bursts longer than eight milliseconds are rare.

Michael
The reason is that a process often requests I/O before it has spent a very long time running. Two milliseconds may sound brief, but it’s a huge amount of time on a computer. At one gigahertz, the processor runs about one billion cycles in a second.

Simon
The number of operations in those cycles depends on the operation. An addition might take one cycle, multiplication can take a few, and a load that misses the cache and goes to main memory can take hundreds of cycles. If we use ten cycles per operation as a rough example, that’s about one hundred million operations per second.

Michael
So in one millisecond, that rough example gives us about one hundred thousand operations; in two milliseconds, about two hundred thousand. That helps explain why a burst lasting several milliseconds can involve a great deal of work without an I/O request—and why such long bursts are relatively uncommon.

Simon
The part of the operating system that selects a process from the ready set is the short-term, or CPU, scheduler. We call the set of ready processes the ready queue, but “queue” doesn’t require a particular implementation. It could be a first-in, first-out queue, a priority queue, or even a tree.

Michael
That flexibility matters because real scheduling often uses priorities. If processes are organized by priority, the operating system may use a priority queue or a tree to keep track of them. The ready queue is really the data structure the system uses to maintain its ready processes.

Simon
The scheduler has to make choices at several points. One is when a running process requests I/O and becomes waiting. Another is when a process is interrupted and becomes ready again, so another process can run. That second kind of interruption is the basis of preemptive scheduling.

Michael
With preemption, the operating system can take the CPU away from a process—for example, when a timer interrupt indicates that its time is up—and give the CPU to another process. This requires hardware support, including a timer. Without a timer, the operating system can’t use that mechanism to interrupt a running process.

Simon
In non-preemptive scheduling, the operating system doesn’t interrupt a process to take the CPU away. The process keeps running until it releases the CPU itself, such as by requesting I/O. Scheduling decisions also arise when I/O completes and a process returns from waiting to ready, or when a process terminates.

Michael
It helps to distinguish the scheduler from the dispatcher. The scheduler chooses which process should get the CPU. The dispatcher carries out that choice: it saves the state of the process losing the CPU and restores the state of the process that’s about to run.

Simon
The dispatcher also switches the processor into user mode and transfers execution to the right place in the selected program. Switching to user mode is essential: if a user process were allowed to run with kernel privileges, it could perform operations that should be restricted to the operating system.

Michael
The time required to carry out this handoff—the dispatcher's work—is called dispatch latency. It’s useful to remember that a scheduling decision isn’t free: the operating system has to do the work needed to switch from one process to another.

Simon
To compare scheduling algorithms, we need criteria. CPU utilization measures how much the CPU is being used, and we generally want to maximize it. Throughput is the number of processes or tasks completed in a given period, and we generally want that to be high too.

Michael
Turnaround time measures the time from submitting a task to the system until it finishes, so we generally want to minimize it. Waiting time is the time a process spends in the ready queue, and that’s another measure we want to minimize.

Simon
Response time is different from turnaround time. It measures the interval from submitting a task until the first response. A process might respond quickly but still take a long time to finish, so both measures can matter.

Michael
These goals can conflict. Consider shortening the time quantum—the amount of time a process can run before the scheduler may preempt it. A shorter quantum lets processes take turns more often, which can improve response time because users see activity sooner.

Simon
But frequent turns mean frequent context switches, and those switches take time in the kernel. That overhead can make turnaround time worse: processes may take longer to finish because less time is spent doing their useful work.

Michael
It can also reduce throughput, because the system spends more time switching between processes and completes less work in a given period. So a system can feel more responsive to users while using its resources less efficiently.

Simon
That’s the balancing act. User needs may favor quick responses, while system goals include using resources efficiently and completing work. Making the quantum too short might help one goal while hurting others, so a scheduler has to weigh the tradeoffs.

Michael
As we study different scheduling algorithms, the main measure we’ll use is waiting time—more specifically, average waiting time. Looking at an average gives us a way to compare how processes fare overall, though other measures may matter for particular goals.

Simon
For example, if interactivity is especially important, it may be useful to focus on the worst response time rather than only an average. The larger point is that scheduling algorithms are evaluated against chosen criteria, and the best choice depends on which outcomes the system needs to prioritize.

Ask anything
We've read this whole study set, so questions about it get the best answers.