Simvik

Response-time analysis

Compute worst-case response times under fixed-priority scheduling and check every deadline. A hard embedded C problem (RTOS scheduling), run and graded in…

hard · RTOS scheduling · rtos · rate-monotonic · schedulability

Periodic tasks run under preemptive fixed-priority scheduling; tasks[0] has the highest priority. A task's worst-case response time R is its own execution time plus the time stolen by every higher-priority task released while it waits:

R = C_i + sum over j < i of ceil(R / T_j) * C_j

R appears on both sides, so iterate: start with R = C_i and recompute until the value stops changing. If it ever exceeds the task's period, the task misses its deadline.

Fill response[i] with each task's worst-case response time, or 0 if it misses its deadline. Return true if every task meets its deadline.

Starter code

bool rm_schedulable(const rt_task_t *tasks, size_t n, uint32_t *response) {
  bool ok = true;
  for (size_t i = 0; i < n; i++) {
    uint32_t r = tasks[i].c;
    for (size_t j = 0; j < i; j++) r += tasks[j].c;   // one preemption each
    response[i] = r <= tasks[i].t ? r : 0;
    if (r > tasks[i].t) ok = false;
  }
  return ok;
}

Example tests

A schedulable set

rt_task_t t[] = { { 1, 4 }, { 2, 6 }, { 3, 12 } };
check(t, 3);

Expected output:

1 1 3 10

An overloaded set

rt_task_t t[] = { { 2, 4 }, { 3, 6 }, { 1, 8 } };
check(t, 3);

Expected output:

0 2 0 0

Learn it first

More RTOS scheduling problems