Lesson 13 · Processes, threads, and CPU scheduling

Earliest Deadline First Scheduling

Always run the ready job whose deadline is nearest, and switch the moment a more urgent job arrives. On one CPU that meets every deadline whenever the work fits at all, up to 100% utilisation. The price is what happens when it does not fit: the misses spread to every task.

Earliest Deadline First Scheduling concept diagramA visual explanation of the layout and operations shown in this lesson.priority is whichever deadline is nearest — so it changes as time passesT2released 5deadline 10needs 2T1released 6deadline 9needs 1CPUT2T1T2024681012at t=6 T1 arrives with the nearer deadline (9 < 10), so it preempts T2 mid-executionschedulable whenever U ≤ 1 — rate-monotonic gives up near 0.69 for many tasks
1

How EDF Picks the Next Job

  1. When a job is released, compute its absolute deadline: release time + relative deadline
  2. Insert it into the ready queue, ordered by absolute deadline
  3. If its deadline is sooner than the running job's, preempt the running job
  4. When a job finishes, run the job at the head of the queue

Earliest deadline first scheduling gives the CPU to the ready job with the nearest absolute deadline. A job released at time 30 with 10 ticks to finish has absolute deadline 40, and 40 is the number compared. Nothing else counts: not the task's period, not its arrival order, not how long it runs.

Because every new job brings a new deadline, priority belongs to jobs, not tasks. Task C can outrank task B at one moment and sit behind it a few ticks later. That is the difference from rate monotonic and deadline monotonic scheduling, which fix a rank per task before the system starts.

The EDF scheduling algorithm is preemptive: it decides again whenever a job arrives or finishes. The ready set is kept ordered by deadline, in a heap or a balanced tree, so each decision costs O(log n).

  • Priority is recomputed per job, not set once per task
  • Ties may be broken any way; every order meets the same deadlines
2

The U ≤ 1 Schedulability Test

Each periodic task needs C ticks of CPU every T ticks, so it uses the fraction C/T. Add these up and you get the utilisation U. Liu and Layland proved in 1973 that when every deadline equals its period, EDF meets every deadline if and only if U ≤ 1. It is both necessary and sufficient: if the work fits in the CPU at all, EDF fits it, so no other algorithm on one processor can do better.

The shared task set on this page and the deadline monotonic lesson is A (1 tick every 4), B (2 every 6) and C (3 every 8). U = 1/4 + 2/6 + 3/8 = 23/24 ≈ 0.958, so EDF is guaranteed safe. The fixed-priority bound for three tasks, 3(2^(1/3) − 1) ≈ 0.780, is far below that, and on this set fixed priority does miss.

The test rests on assumptions, and each one matters on an exam: one CPU, independent tasks that never block on each other, full preemption, and switching cost treated as zero. When some deadline is shorter than its period, U ≤ 1 is still required but no longer enough. You then check processor demand: for every interval length L, the work that must finish within L has to fit in L.

  • U = sum of C/T over all tasks
  • Deadlines equal periods: U ≤ 1 is exact
  • Deadlines shorter than periods: check demand over every interval
3

Worked Example: Three Tasks at 96%

All three release at t = 0 with deadlines 4, 6 and 8, so A runs, then B for two ticks, then C starts at 3. At 4 a new A job ties C at deadline 8; either may go, and A does.

The tick that matters is t = 6. B releases a job due at 12 while C still has one tick left, due at 8. EDF keeps C, which finishes at 7. A fixed-priority scheduler ranks B above C, switches to B here, and C finishes at 10, two ticks late.

The rest of the 24-tick hyperperiod (lcm of 4, 6 and 8) follows the same rule. All 13 jobs finish on time and the CPU is idle for exactly one tick, which is the 1/24 the task set leaves unused. The tracer above runs every tick, then the same set under fixed priority, then an overload.

  • Schedule: A B B C A C C B A B C C A C B B A C B B A C C, then one idle tick
  • Fixed priority on the same set: C late at 8
