Find the highest ready priority
Pick the highest priority from a 32-bit ready bitmap. An easy embedded C problem (RTOS scheduling), run and graded in the browser.
easy · RTOS scheduling · rtos · scheduler · bits
Many RTOS kernels keep one bit per priority level: bit p is set when some task at priority p is ready. Higher numbers are more important.
Return the highest priority with its bit set, or -1 if no bit is set.
Starter code
int8_t highest_ready(uint32_t ready) {
for (int8_t p = 0; p < 32; p++)
if (ready & (1u << p)) return p;
return -1;
}Example tests
Two ready priorities
printf("%d\n", highest_ready(0x00000012u));Expected output:
4The top and bottom bits
printf("%d\n", highest_ready(0x80000001u));Expected output:
31Learn 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.
- 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.
- Priority inversion, tick by tick · Simulate a preemptive scheduler with a shared mutex, and see what priority inheritance changes.
- Ventilator: will every task meet its deadline? · Prove the ventilator's tasks are schedulable with response-time analysis, not just a utilisation total.