The idea: give every resource a priority of its own

Real-time scheduling shows priority inversion with one mutex and how priority inheritance fixes it: the job holding the lock runs at the priority of the job it blocks. With more than one resource, inheritance is not enough. A high-priority job can be blocked once by every lower job, and two jobs that lock in opposite orders can still deadlock. The priority ceiling protocols fix both. Each resource gets a ceiling: the highest priority of any task that uses it. The ceilings are known before the system runs, so the scheduler can refuse a lock that might cause trouble later.

Three tasks H (priority 3), M (2) and L (1) on the left, resources R1 and R2 on the right. H and L use R1, H and M use R2, so both ceilings are 3, the priority of H. A box states the ceiling rule: a lock is granted only if the requester's priority is above the ceilings of all resources held by other jobs
A ceiling is the highest priority that will ever use a resource; here H uses both, so both ceilings are 3.

The page runs three jobs on one CPU with fixed priorities H = 3, M = 2 and L = 1 (higher wins). A job is a list of ticks of work and lock and unlock steps; R1[3] means "hold R1 for 3 ticks". A red cell means the job is ready but a lower-priority job runs: that is blocking, and the "blocked" column counts it. A yellow cell is normal waiting for a higher job.

Chained blocking under inheritance

In scenario A, L locks R1 at tick 1, M preempts it and locks R2 at tick 3, and H arrives at tick 4 needing R1 and then R2. With no protocol, H blocks on R1 and M, which does not even use R1, runs ahead of it: H waits 5 ticks, and a longer M would make it wait longer (Demo: no protocol, unbounded inversion). With inheritance, L runs at priority 3 until it unlocks R1 (ticks 5–6), then H runs, asks for R2, and M has to finish its section at priority 3 (tick 8). H was blocked twice, once by each lower job: 3 ticks here, and up to 3 + 2 = 5 in the worst case (Demo: inheritance, chained blocking). With n lower jobs, H can be blocked n times.

The deadlock inheritance allows

In scenario B, L locks R1 and then R2 inside it, while H locks R2 and then R1: the opposite order. L takes R1 at tick 1. H arrives at tick 2, preempts L and takes R2, which is free. At tick 3 H asks for R1 and blocks; L inherits priority 3, runs, asks for R2 and blocks too. Each waits for the other forever (Demo: inheritance, deadlock with nested locks). Inheritance changes who runs, not who holds what, so it cannot break the cycle. Lock order deadlock shows the same cycle between two threads, and deadlock detection shows how a system finds one after the fact.

Two timelines of scenario B over 10 ticks. With priority inheritance: L runs ticks 0 and 1 and takes R1, H runs tick 2 and takes R2, at tick 3 H waits for R1 and L waits for R2, a deadlock; M runs ticks 3 and 4 and then the CPU idles. With the original priority ceiling: H's request for R2 at tick 2 is refused because L holds R1 with ceiling 3, L runs ticks 2 and 3 at priority 3 taking R2 and releasing both, H runs ticks 4 to 6, M 7 and 8, L 9
The ceiling rule refuses H's first lock at tick 2, although R2 is free; that one refusal is what stops the cycle from forming.

The original priority ceiling protocol

Sha, Rajkumar and Lehoczky (1990) added one rule to inheritance: a job may lock a resource only if its priority is higher than the ceilings of all resources currently locked by other jobs. If not, it blocks, and the job holding that resource inherits its priority. In scenario A, M asks for R2 at tick 3. R2 is free, but L holds R1 with ceiling 3, and M's 2 is not above 3: refused. So when H arrives, only L is in its way. H is blocked 1 tick instead of 3, and its response time drops from 7 to 5 (Demo: priority ceiling, one critical section). In scenario B, H's request for R2 at tick 2 is refused the same way; L takes R2, finishes both sections and lets H go at tick 4 (Demo: priority ceiling, no deadlock). The protocol guarantees two things:

  • A job is blocked for at most the length of one critical section of one lower job: Bi = the longest critical section of a lower job on a resource whose ceiling is at least the priority of task i.
  • No deadlock: once a job holds a resource with ceiling c, no other job with priority ≤ c can lock anything, so no second job can start building the other half of a cycle.

