C12 · Problem set 8
Problem set 8: state machines, scheduling and real time
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 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.
Check
A table-driven state machine has 8 states and 4 events; each entry holds a next state (1 byte) and an action number (1 byte), and each state has 2 byte(s) of output settings.
(a)
How much flash do the tables take?
(b)
How many tests cover every transition once?
Check
A door controller holds the door open for 3 s, then closes it over 2 s. An obstruction while closing reopens it (taking as long as it had been closing) and restarts the hold. Someone blocks it 3 time(s), each just before it would finish closing.
(a)
How long from when the door is first fully open until it's finally shut?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
(b)
How many times does the motor start, counting the first opening (each open or close is one start)?
Check
A superloop runs three tasks in turn, taking at most 0.5, 2 and 30 ms.
(a)
What is the longest the first task can wait for its next turn after finishing?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
If the third task is split into four equal slices, one per turn, what does that wait become for the second task?
Check
Task 1 runs 1 ms every 4 ms and task 2 runs 3 ms every 20 ms, both above task 3, which needs 6 ms and can be blocked for 1 ms by a lower task holding a shared mutex (with priority inheritance).
(a)
What is task 3's worst-case response time?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
What share of the processor do the two higher tasks use?
Check
Three tasks: 1 ms every 10 ms, 10 ms every 50 ms, 10 ms every 100 ms, scheduled rate-monotonically.
(a)
What is the utilization?
(b)
How much headroom is left under the three-task bound (negative if over it)?
Check
A task runs 2 ms of work 500 times a second; each run costs two context switches of 20 µs.
(a)
What share of the processor does it take?
(b)
How much of that is switching?
Check
An interrupt takes 50 µs to reach its handler, interrupts can be disabled for up to 100 µs elsewhere, and the handler's job takes 300 µs and must finish within 1500 µs of the event.
(a)
What is the worst-case completion time?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
(b)
What is the slack (negative if it misses)?
Check
An RTOS counts time in ticks of 1 ms. A task asks to sleep 5 ms, rounded up to whole ticks, starting at an unknown point within the current tick.
(a)
What is the shortest it can actually sleep?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
And the longest?
Check
An ISR puts 16-byte readings into a queue 500 times a second; the consumer task can be kept from running for up to 10 ms by higher-priority work.
(a)
How many entries must the queue hold?
(b)
How much RAM is that?
Check
A 50 Hz sine of amplitude 1 V is sampled either in a polling loop with 1000 µs of timing jitter, or from a timer interrupt with 2 µs.
(a)
What is the polling loop's worst-case sampling error?
Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.
(b)
By what factor does the interrupt reduce it?
Check
A device polls a sensor every 10 ms, waking for 20 µs at 5 mA each time; an interrupt-driven design instead sleeps until the sensor signals, about once a minute, for the same 20 µs.
(a)
What average current does polling add?
Write units the way you normally would: 1.5 µA, 1500 nA and 0.0015 mA are the same answer.
(b)
How many times more often does it wake than the interrupt design?
Check
Five tasks have periods of 5, 8, 20, 50 and 100 ms. Order them from highest to lowest priority under rate-monotonic scheduling.
- The 100 ms task
- The 5 ms task
- The 20 ms task
- The 50 ms task
- The 8 ms task
Check
An RTOS runs 5 tasks; each stack must hold its deepest call chain (256 bytes) plus one interrupt frame (128 bytes), on a chip with 32 KB of RAM.
(a)
How much RAM do the stacks take?
(b)
What share of RAM is that?
Check
A high-priority task can be blocked by a low-priority task holding a mutex for 0.5 ms; a medium task runs 10 ms; another low task holds a second mutex the high task also needs, for 8 ms.
(a)
With priority inheritance, how long can the high task be blocked at worst, if it may meet both mutexes held?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
And without it, if the medium task runs once while one of the mutexes is held?
Check
A cooperative scheduler runs 6 tasks that each yield every 2 ms at most. A button press must get a response within 50 ms.
(a)
What is the worst-case wait before the button task runs again?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
Does it meet the response time (1 for yes)?
Have paper and a calculator ready. Work each problem out by hand, then enter the answer. Your first try on each one counts toward your score.
Practice
Keep practicing
14 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.