Priorities with round-robin
Choose the next task to run, sharing the CPU in turns among tasks of equal priority. A medium embedded C problem (RTOS scheduling), run and graded in the…
medium · RTOS scheduling · rtos · scheduler
pick_next(tasks, n, current) chooses the task to run next:
- the ready task with the highest priority;
- among several ready tasks of that priority, the first one after
currentin index order, wrapping around, so they take turns (round-robin); currentis -1 before any task has run: then search from index 0;- return -1 if no task is ready.
Starter code
int pick_next(const tcb_t *tasks, int n, int current) {
return (current + 1) % n;
}Example tests
Two top-priority tasks take turns
const tcb_t t[] = { { 1, true }, { 2, true }, { 2, true }, { 1, true } };
schedule(t, 4, 4);Expected output:
1212All equal
const tcb_t t[] = { { 1, true }, { 1, true }, { 1, true } };
schedule(t, 3, 4);Expected output:
0120Learn 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.
- 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.