The immediate ceiling protocol

The immediate ceiling protocol (also called highest locker, or priority protect) is simpler to build: a job that locks a resource is raised at once to that resource's ceiling. In scenario A, L runs at priority 3 from tick 1, so M, released at tick 2, cannot even start until L unlocks R1 at tick 4 (Demo: immediate ceiling, block before start). A job is never blocked in the middle; all its blocking happens before it starts, and the bound is the same single critical section. POSIX offers it as PTHREAD_PRIO_PROTECT with pthread_mutexattr_setprioceiling(), and Ada calls it ceiling locking. It costs more priority changes (one at every lock, even when nobody else wants the resource), but needs no wait-for tracking.

Baker's stack resource policy

Baker's stack resource policy (SRP, 1991) moves the test from lock time to start time. Each task has a preemption level π (fixed, from its relative deadline: shorter deadline, higher level), each resource a ceiling in levels, and the system ceiling Π is the highest ceiling of the resources held right now. A job may start, and so preempt, only if it has the highest priority among the ready jobs and its π is above Π. In scenario A, M may not start at tick 2 because L holds R1 and Π = 3 (Demo: stack resource policy, one stack). With fixed priorities the schedule is the same as the immediate ceiling one. Two things are new:

  • A job that has started never blocks, so it only waits for jobs that start after it and finish before it does. All jobs can then run on one shared stack, a big memory saving on small controllers. The resources panel shows the started jobs as a stack: under SRP and immediate ceiling the running job is always on top ✓; under inheritance (ticks 5, 6 and 8 in scenario A) and the original ceiling (ticks 3 and 5) a lower job runs while a higher one sits above it, so each job needs its own stack ✗.
  • Preemption levels do not depend on the current priority, so SRP works the same with dynamic priorities such as earliest deadline first, where a job's priority changes at every release.

Blocking time in response-time analysis

Response-time analysis finds the worst response Ri by iteration. With shared resources it adds the blocking bound Bi, the longest time a lower job can hold task i up:

Ri=Ci+Bi+∑j∈hp(i)⌈RiTj⌉Cj

Give scenario A's tasks periods T = 8, 20 and 40 (C = 4, 5 and 5, U = 0.875). The lower critical sections are L·R1 = 3 ticks and M·R2 = 2 ticks (Demo: blocking bound in response-time analysis):

protocolBHRH (T = 8)BMRM (T = 20)H in the schedule
no protocolunboundedno bound ✗unboundedno bound ✗blocked 5, response 9
priority inheritance3 + 2 = 54 + 5 = 9 ✗312 → 16 → 16 ✓blocked 3, response 7
original ceilingmax(3, 2) = 34 + 3 = 7 ✓316 ✓blocked 1, response 5
immediate ceiling37 ✓316 ✓blocked 0, response 4
SRP37 ✓316 ✓blocked 0, response 4

L has no lower job, so BL = 0 and RL = 14 → 18 → 22 → 27 → 31 → 31 ≤ 40 under every bounded protocol. The task set fails under inheritance only because of chained blocking: the protocol, not the CPU load, decides. M gets B = 3 under every protocol because L's section on R1 can push in front of it (by inheritance or by the ceiling). The bound is a worst case; on this one release pattern H was blocked less (3, 1 and 0 ticks).

See also the futex mutex (how a lock puts a thread to sleep and wakes it), real-time scheduling (SCHED_FIFO, priority inversion and the Mars Pathfinder) and rate-monotonic scheduling (the utilization bound and response-time analysis without blocking).

See also the cyclic executive, where slices never overlap, so shared data needs no locks at all.

What the page leaves out

