---
title: Problem set 8
heading: "Problem set 8: state machines, scheduling and real time"
eyebrow: C12 · Problem set 8
type: problems
---

::: hook
These problems use lessons 36 to 39: state machines, superloops and cooperative scheduling, preemptive scheduling, and latency, jitter and deadlines. Response times under fixed priorities follow $R = C + B + \sum \lceil R/T_j\rceil C_j$ over the higher-priority tasks, where B is the longest time the task can be blocked by a lower one; deadlines equal periods unless a problem says otherwise.
:::

::: pool
pick: 12

::: check
kind: parts
given: s = 5 | 8 | 12; e = 4 | 6; o = 1 | 2

## A table-driven state machine has {s} states and {e} events; each entry holds a next state (1 byte) and an action number (1 byte), and each state has {o} byte(s) of output settings.

::: part
answer: s*e*2 + s*o
unit: bytes
tolerance: 0

### How much flash do the tables take?

::: misses
- [s*e*2] Add the per-state outputs.
- [any] Transitions × 2 bytes + states × outputs.
:::
:::

::: part
answer: s*e
tolerance: 0

### How many tests cover every transition once?

::: misses
- [any] One per state-event pair.
:::
:::
:::

::: check
kind: parts
given: o = 3 | 5; c = 2 | 4; k = 1 | 2 | 3

## A door controller holds the door open for {o} s, then closes it over {c} s. An obstruction while closing reopens it (taking as long as it had been closing) and restarts the hold. Someone blocks it {k} time(s), each just before it would finish closing.

::: part
answer: o + c + k*(c + o + c)
unit: s
tolerance: 0.5%

### How long from when the door is first fully open until it's finally shut?

::: misses
- [o + c] Each obstruction costs a reopening, a new hold and a new close.
- [any] Hold + close, plus {k} × (reopen + hold + close).
:::
:::

::: part
answer: 2*k + 2
tolerance: 0

### How many times does the motor start, counting the first opening (each open or close is one start)?

::: misses
- [any] One open and one close, plus a reopen and a close for each obstruction.
:::
:::
:::

::: check
kind: parts
given: a = 0.5 | 1; b = 2 | 5; c = 10 | 30

## A superloop runs three tasks in turn, taking at most {a}, {b} and {c} ms.

::: part
answer: b + c
unit: ms
tolerance: 0.5%

### What is the longest the first task can wait for its next turn after finishing?

::: misses
- [any] The other two at their longest.
:::
:::

::: part
answer: a + b + c/4
unit: ms
tolerance: 0.5%

### If the third task is split into four equal slices, one per turn, what does that wait become for the second task?

::: misses
- [any] The first task and one slice.
:::
:::
:::

::: check
kind: parts
given: C1 = 1 | 2; T1 = 4 | 5; C2 = 2 | 3; C3 = 4 | 6; B = 1 | 2; let r0 = C3 + B + C1 + C2; let r1 = C3 + B + ceil(r0/T1)*C1 + ceil(r0/20)*C2; let r2 = C3 + B + ceil(r1/T1)*C1 + ceil(r1/20)*C2; let r3 = C3 + B + ceil(r2/T1)*C1 + ceil(r2/20)*C2; let r4 = C3 + B + ceil(r3/T1)*C1 + ceil(r3/20)*C2

## Task 1 runs {C1} ms every {T1} ms and task 2 runs {C2} ms every 20 ms, both above task 3, which needs {C3} ms and can be blocked for {B} ms by a lower task holding a shared mutex (with priority inheritance).

::: part
answer: r4
unit: ms
tolerance: 0

### What is task 3's worst-case response time?

::: misses
- [C3 + B + C1 + C2] The higher tasks may be released again before task 3 finishes: iterate.
- [any] Iterate $R = C_3 + B + \lceil R/T_1\rceil C_1 + \lceil R/20\rceil C_2$.
:::
:::

::: part
answer: (C1/T1 + C2/20)*100
unit: %
tolerance: 0.5%

### What share of the processor do the two higher tasks use?

::: misses
- [any] $C_1/T_1 + C_2/T_2$.
:::
:::
:::

::: check
kind: parts
given: C1 = 1 | 3; T1 = 10 | 20; C2 = 5 | 10; T2 = 40 | 50; C3 = 10 | 20; T3 = 100 | 200

## Three tasks: {C1} ms every {T1} ms, {C2} ms every {T2} ms, {C3} ms every {T3} ms, scheduled rate-monotonically.

::: part
answer: (C1/T1 + C2/T2 + C3/T3)*100
unit: %
tolerance: 0.5%

### What is the utilization?

::: misses
- [any] $\sum C_i/T_i$.
:::
:::

::: part
answer: (3*(2^(1/3) - 1) - (C1/T1 + C2/T2 + C3/T3))*100
unit: %
tolerance: 0.5%

### How much headroom is left under the three-task bound (negative if over it)?

::: misses
- [any] 78.0% less the utilization.
:::
:::
:::

::: check
kind: parts
given: C = 2 | 5; s = 20 | 50; f = 100 | 500

## A task runs {C} ms of work {f} times a second; each run costs two context switches of {s} µs.

::: part
answer: (C + 2*s/1000)*f/1000*100
unit: %
tolerance: 0.5%

### What share of the processor does it take?

::: misses
- [C*f/1000*100] Include the two switches per run.
- [any] (work + switches) × rate.
:::
:::

::: part
answer: 2*s/1000*f/1000*100
unit: %
tolerance: 0.5%

### How much of that is switching?

::: misses
- [any] Switch time × rate.
:::
:::
:::

::: check
kind: parts
given: L = 50 | 200; C = 300 | 800; d = 20 | 100; D = 1000 | 1500