Implementation

EDF on one task set, then fixed priority and overload

def edf(tasks, horizon):
    jobs, order, missed = [], [], []
    for t in range(horizon):
        for i, (name, c, period) in enumerate(tasks):
            if t % period == 0:
                jobs.append([t + period, i, c, name])  # absolute deadline, tie-break, work left
        ready = [j for j in jobs if j[2] > 0]
        if ready:
            job = min(ready)  # nearest absolute deadline runs, even if it preempts
            job[2] -= 1
            order.append(job[3])
        else:
            order.append(".")
        for j in jobs:
            if j[0] == t + 1 and j[2] > 0:
                missed.append(f"{j[3]}@{j[0]}")  # late, yet still the earliest deadline
    return "".join(order), missed


def report(tasks, horizon):
    u = sum(c / period for _, c, period in tasks)
    print(f"U = {u:.3f}, EDF test U <= 1: {'pass' if u <= 1 else 'fail'}")
    order, missed = edf(tasks, horizon)
    print("EDF:", order)
    print("missed:", " ".join(missed) or "none")


tasks = [("A", 1, 4), ("B", 2, 6), ("C", 3, 8)]
report(tasks, 24)
tasks[2] = ("C", 4, 8)  # one extra tick of work pushes U past 1
report(tasks, 48)
#include <cstdio>
#include <string>
#include <vector>

struct Task { char name; int c, period; };
struct Job { int deadline, index, left; char name; };

void report(const std::vector<Task>& tasks, int horizon) {
    double u = 0;
    for (const Task& k : tasks) u += double(k.c) / k.period;
    std::printf("U = %.3f, EDF test U <= 1: %s\n", u, u <= 1 ? "pass" : "fail");
    std::vector<Job> jobs;
    std::string order, missed;
    for (int t = 0; t < horizon; t++) {
        for (int i = 0; i < (int)tasks.size(); i++)
            if (t % tasks[i].period == 0)
                jobs.push_back({t + tasks[i].period, i, tasks[i].c, tasks[i].name});
        Job* best = nullptr;
        for (Job& j : jobs)  // nearest absolute deadline runs, even if it preempts
            if (j.left > 0 && (!best || j.deadline < best->deadline ||
                               (j.deadline == best->deadline && j.index < best->index)))
                best = &j;
        if (best) { best->left--; order += best->name; }
        else order += '.';
        for (const Job& j : jobs)
            if (j.deadline == t + 1 && j.left > 0)  // late, yet still the earliest deadline
                missed += (missed.empty() ? "" : " ") + std::string(1, j.name) + "@" + std::to_string(j.deadline);
    }
    std::printf("EDF: %s\nmissed: %s\n", order.c_str(), missed.empty() ? "none" : missed.c_str());
}

int main() {
    std::vector<Task> tasks = {{'A', 1, 4}, {'B', 2, 6}, {'C', 3, 8}};
    report(tasks, 24);
    tasks[2].c = 4;  // one extra tick of work pushes U past 1
    report(tasks, 48);
}
import java.util.ArrayList;
import java.util.List;

public class Main {
    static void report(char[] names, int[] c, int[] period, int horizon) {
        double u = 0;
        for (int i = 0; i < c.length; i++) u += (double) c[i] / period[i];
        System.out.printf("U = %.3f, EDF test U <= 1: %s%n", u, u <= 1 ? "pass" : "fail");
        List<int[]> jobs = new ArrayList<>();  // {absolute deadline, task index, work left}
        StringBuilder order = new StringBuilder();
        List<String> missed = new ArrayList<>();
        for (int t = 0; t < horizon; t++) {
            for (int i = 0; i < c.length; i++)
                if (t % period[i] == 0) jobs.add(new int[]{t + period[i], i, c[i]});
            int[] best = null;
            for (int[] j : jobs)  // nearest absolute deadline runs, even if it preempts
                if (j[2] > 0 && (best == null || j[0] < best[0] || (j[0] == best[0] && j[1] < best[1])))
                    best = j;
            if (best != null) { best[2]--; order.append(names[best[1]]); }
            else order.append('.');
            for (int[] j : jobs)
                if (j[0] == t + 1 && j[2] > 0) missed.add(names[j[1]] + "@" + j[0]);  // late, yet still the earliest deadline
        }
        System.out.println("EDF: " + order);
        System.out.println("missed: " + (missed.isEmpty() ? "none" : String.join(" ", missed)));
    }

