Now that we know how the operating system can switch from one process to another, we can ask: how does it decide who gets to run when?
A process is a running program. Each process has its own virtual address space and some other state.
A thread (or sometimes task) is a what actually runs instructions within a process. Each thread has its own program counter. One process can have multiple threads that all share the process’s virtual address space.
A process switch is just a thread switch where the threads are in different processes.
Suppose there are a bunch of threads that all want to run on the CPU. The operating system needs a system for deciding which thread to run when.
And when one thread stops running, the OS needs to pick a new thread to run.
This is called a scheduling algorithm.
What are the criteria we should use for judging different scheduling algorithms?
Throughput: how much total work gets done. Since context switches don’t count as work, this is usually the same as minimizing context switches.
Latency: how long does a thread have to wait to do some work. Usually shows up as responsiveness. If your video game has to wait a long time to get a chance to respond to your keystrokes it feels laggy.
Fairness: can one thread or process hog the CPU or does every thread get a fair chance to run?
Simplicity. Software is hard to get right so you don’t want your scheduling algorithm to be too complicated.
Also if your scheduling algorithm is too complicated, you will spend more time and memory running the scheduling algorithm that could have otherwise been spent on useful work.
What are some ways the operating system could try to schedule threads to maximize one or more of these criteria?
The simplest scheduling scheme: as soon as a thread is ready to run, run it to completion. Then run whatever thread was ready next. Also called FIFO for “first in, first out”.
Throughput is great (minimum context switching overhead ) but latency is terrible. Also not very fair since the first thing to run can use as much CPU as it wants.
Also pretty simple, just run threads in FIFO order but for a fixed time slice (sometimes called a time quantum). When a thread is suspended it goes to the back of the queue.
Simple to implement and totally fair but its behavior hinges on the length of the time slice.
Longer slices increase throughput at the cost of latency while shorter slices lose throughput due to increased context switching costs.
Make a set of queues, in priority order. Each thread goes in one queue and the scheduler schedules threads from the highest priority non-empty queue.
Simple version is not fair in that high-priority threads can completely starve low-priority threads. Sometimes that’s what you want.
The basic MLQ structure can use a different algorithm for choosing which queue to take tasks from such as 80% of CPU time goes to one queue and 20% to another.
Also, each priority queue can have it’s own scheduling algorithm such as FIFO or round robin.
MLFQ (Multilevel Feedback Queue) is a variation where threads are moved between queues to adjust their priority.
Assign every thread a numeric priority and run the highest priority threads at any given time. Threads of equal priority take turns using the CPU until they’re all done, similar to how the highest priority queue in MLQ scheduling starves threads in lower-priority queues.
Real priority scheduling systems tend to adjust priorities so avoid starvation such as by bumping the priority of threads that have been waiting for a long time so eventually they will get to the head of the line.
When do we switch threads?
When the running thread completes.
When the running thread’s time slice runs out.
When the running thread does a blocking operation.
Remember the memory hierarchy? Some operations are really slow compared to the CPU executing instructions or dealing with RAM.
Things like reading or writing the disk take so long (hundreds of thousands or millions of instructions cycles) that they are considered “blocked” until the disk finishes it work.
When a thread is blocked waiting for a disk, the CPU should run some thread that isn’t blocked. So the scheduler needs to pick another thread.
A scheduling algorithm is designed to make a particular set of tradeoffs among throughput, latency, and fairness.
And all things being equal, simpler algorithms are better than more complex ones both because they’re easier to get right and because they may add less scheduling overhead.
The main job of a scheduling algorithm is to decide which thread to run whenever the currently running thread completes or blocks or is suspended by the scheduler.
It is also up to the scheduler to decide when to suspend the running thread which it can implement by setting a timer and then suspending the thread and scheduling a new one when the timer goes off.