Types of Schedulers in OS
Three schedulers make three different choices: which jobs enter memory, which ready process runs next, and which process is swapped out when memory runs short.
Three Decisions, Three Timescales
- Long term (job scheduler) – picks which new jobs are loaded into memory: new → ready.
- Short term (CPU scheduler) – picks which ready process gets the CPU next: ready → running.
- Medium term (swapper) – picks which process to take out of memory, and when to bring it back: ready or blocked → suspended, and back.
An operating system has to answer three separate questions about processes, and each one is answered by its own scheduler. Each scheduler picks from a different queue and runs at a different rate.
The rate follows from the weight of the decision. Admitting a job changes the load on the whole machine for minutes, so it can be made slowly and with care. Choosing the next process to run lasts only one time slice, so it is made hundreds of times a second and must cost almost nothing.
- Each scheduler owns one transition in the process state diagram
- When people say "the scheduler" they nearly always mean the short term one
- Rare decisions can be slow; frequent ones must be fast
Long Term Scheduler
The long term scheduler, also called the job scheduler, chooses which jobs waiting on disk are loaded into memory and become ready. It therefore sets the degree of multiprogramming: the number of processes in memory at once. In a steady state it admits a new job only when one leaves, so the admission rate matches the departure rate.
Because it runs seconds or minutes apart, it can look at the job mix. A good mix of I/O-bound jobs (short CPU bursts, lots of waiting) and CPU-bound jobs (long bursts) keeps both the CPU and the devices busy. All I/O-bound and the ready queue is nearly always empty; all CPU-bound and the devices sit idle.
It also protects memory. Admit more jobs than RAM can hold working sets for, and every job faults constantly, which is thrashing. Admit too few and the CPU waits with nothing to do.
- Controls the degree of multiprogramming
- Balances CPU-bound and I/O-bound jobs
- Found in batch systems; Unix and Windows start a program as soon as you run it
Short Term Scheduler
The short term scheduler, or CPU scheduler, picks one process from the ready queue each time the CPU becomes free: when the running process blocks, ends, or is preempted by a timer interrupt. On a busy system that is every few milliseconds.
Its own run time is pure overhead. If choosing a process took 1 ms and the process then ran for 4 ms, a fifth of the CPU would go to scheduling, so the algorithms used here are kept simple: round robin, priority scheduling, multilevel feedback queues and the others in CPU scheduling algorithms.
- Runs on every block, exit, timer interrupt or higher-priority arrival
- The scheduling algorithm decides response time, fairness and throughput
- Choosing a different process costs a context switch
Terms, operations, and practical uses
Terms to know
- Degree of multiprogrammingHow many processes are in memory at the same time.
- Suspended processA process swapped out to disk; it keeps its PCB but no memory.
- DispatcherThe code that switches the CPU to the process the scheduler chose.
- Dispatch latencyTime taken to stop one process and start another.
Related lessons
- Process states and PCBThe states these schedulers move processes between.
- CPU scheduling algorithmsWhat the short term scheduler runs.
- Swapping in OSHow the medium term scheduler moves a process out and in.
- ThrashingWhat happens when too many processes are admitted.
Five jobs through all three schedulers
TOTAL = 60
need = {"J1": 15, "J2": 15, "J3": 15, "J4": 15, "J5": 15}
new = ["J1", "J2", "J3", "J4", "J5"]
ready, suspended = [], []
running = None
def used():
resident = ready + ([running] if running else [])
return sum(need[j] for j in resident)
def show(who, what):
print(f"{who:<7}{what:<22}used {used()}/{TOTAL}")
def long_term():
# Admit only while a full working set still fits in RAM.
while new and used() + need[new[0]] <= TOTAL:
job = new.pop(0)
ready.append(job)
show("long", f"admit {job}")
if new:
show("new", f"{new[0]} waits")
def short_term():
global running
if running:
ready.append(running)
running = ready.pop(0) if ready else None
show("short", f"run {running}")
def medium_term():
# Over-committed: suspend from the back of the queue until it fits.
while used() > TOTAL:
victim = ready.pop()
suspended.append(victim)
show("medium", f"swap out {victim}")
while suspended and used() + need[suspended[0]] <= TOTAL:
job = suspended.pop(0)
ready.append(job)
show("medium", f"swap in {job}")
def finish():
global running
job, running = running, None
show("exit", f"{job} finishes")
def main():
long_term()
short_term()
finish()
long_term()
short_term()
need["J2"] = 30
show("grow", "J2 needs 30")
medium_term()
short_term()
finish()
medium_term()
if __name__ == "__main__":
main()#include <cstdio>
#include <deque>
#include <map>
#include <string>
using namespace std;
const int TOTAL = 60;
map<string, int> need = {{"J1", 15}, {"J2", 15}, {"J3", 15}, {"J4", 15}, {"J5", 15}};
deque<string> fresh = {"J1", "J2", "J3", "J4", "J5"};
deque<string> ready, suspended;
string running;
int used() {
int total = running.empty() ? 0 : need[running];
for (const string& j : ready) total += need[j];
return total;
}
void show(const string& who, const string& what) {
printf("%-7s%-22sused %d/%d\n", who.c_str(), what.c_str(), used(), TOTAL);
}
void longTerm() {
// Admit only while a full working set still fits in RAM.
while (!fresh.empty() && used() + need[fresh.front()] <= TOTAL) {
string job = fresh.front();
fresh.pop_front();
ready.push_back(job);
show("long", "admit " + job);
}
if (!fresh.empty()) show("new", fresh.front() + " waits");
}
void shortTerm() {
if (!running.empty()) ready.push_back(running);
running = ready.empty() ? "" : ready.front();
if (!ready.empty()) ready.pop_front();
show("short", "run " + running);
}
void mediumTerm() {
// Over-committed: suspend from the back of the queue until it fits.
while (used() > TOTAL) {
string victim = ready.back();
ready.pop_back();
suspended.push_back(victim);
show("medium", "swap out " + victim);
}
while (!suspended.empty() && used() + need[suspended.front()] <= TOTAL) {
string job = suspended.front();
suspended.pop_front();
ready.push_back(job);
show("medium", "swap in " + job);
}
}
void finish() {
string job = running;
running = "";
show("exit", job + " finishes");
}
int main() {
longTerm();
shortTerm();
finish();
longTerm();
shortTerm();
need["J2"] = 30;
show("grow", "J2 needs 30");
mediumTerm();
shortTerm();
finish();
mediumTerm();
}import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
static final int TOTAL = 60;
static Map<String, Integer> need = new HashMap<>();
static Deque<String> fresh = new ArrayDeque<>(List.of("J1", "J2", "J3", "J4", "J5"));
static Deque<String> ready = new ArrayDeque<>();
static Deque<String> suspended = new ArrayDeque<>();
static String running = null;
static int used() {
int total = running == null ? 0 : need.get(running);
for (String j : ready) total += need.get(j);
return total;
}
static void show(String who, String what) {
System.out.printf("%-7s%-22sused %d/%d%n", who, what, used(), TOTAL);
}
static void longTerm() {
// Admit only while a full working set still fits in RAM.
while (!fresh.isEmpty() && used() + need.get(fresh.peekFirst()) <= TOTAL) {
String job = fresh.pollFirst();
ready.addLast(job);
show("long", "admit " + job);
}
if (!fresh.isEmpty()) show("new", fresh.peekFirst() + " waits");
}
static void shortTerm() {
if (running != null) ready.addLast(running);
running = ready.pollFirst();
show("short", "run " + running);
}
static void mediumTerm() {
// Over-committed: suspend from the back of the queue until it fits.
while (used() > TOTAL) {
String victim = ready.pollLast();
suspended.addLast(victim);
show("medium", "swap out " + victim);
}
while (!suspended.isEmpty() && used() + need.get(suspended.peekFirst()) <= TOTAL) {
String job = suspended.pollFirst();
ready.addLast(job);
show("medium", "swap in " + job);
}
}
static void finish() {
String job = running;
running = null;
show("exit", job + " finishes");
}
public static void main(String[] args) {
for (String j : fresh) need.put(j, 15);
longTerm();
shortTerm();
finish();
longTerm();
shortTerm();
need.put("J2", 30);
show("grow", "J2 needs 30");
mediumTerm();
shortTerm();
finish();
mediumTerm();
}
}Step through it
Running on 60 frames, jobs J1 to J5 needing 15 frames each; J2 later grows to 30
Medium Term Scheduler
The medium term scheduler removes a whole process from memory when memory is over-committed, writing its pages to swap space, and brings it back once there is room. The removed process is suspended: it keeps its PCB and its place in the system but holds no frames.
This is the cure once thrashing has started. No page replacement policy can help when the working sets of the resident processes add up to more than RAM; the only fix is fewer resident processes, and choosing which one to remove is this scheduler's job.
A good victim is a process that will not run soon: one blocked on slow I/O, or one at the back of the queue. Swapping out a process that is about to run means paying for two full transfers and gaining nothing. See swapping in OS for the cost of each transfer.
- Swap out lowers the degree of multiprogramming; swap in raises it again
- A suspended process keeps its PCB but no memory
- The process model gains two states: suspended ready and suspended blocked
Long Term vs Short Term vs Medium Term Scheduler
Exam questions usually ask for this comparison. Each row is a direct consequence of which transition the scheduler owns: the further a decision reaches, the less often it is made.
| Long term | Short term | Medium term | |
|---|---|---|---|
| Other name | Job scheduler | CPU scheduler | Swapper |
| Moves a process | New → ready | Ready → running | Ready or blocked → suspended, and back |
| How often | Seconds to minutes apart | Every few milliseconds | Only under memory pressure |
| Controls | Degree of multiprogramming | Which process holds the CPU | Which processes stay in memory |
| Speed of the decision | Can be slow | Must be very fast | In between |
| In a time-sharing OS | Minimal or absent | Always present | Present where swapping is used |
- Only the short term scheduler exists on every system
- Long term and medium term both change how many processes are in memory
- Long term acts before a process starts; medium term acts while it runs
Scheduler vs Dispatcher
The short term scheduler only decides which process runs next. The dispatcher carries the decision out: it performs the context switch, switches the CPU to user mode, and jumps to the right instruction in the chosen process.
The time the dispatcher takes to stop one process and start another is the dispatch latency. No useful work happens during it, so the dispatcher holds no policy at all; changing the scheduling algorithm never changes the dispatcher.
- Scheduler = policy; dispatcher = mechanism
- Dispatch latency is pure overhead and is kept as small as possible
- The dispatcher runs every time the short term scheduler picks a new process
Where Real Systems Differ
The three-scheduler model comes from batch systems, and modern desktop and server systems keep only part of it. Linux and Windows start a program the moment it is run, so there is no admission queue; memory pressure is handled mostly by page replacement, with whole-process swapping as a last resort or not used at all.
Mobile systems take the medium term role further: when memory runs out, Android and iOS end background apps outright instead of suspending them to swap. The ideas still hold; only the mechanism changes.
- No long term scheduler on interactive systems
- Swapping whole processes is rare; paging does most of the work
- Batch and HPC job queues (such as Slurm) still do long term admission