## An interrupt takes {L} µs to reach its handler, interrupts can be disabled for up to {d} µs elsewhere, and the handler's job takes {C} µs and must finish within {D} µs of the event.

::: part
answer: d + L + C
unit: µs
tolerance: 0.5%

### What is the worst-case completion time?

::: misses
- [L + C] A disabled stretch may delay the start.
- [any] Disabled time + latency + execution.
:::
:::

::: part
answer: D - (d + L + C)
unit: µs
tolerance: 0.5%

### What is the slack (negative if it misses)?

::: misses
- [any] Deadline less completion.
:::
:::
:::

::: check
kind: parts
given: T = 1 | 10; d = 5 | 25 | 47

## An RTOS counts time in ticks of {T} ms. A task asks to sleep {d} ms, rounded up to whole ticks, starting at an unknown point within the current tick.

::: part
answer: (ceil(d/T) - 1)*T
unit: ms
tolerance: 0.5%

### What is the shortest it can actually sleep?

::: misses
- [d] The current tick is partly gone already.
- [any] $(\lceil d/T\rceil - 1)T$.
:::
:::

::: part
answer: ceil(d/T)*T
unit: ms
tolerance: 0.5%

### And the longest?

::: misses
- [any] $\lceil d/T\rceil T$.
:::
:::
:::

::: check
kind: parts
given: r = 500 | 2000; B = 10 | 40; s = 4 | 16

## An ISR puts {s}-byte readings into a queue {r} times a second; the consumer task can be kept from running for up to {B} ms by higher-priority work.

::: part
answer: ceil(r*B/1000)
tolerance: 0

### How many entries must the queue hold?

::: misses
- [any] Arrivals during the longest wait, rounded up.
:::
:::

::: part
answer: ceil(r*B/1000)*s
unit: bytes
tolerance: 0

### How much RAM is that?

::: misses
- [any] Entries × size.
:::
:::
:::

::: check
kind: parts
given: f = 50 | 1000; A = 1 | 1.65; j1 = 200 | 1000; j2 = 1 | 2

## A {f} Hz sine of amplitude {A} V is sampled either in a polling loop with {j1} µs of timing jitter, or from a timer interrupt with {j2} µs.

::: part
answer: 2*pi*f*A*j1*1e-6*1000
unit: mV
tolerance: 0.5%

### What is the polling loop's worst-case sampling error?

::: misses
- [any] $2\pi fA\Delta t$.
:::
:::

::: part
answer: j1/j2
tolerance: 0.5%

### By what factor does the interrupt reduce it?

::: misses
- [any] The error is proportional to the jitter.
:::
:::
:::

::: check
kind: parts
given: P = 10 | 100; t = 20 | 50; I = 2 | 5

## A device polls a sensor every {P} ms, waking for {t} µs at {I} mA each time; an interrupt-driven design instead sleeps until the sensor signals, about once a minute, for the same {t} µs.

::: part
answer: I*t*1e-6*(1000/P)*1000
unit: µA
tolerance: 0.5%

### What average current does polling add?

::: misses
- [any] Current × awake time × wakes per second.
:::
:::

::: part
answer: (1000/P)*60
tolerance: 0.5%

### How many times more often does it wake than the interrupt design?

::: misses
- [any] Polls per minute.
:::
:::
:::

::: check
kind: order

## Five tasks have periods of 5, 8, 20, 50 and 100 ms. Order them from highest to lowest priority under rate-monotonic scheduling.

1. The 5 ms task
2. The 8 ms task
3. The 20 ms task
4. The 50 ms task
5. The 100 ms task

::: misses
- [any] Shorter period, higher priority.
:::
:::

::: check
kind: parts
given: n = 3 | 5 | 8; f = 256 | 512; i = 64 | 128; R = 16 | 32

## An RTOS runs {n} tasks; each stack must hold its deepest call chain ({f} bytes) plus one interrupt frame ({i} bytes), on a chip with {R} KB of RAM.

::: part
answer: n*(f + i)
unit: bytes
tolerance: 0

### How much RAM do the stacks take?

::: misses
- [n*f] Every task's stack must also absorb an interrupt.
- [any] Tasks × (chain + interrupt frame).
:::
:::

::: part
answer: n*(f + i)/(R*1024)*100
unit: %
tolerance: 0.5%

### What share of RAM is that?

::: misses
- [any] Over $R \times 1024$.
:::
:::
:::

::: check
kind: parts
given: c = 0.5 | 2; m = 10 | 50; l = 3 | 8

## A high-priority task can be blocked by a low-priority task holding a mutex for {c} ms; a medium task runs {m} ms; another low task holds a second mutex the high task also needs, for {l} ms.

::: part
answer: c + l
unit: ms
tolerance: 0.5%

### With priority inheritance, how long can the high task be blocked at worst, if it may meet both mutexes held?

::: misses
- [c + l + m] Inheritance keeps the medium task out.
- [any] Each mutex's critical section, once.
:::
:::

::: part
answer: c + l + m
unit: ms
tolerance: 0.5%

### And without it, if the medium task runs once while one of the mutexes is held?

::: misses
- [any] The medium task can preempt while a mutex is held.
:::
:::
:::

::: check
kind: parts
given: w = 2 | 8; p = 50 | 200; n = 3 | 6

## A cooperative scheduler runs {n} tasks that each yield every {w} ms at most. A button press must get a response within {p} ms.

::: part
answer: (n - 1)*w
unit: ms
tolerance: 0.5%

### What is the worst-case wait before the button task runs again?

::: misses
- [n*w] The other {n} − 1 tasks run first.
- [any] Every other task's longest slice.
:::
:::

::: part
answer: step(p - (n - 1)*w)
tolerance: 0

### Does it meet the response time (1 for yes)?

::: misses
- [any] Compare with {p} ms.
:::
:::
:::
:::
