Simvik

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 current in index order, wrapping around, so they take turns (round-robin);
  • current is -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:

1212

All equal

const tcb_t t[] = { { 1, true }, { 1, true }, { 1, true } };
schedule(t, 3, 4);

Expected output:

0120

Learn it first

More RTOS scheduling problems