10-02
Chapter 10 · Fairness, derived
Weak fairness from a starvation trace
00Recall
One card from 10-01 on why a spec with no fairness lets a process wait for ever, one from 04-01 on [A]_v and <<A>>_v, and one from 09-03 on why liveness lives in its own cfg. Today <<A>>_v stops being notation and starts doing a job.
10-01-a from Be the scheduler
FairLock's Spec is Init /\ [][Next]_vars, with no fairness. Why can TLC show a behavior in which a waiting process never gets the lock?
Show answer
Spec only constrains each step. A behavior that stops and stutters for ever satisfies it, so nothing forces the step the waiting process needs.
04-01-b from The shape of every spec
What does <<A>>_v mean, and why is <<Rest>>_vars false of every step when Rest is UNCHANGED vars?
Show answer
A /\ (v' # v), an A step that really changes v. A Rest step never changes vars, so no step is both.
09-03-a from Why TLC shows you a lasso
PROPERTY P, where P is a state predicate with no temporal operator. What does TLC check?
Show answer
Only that P holds in the initial states. TLC warns about it. INVARIANT P, or PROPERTY []P, checks every reachable state.
Skip recall Recall is never required.
01Commit
Here is one behavior of FairLock with no fairness. A asks, B asks, B takes the lock. Then nothing happens, for ever: B sits in its critical section and A waits.
You want one rule, about one action, that throws this behavior out and nothing reasonable with it.
Commit a
In the last state of that behavior, which step stays possible at every moment of the "for ever", and never happens?
- Release(B): B holds the lock, and releasing is always possible while it does.
- Acquire(A): A is waiting, so A's acquire is the step that is owed.
- Request(A): A should ask again.
Answer Release(B): B holds the lock, and releasing is always possible while it does.
- If you picked B
- A's acquire is the step you want, but it is not possible here: the lock is taken. A rule that forced a step that is not possible would be asking for the impossible. Look for a step that is possible in every one of those states.
- If you picked C
- A is already waiting, so Request(A) is not possible. And forcing a process to ask is a different kind of rule, about what users do, not about what the scheduler does.
The rule you found, applied to every Acquire(p) and every Release(p), is WFSpec in specs/FairLock.tla. The module also defines a weaker liveness property, Progress: if anyone waits, someone eventually holds the lock.
Commit b
Under WFSpec, which of Progress and NoStarvation does TLC find no violation of?
- Both.
- Progress only.
- Neither.
Answer Progress only.
- If you picked A
- NoStarvation needs the waiting process's own Acquire to happen. Ask whether that step is possible at every moment of the scheduler's favouritism trick, or only at some.
- If you picked C
- The rule throws out every behavior that stops while a release or an acquire stays possible. That is enough for someone to get the lock; check it against the trace in the lab.
02Lab
NoStarvation violated · loop
This is a teaching view. Real checking happens with TLC on your machine.
Trace
- State 1 Initial state lock = FALSE pc = (A :> "idle" @@ B :> "idle")
- State 2 Request(A) pc (A :> "idle" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "idle")
- State 3 Request(B) pc (A :> "waiting" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "waiting")
- State 4 Acquire(B) lock FALSE becomes TRUE pc (A :> "waiting" @@ B :> "waiting") becomes (A :> "waiting" @@ B :> "cs")
- Loop stutters forever in state 4
Text view: every state and step
| State | pc | lock | Steps (action → state) |
|---|---|---|---|
| initial 1 | (A :> "idle" @@ B :> "idle") | FALSE | Request(A) → 2; Request(B) → 3 |
| 2 | (A :> "waiting" @@ B :> "idle") | FALSE | Request(B) → 4; Acquire(A) → 5 |
| 3 | (A :> "idle" @@ B :> "waiting") | FALSE | Request(A) → 4; Acquire(B) → 6 |
| 4 | (A :> "waiting" @@ B :> "waiting") | FALSE | Acquire(A) → 7; Acquire(B) → 8 |
| 5 | (A :> "cs" @@ B :> "idle") | TRUE | Request(B) → 7; Release(A) → 1 |
| 6 | (A :> "idle" @@ B :> "cs") | TRUE | Request(A) → 8; Release(B) → 1 |
| 7 | (A :> "cs" @@ B :> "waiting") | TRUE | Release(A) → 3 |
| 8 | (A :> "waiting" @@ B :> "cs") | TRUE | Release(B) → 2 |
Pick the weak-fairness variant, then play the favourites trick from 10-01: let A ask, take, release and ask again while B waits. At each state, look at whether B's Acquire is shaded. Then run:
./tla check chapters/10-fairness/specs/FairLock.wf.live.cfg
./tla check chapters/10-fairness/specs/FairLock.wfstarve.live.cfg10-fairness/specs/FairLock.wfstarve.live10-fairness/specs/FairLock.wfstarve.live FairLock.wfstarve.live.cfg (A=A, B=B)
parse ok
model check: temporal property NoStarvation violated; lasso of 6 states, looping back to state 4; 8 distinct states
expected outcome met: property NoStarvation
trace (changed variables only; ./tla trace --full shows all):
1 Init pc = (A :> "idle" @@ B :> "idle"), lock = FALSE
2 Request pc: (A :> "idle" @@ B :> "idle") -> (A :> "waiting" @@ B :> "idle")
3 Request pc: (A :> "waiting" @@ B :> "idle") -> (A :> "waiting" @@ B :> "waiting")
4 Acquire pc: (A :> "waiting" @@ B :> "waiting") -> (A :> "waiting" @@ B :> "cs"), lock: FALSE -> TRUE
5 Release pc: (A :> "waiting" @@ B :> "cs") -> (A :> "waiting" @@ B :> "idle"), lock: TRUE -> FALSE
6 Request pc: (A :> "waiting" @@ B :> "idle") -> (A :> "waiting" @@ B :> "waiting")
Acquire(B) back to state 4Reveal for commit a
Release(B). In every state from the moment B holds the lock, releasing is possible, and in the behavior it never happens. That is the shape a weak fairness rule forbids: a step possible at every moment from some point on, and never taken.
Reveal for commit b
Progress passes, in the same 8 distinct states as every other FairLock run. NoStarvation fails, with a lasso of 6 states. It is the favourites trick again: someone keeps getting the lock, which is all Progress asks for, and it is never the process that waits.
03Reason
If you picked Acquire(A) in the first question, you named the step you wanted, not the step the rule can demand. A fairness rule can only demand a step that is possible. So read the trace for what was possible and did not happen.
Possible. For an action A, ENABLED A is a state predicate: it is true in a state exactly when some step from that state satisfies A. ENABLED Release(B) is true in every state where B is in its critical section.
Did not happen, for real. A stuttering step changes nothing. If it counted as "taking" Release(B), the scheduler could claim it served B while doing nothing. So the rule talks about <<A>>_v, read "an A step that changes v". You met it in 04-01; this is what it is for.
At every moment from some point on. <>[]P is read "eventually always P": from some point on, P holds in every state. The word for that is continuously.
Put the three together and you have the rule from the first question, for any action. TLA+ has a name for it, WF_vars(act), weak fairness of act, and the Core exercise asks you to write it as a formula yourself. The exact definition is printed after the exercises, below; write yours first.
Write something before openingthe rule switches on when the action is possible "from some point on". Why not "from the very first state"?
Because most actions are not possible in the first state. Release(B) is not: nobody holds the lock yet. A rule that applied only to actions possible from the very first state would never apply to it, and the behavior from the first question, where B sits in its critical section for ever, would escape. "From some point on" lets the rule switch on whenever an action becomes possible for good, however late. The promise needs "infinitely often" and not "once" for a similar reason: after one step the action may become possible again and stay so for ever, and the promise has to cover that stretch too.
Now look at the lasso TLC printed under WFSpec:
- State 1 Initial state pc = (A :> "idle" @@ B :> "idle") lock = FALSE
- State 2 Request pc (A :> "idle" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "idle")
- State 3 Request pc (A :> "waiting" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "waiting")
- State 4 Acquire pc (A :> "waiting" @@ B :> "waiting") becomes (A :> "waiting" @@ B :> "cs") lock FALSE becomes TRUE
- State 5 Release pc (A :> "waiting" @@ B :> "cs") becomes (A :> "waiting" @@ B :> "idle") lock TRUE becomes FALSE
- State 6 Request pc (A :> "waiting" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "waiting")
- Loop Acquire(B) back to state 4
Write something before openingwhy does Progress hold under WFSpec, when NoStarvation does not?
Suppose someone waits and from then on nobody ever holds the lock. Then the lock is free in every state from there on. Nothing but Acquire moves a waiting process, so the waiter keeps waiting, and its Acquire is enabled in every state: continuously. Weak fairness on that Acquire forces it, and that contradicts "nobody ever holds the lock". So someone gets the lock. Nothing in the argument says the waiter is the one who gets it while others compete, which is why NoStarvation escapes.
The same three words fit any system. The lift in specs/Elevator.tla gets weak fairness on each of the car's own actions and none on the people pressing buttons; Elevator.live.cfg finds no violation of CallsAnswered in 55 distinct states, and Elevator.nofair.live.cfg shows the car stopping for good without it. The Warm-up asks you to write that fairness.
Notice what the lift's fairness does not say. It does not say the car is quick, or that it answers calls in the order they came, or that anyone presses a button at all. It says only that the car does not stop for good while it has something it could do. Every stronger promise about the lift needs a property of its own, checked on its own, not more fairness.
Write something before openingwhy put fairness on each Acquire(p) separately, instead of one WF on \E p \in Procs : Acquire(p)?
WF_vars(\E p \in Procs : Acquire(p)) is one promise, kept whenever some process acquires. With the quantifier outside, \A p \in Procs : WF_vars(Acquire(p)), each process has its own. On FairLock the two happen to allow the same behaviors: there is one lock, so every acquire disables every other process's Acquire, and no waiter's Acquire can stay enabled while others keep acquiring. Where one process's step does not disable another's, such as one queue per region, the placement is the difference between "the system makes progress" and "every process does". The review item 10-review is such a system.
04In your editor
Check this lesson's exercises and the specs it uses with:
./tla check 10-02The passing run:
10-fairness/specs/FairLock.wf.live10-fairness/specs/FairLock.wf.live FairLock.wf.live.cfg (A=A, B=B)
parse ok
model check: no violation found in 8 distinct states (15 generated, depth 4)
expected outcome met: pass
coverage: all 3 actions fired
witness Contended reached (trace of 4 states)When you add fairness, write one conjunct per action and per process, and next to each one, in a comment, the sentence it stands for: "a process in its critical section eventually leaves it". If you cannot write the sentence, you do not yet know why the conjunct is there.
05Practice
Warm-up. The lift stops for good. Give the car's own actions weak fairness, and nobody else's.
Core. Write weak fairness by hand, from ENABLED, <<act>>_vars, [] and <>. Two cfgs check that it means exactly WF_vars, one direction each.
10-02-ex1Warm-up5 min
10-02-ex1 · Wake the elevator
Tier: warm-up. Time: about 5 minutes.
The lift from chapter 09, written in this chapter's style. LiveSpec has no fairness yet, so a car with calls waiting may stop for good.
- Run
./tla check 10-02-ex1.Elevator.cfgreportsCallsAnsweredviolated. Read how the trace ends, and which of the car's actions is possible in its last state. - Fill gap g1: weak fairness on each of the car's own actions, one conjunct per action. The car's actions are the ones a controller performs. Pressing a button is not one of them.
- Run the check again. Both cfgs must pass, with every action firing and the witness
BusyMiddlereached.
MCElevator.cfg is the course's check that your fairness does not assume too much. It runs a spec with strong fairness on the car's actions and no fairness on anyone pressing a button, and asks whether every behavior of that spec satisfies your LiveSpec. Fairness on Call fails there: no lift controller can make people keep pressing buttons. Leave MCElevator.tla and its cfg as they are.
Hints: HINTS.md, under "10-02-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/10-fairness/exercises/02-ex1-wake-the-elevator/; open it after you have a passing answer of your own.
Done when: ./tla check 10-02-ex1 prints "2 of 2 checks met their expected outcome".
./tla check 10-02-ex1
Repo chapters/10-fairness/exercises/02-ex1-wake-the-elevator/
Hints
Hint 1
In the last state of the trace there are calls and the car does nothing. Which of its four actions is enabled there, and stays enabled for ever in the stutter?
Hint 2
Sort the five actions: which are the car's own, and which belong to the people in the building? A fairness conjunct is a promise about whoever performs the action, and the lift's controller can only keep promises about the car.
Hint 3 (nearly the answer)
One weak fairness conjunct for each of the car's four actions, and none on Call. If MCElevator.cfg fails, you wrote fairness on Call, or something stronger than the ceiling allows.
10-02-ex2Core8 min
10-02-ex2 · Weak fairness by hand
Tier: core. Time: about 8 minutes.
Lesson 10-02 built weak fairness out of three words: possible, did not happen, continuously. Write it yourself, as an operator on any action, and let TLC compare it with the real thing.
MCFairLock.tla extends FairLock. HandSpec puts your HandWF on every Acquire(p) and every Release(p), exactly where WFSpec puts WF_vars. Two cfgs compare them:
MCFairLock.cfgchecks that every behavior ofHandSpecis a behavior ofWFSpec. If your operator is too weak, this can fail.MCFairLock.converse.cfgchecks the other direction. If your operator is too strong, this can fail.
Every Acquire(p) and Release(p) changes pc and disables itself, so on those actions some wrong operators still pass. MCFairLock.next.cfg and MCFairLock.next.converse.cfg compare HandNextSpec with WFNextSpec in the same two directions, on Next \/ UNCHANGED vars, an action that is always enabled and that a stuttering step satisfies.
- Run
./tla check 10-02-ex2. The starter,[]<>TRUE, assumes nothing. Read which cfgs fail and what their counterexamples show. - Fill gap g1 using
ENABLED,<<act>>_vars,[]and<>. Do not writeWF_orSF_: the exercise is to build the definition, and the cfgs cannot tell whether you copied it. - Run the check. All four cfgs must pass, with every action firing and the witness
Contendedreached.
Before you run your answer, write down two predictions. If you write the strong fairness hypothesis by mistake, which cfg fails? If you leave out the angle brackets, which one fails, and why only that one?
Leave the cfgs, HandSpec and HandNextSpec as they are.
Hints: HINTS.md, under "10-02-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/10-fairness/exercises/02-ex2-weak-fairness-by-hand/; open it after you have a passing answer of your own.
Done when: ./tla check 10-02-ex2 prints "4 of 4 checks met their expected outcome".
./tla check 10-02-ex2
Repo chapters/10-fairness/exercises/02-ex2-weak-fairness-by-hand/
Hints
Hint 1
Say weak fairness in words first: if an act step that changes vars is possible at every moment from some point on, such steps happen again and again.
Hint 2
The formula is an implication: "possible, continuously" on the left, "happens, again and again" on the right. Which temporal operator says "from some point on, always", and which says "infinitely often"?
Hint 3 (nearly the answer)
"Possible" is ENABLED applied to the angle-bracket action, and both sides talk about the angle-bracket action, not the bare one; if MCFairLock.next.cfg fails, check the brackets. In a temporal property TLC accepts an action only in the forms []<>A, <>[]A and [][A]_v, so a bare <> in front of the action is an error. If a converse cfg fails, your operator assumes too much, as the strong fairness hypothesis does.
Read the callout below only once your Core passes all four cfgs, and compare it with what you wrote.
06Explain it
Open NOTES.md and answer the three prompts under 10-02, about the Core exercise.
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:Compare your answers with "10-02 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "HandWF(act) says: if <<act>>_vars is enabled at every moment from some point on, it happens infinitely often. The cfgs show that HandSpec and WFSpec allow the same behaviors of FairLock, and so do HandNextSpec and WFNextSpec. The angle brackets matter: without them a stuttering step counts as taking Next \/ UNCHANGED vars, and MCFairLock.next.cfg fails. On Acquire and Release alone TLC could not tell, because every one of those steps changes pc. TLC compared the operators only on this two-process lock, not on every spec."
A weak answer: "I wrote the definition from the lesson and all the cfgs passed." It does not explain the definition, or say what each pair of cfgs shows that one direction alone does not.
Done?