Priority inversion, tick by tick
Simulate a preemptive scheduler with a shared mutex, and see what priority inheritance changes. A hard embedded C problem (RTOS scheduling), run and graded…
hard · RTOS scheduling · rtos · scheduling · mutex · priority-inversion · simulation
Write simulate(t, n, inherit, ticks, out): run n tasks on one CPU for ticks ticks under fixed-priority preemptive scheduling, with one mutex, and write the letter of the task that ran in each tick to out (. if none), then a terminating '\0'.
First reset each task's state: pc 0, eff = prio, not blocked, done if it has no steps. Then for each tick:
- Pick the task with the highest
effamong those released (release <= tick), not done and not blocked. On a tie, the lowest index. If there is none, the tick is idle. - Look at its next step:
L: if another task owns the mutex, this task becomes blocked. Withinherit, the owner'seffis raised to this task'seffif it is lower. Go back to 1 and pick again in the same tick. Otherwise this task becomes the owner, moves to the next step, and you go back to 1.U: release the mutex, set this task'seffback to itsprio, unblock every blocked task (each tries itsLagain when it next runs), move to the next step, go back to 1.c: the task runs for this tick. Move to the next step, and the tick is over.
After any step, a task with no steps left is done.
Starter code
void simulate(task_t *t, int n, bool inherit, int ticks, char *out) {
for (int i = 0; i < n; i++) { t[i].pc = 0; t[i].eff = t[i].prio; t[i].blocked = false; t[i].done = false; }
for (int now = 0; now < ticks; now++) {
out[now] = '.';
int run = -1;
for (int i = 0; i < n; i++)
if (!t[i].done && t[i].release <= now && (run < 0 || t[i].prio > t[run].prio)) run = i;
if (run < 0) continue;
task_t *k = &t[run];
while (k->steps[k->pc] == 'L' || k->steps[k->pc] == 'U') k->pc++; // the mutex is ignored
out[now] = k->name;
k->pc++;
if (k->steps[k->pc] == '\0') k->done = true;
}
out[ticks] = '\0';
}Example tests
The classic inversion
task_t t[] = {
{ 'L', 1, 0, "cLcccUc" },
{ 'M', 2, 3, "ccccc" },
{ 'H', 3, 2, "cLcUc" },
};
show(t, 3, 13);Expected output:
plain: LLHMMMMMLLHHL
inherit: LLHLLHHMMMMMLLearn it first
- RTOS scheduling and timing · Preemption, context switches, ticks and rate-monotonic analysis.
More RTOS scheduling problems
- Drift-free periodic wake-up · Compute how long a periodic task should sleep so its period never drifts.
- Response-time analysis · Compute worst-case response times under fixed-priority scheduling and check every deadline.
- Find the highest ready priority · Pick the highest priority from a 32-bit ready bitmap.
- Rate-monotonic utilisation test · Decide from CPU utilisation whether a task set is schedulable under rate-monotonic priorities.
- Priorities with round-robin · Choose the next task to run, sharing the CPU in turns among tasks of equal priority.
- Earliest-deadline-first schedule · Simulate an EDF scheduler tick by tick, draw the timeline and count missed deadlines.
- Ventilator: will every task meet its deadline? · Prove the ventilator's tasks are schedulable with response-time analysis, not just a utilisation total.