From b6749b36d12ed6c252ff36f6a66dbba10805bbfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Skaaden?= Date: Mon, 10 May 2021 18:30:58 +0200 Subject: [PATCH] Writeup of lecture 11 about preemptive schedudling --- ntnu/21v/ttk4145/summary/summary.md | 121 ++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/ntnu/21v/ttk4145/summary/summary.md b/ntnu/21v/ttk4145/summary/summary.md index efd1679..8f5d2ea 100644 --- a/ntnu/21v/ttk4145/summary/summary.md +++ b/ntnu/21v/ttk4145/summary/summary.md @@ -198,3 +198,124 @@ A transaction is a design framework for Damage Confinement and Error Recovery. * **C**oncistency: Leaves the system in a consistent state when finished * **I**solation: Errors does not spread * **D**urability: Results are not lost + +### Atomic Actions + +**Resumption vs. Termination mode** +* If we continue where we were (e.g. after the interrupt) --> *Resumption* +* If we continue somewhere else (i.e. terminating what we where doing) --> Termination + +**Async Notification (AN) = Low level thread interaction** +* Async event handling. ("Signals") (resumption) + * Modeled after a HW interrupt + * Can be sent to the correct thread + * Can be handled, ignored, blocked --> The domain can be controlled. + * Often lead to polling + * Could rather skip the signal and poll a status variable or a message queue + * Useless +* ATC --> Async transfer of Control (termination) + * Canceling threads + * setjmpt/longjmp could convert signals to ATC (not really, but still) + * ADA: a strictured mechanism for ATV is integraded with the selected statement + * RT Java: A structured mechanism for ATC is integraded with the exception-handling mechanism + +#### Cancelling threads + +**Yes, killing threads is ATC!** + +* Can make termination model by letting domain be a thread + * "Create a `doWork` thread, and kill it if the action fails" +* Ca still control domain by disabling "cancelstate" + +**But, but, but: It leaves ut in undifined state!?** +* Not if we have... + * Full control over changed state (like logs or recovery points) or some other way of recovering well. + * A lock manager that can unlock on behalf of killed thread + * Some control of where we were killed (like nok in the middle of a lock manager or log call) +* An this is what we have! + + +## Shared variable synchronization + +### Non-Preemptive scheduling + +Controlling a pump filling a tank. + +**Spec:** +* Every second: measure the water level of the tank and generate the reference to the pump +* 10 times a second: Set the power of the pump motor +* Do some GUI: let the human control the process + +#### A trivial solution: "Cyclic Exectutive" + +{% highlight c %} +oldTime = now(); +i = 0; +while(true) { + i = i + 1; + if (i % 10 == 0) { + i = 0; + calculatePumpReference(); + } + controlPump(); + do { + handleUserEvent(); + } while(now() < oldTime + 0.1); + oldTime = oldTime + 0.1; +} +{% endhighlight %} + +**Drawbacks** + +* OK tasks? +* Timing hard to tune (what if pump sampling should be $\pi$/10?) +* Overload (what if `calucaltePumpReference` uses more than 1/10 seconds?) +* How to add new tasks? (Everything is coupled) +* Waste of time in the do-loop? +* What is priority of `handleUserEvents`? +* How are erros, exceptions, alarms etc. handled? + +#### Better soulution with Non-preemptive scheduler + +* *3 taskts* administered by a scheduler +* The scheduler takes care of who runs and timing +* Scheduler often inculuded in OSes +* Introducing priorities + +{% highlight c %} +/** + * scheduler_registerThread(function, time, priority) + * Higher priority numer means higher priority in scheduler + */ +main() { + scheduler_registrerThread(controlPump, 0.1, 3); + scheduler_registrerThread(calculatePumpReference, 1, 2); + scheduler_registrerThread(handleUserEvents, 0.2, 1); + scheduler_mainLoop(); +} +{% endhighlight %} + +**Some notes on priorities** +* Priority is generally not important; rather, the main rule is to give higher priority to shorter-deadline tasks. + * This allows tasks to reach its deadlines. +* ... but this is not always the case - if e.g. the tasks are cooperating +* We still handle overload badly +* And: What connection between deadline and priority to start with? + * Is this a good dependency seen from a code quality perspective? + +### Pros and cons of nonpreemptive scheduling + +| **Pros** | **Cons** | +| :--------------------------------------------- | :------------------------------------------------------------------------- | +| Simple, intuitive, predictable | C macro hell | +| No kernel | Threads must cooperate <-- a form of dependency breaking module boundaries | +| Fast switching times | Heavy threads must be divided | +| Some elegant sunchronization patterns possible | Can we handle blocking of library functions? | +| | Unrobust to errors | +| | Unrobust to (heavy) error handling | +| | Hard to tune at end of project | +{: .table-responsive-lg .table } + + + +