    public static void main(String[] args) {
        char[] names = {'A', 'B', 'C'};
        int[] period = {4, 6, 8};
        report(names, new int[]{1, 2, 3}, period, 24);
        report(names, new int[]{1, 2, 4}, period, 48);  // one extra tick of work pushes U past 1
    }
}
Watch it run

Step through it

Running on A (1 every 4), B (2 every 6), C (3 every 8) · deadline = period · U = 23/24

A: 1 tick every 4 · B: 2 every 6 · C: 3 every 8ABC024681012141618202224U = 0.250 + 0.333 + 0.375 = 0.958EDF test: U ≤ 1 → passfixed-priority bound for 3 tasks: 0.780 → says nothinghyperperiod lcm(4, 6, 8) = 24 ticks: then the pattern repeats
Step 1

Three tasks, 96% of the CPU

Each task releases a job every period and that job must finish before the next release, so the deadline ticks on each row are also release times. Utilisation is 1/4 + 2/6 + 3/8 = 23/24. EDF's test is exact for this kind of set: U ≤ 1 means every deadline will be met. The fixed-priority bound for three tasks is 0.780, so it cannot vouch for this set at all.

Output
1 / 11
4

EDF vs Rate Monotonic

EDF against rate monotonic is the comparison most courses ask for. Rate monotonic gives the highest fixed priority to the shortest period. It is optimal among fixed-priority rules when deadlines equal periods, but no fixed ranking can match EDF's 100%: the simple Liu and Layland test only promises safety up to about 69% for many tasks, and sets above that need exact response time analysis, which can still fail, as it does on the shared set.

So why do so many real systems use fixed priority anyway? Because of what happens when the load estimate is wrong, and because its analysis is the one certification tools and auditors already know.

Earliest deadline first vs rate monotonic on one CPU
Earliest deadline firstRate monotonic
PriorityPer job, by absolute deadlinePer task, by period, fixed
Guaranteed up toU = 1 (deadline = period)U ≤ n(2^(1/n) − 1), about 0.69 for many tasks; beyond that, exact analysis
Shared set, U = 0.958All deadlines metC misses at t = 8
OverloadMisses spread to every taskOnly the lowest-ranked tasks miss
Run-time costDeadline-ordered queue, more preemptionsFixed-rank lookup
  • EDF: more work fits in the CPU
  • Rate monotonic: predictable damage when it does not
5

Overload and the Domino Effect

Give task C a fourth tick of work and U becomes 26/24 ≈ 1.083: some job must be late. Under EDF the first late job, C's job due at 24, keeps its old deadline. That deadline is now the earliest of all, so the late job runs ahead of jobs that could still have made it, and they go late too. In 48 ticks C misses four times, B twice and A, the most urgent task, once.

Fixed priority on the same overload sacrifices only C, every period, and A and B are never late. That is why systems where a missed deadline is dangerous usually prefer fixed priority, and why EDF systems guard against overload instead of trusting the load estimate.

Linux does exactly that. Its SCHED_DEADLINE class, added in kernel 3.14, schedules by EDF, but each task declares a runtime, deadline and period, the kernel refuses a task that would push the total past what the CPUs can serve, and a budget server throttles any task that overruns its runtime. On several CPUs the U ≤ 1 result no longer holds directly; see multiprocessor scheduling.

  • A late job holds the earliest deadline, so it keeps the CPU
  • Fixed priority sheds only the lowest-ranked task
  • Real EDF systems refuse new tasks rather than overload