One CPU, integer ticks, one job per task, and locks and priority changes that take no time. Multiprocessor protocols (MPCP, MSRP, FMLP) and spinning on another core are left out, as are nested inheritance chains longer than two, multi-unit resources (SRP handles them, the page has one unit each), and the Linux kernel's rt_mutex, which implements inheritance only. Real analyses also add jitter, context-switch and interrupt costs to the formula.

On the mind map

Open the Operating Systems mind mapOperatingSystemsBoot and program start: from power on to main()Boot and program startProcesses and the kernel: how user code talks to the kernelProcesses and the kernelInterrupts and devices: hardware interrupts the CPUInterrupts and devicesScheduling and deadlock: who runs next, who gets whatScheduling and deadlockVirtual memory: every address is translatedVirtual memoryPhysical memory: who gets which bytes of RAMPhysical memoryFiles and storage: from open() down to the diskFiles and storageDisks and RAID: blocks down to platter or flashDisks and RAIDIsolation and security: limit what a process can doIsolation and securityContext switchContext switchswitch_mm loads CR3, switch_to swaps stacksCPU scheduling algorithmsCPU scheduling algorithmsFCFS, SJF, RR, CFS: compare waiting timeMultiprocessor SchedulingMultiprocessor SchedulingPer-CPU queues + stealing beat one lockMulti-Level Feedback QueueMulti-Level Feedback QueueUse your quantum, drop a levelLottery & strideLottery & strideTickets set shares; stride runs lowest passPreemptive vs cooperativePreemptive vs cooperativetimer forces a switch, or wait for yieldReal-time schedulingReal-time schedulingpriority inversion, fixed by inheritanceRate-monotonic analysisRate-monotonic analysisShort period = high priority; RTA decidesPriority ceiling and SRPOne lower critical section, no deadlockEDF vs Least SlackEDF vs Least SlackEDF: any set with U ≤ 1; overload dominoesTime-triggered schedulerTime-triggered schedulerISR only flags; super loop runs, then sleepsCyclic executiveCyclic executivea timer runs a fixed frame tableSuper loop to RTOSSuper loop to RTOSUART worst: 13, 13, 8, 1 msLoad averageLoad averagedamped count of R and D tasksBanker's algorithmBanker's algorithmgrant only if a safe order still existsDeadlock detectionDeadlock detectionFind the cycle, then kill a victimOpen the Operating Systems mind mapOperating Systemswho runs next, who gets whatScheduling and deadlockContext switchContext switchswitch_mm loads CR3, switch_to swaps stacksCPU scheduling algorithmsCPU scheduling algorithmsFCFS, SJF, RR, CFS: compare waiting timeMultiprocessor SchedulingMultiprocessor SchedulingPer-CPU queues + stealing beat one lockMulti-Level Feedback QueueMulti-Level Feedback QueueUse your quantum, drop a levelLottery & strideLottery & strideTickets set shares; stride runs lowest passPreemptive vs cooperativePreemptive vs cooperativetimer forces a switch, or wait for yieldReal-time schedulingReal-time schedulingpriority inversion, fixed by inheritanceRate-monotonic analysisRate-monotonic analysisShort period = high priority; RTA decidesPriority ceiling and SRPOne lower critical section, no deadlockEDF vs Least SlackEDF vs Least SlackEDF: any set with U ≤ 1; overload dominoesTime-triggered schedulerTime-triggered schedulerISR only flags; super loop runs, then sleepsCyclic executiveCyclic executivea timer runs a fixed frame tableSuper loop to RTOSSuper loop to RTOSUART worst: 13, 13, 8, 1 msLoad averageLoad averagedamped count of R and D tasksBanker's algorithmBanker's algorithmgrant only if a safe order still existsDeadlock detectionDeadlock detectionFind the cycle, then kill a victim
This page on the Operating Systems mind map · 78 pages in 9 branches · open it to see the whole map and test yourself in recall mode.