10-01

Chapter 10 · Fairness, derived

Be the scheduler

  • 42 min core
  • Essentials track
  • Lab: Scheduler Game
  • Skills: hostile-env, counterexamples

00Recall

One card from 09-04 on why doing nothing is a legal behavior, one from 09-02 on where []<>P and <>[]P must hold on a lasso, and one from 03-05 on what CHECK_DEADLOCK FALSE changes. The first counterexample below is a behavior that does nothing; the next lesson builds weak fairness out of <>[] and []<>; and lesson 10-04 turns deadlock checking off and shows what that hides.

09-04-a from Doing nothing is legal

Why does a spec of the form Init /\ [][Next]_vars satisfy no liveness property like <>Done?

Show answer

[][Next]_vars allows stuttering steps, so every behavior may stop at any point and repeat its state for ever. Unless Done already holds there, <>Done fails.

09-02-a from Operators on lassos

On a lasso (a stem, then a loop), where must P hold for []<>P, and where for <>[]P?

Show answer

[]<>P: in at least one state of the loop. <>[]P: in every state of the loop. The stem does not matter to either.

Skip recall Recall is never required.

01Commit

Two processes, A and B, share one lock. A process asks for the lock (Request), takes it when it is free (Acquire), and gives it back (Release). That is specs/FairLock.tla. The requirement is that nobody waits for ever:

TLA+
NoStarvation is defined as for all p in Procs : (pc[p] = "waiting") leads to (pc[p] = "cs")

~> is read "leads to": whenever the left side is true, the right side is true then or at some later moment. The spec is the shape you know, Spec == Init /\ [][Next]_vars, with nothing added.

Commit a

FairLock.live.cfg checks NoStarvation against Spec, which has no fairness. What does TLC report?

  1. No violation: every step a waiting process needs is always possible sooner or later.
  2. A violation of NoStarvation, with a lasso.
  3. A deadlock: two processes and one lock get stuck.

Answer A violation of NoStarvation, with a lasso.

A teammate reads that counterexample and adds one conjunct: the system as a whole may not stop while some step is possible.

TLA+
WFNextSpec is defined as Spec and WF_vars(Next)

Commit b

FairLock.wfnext.live.cfg checks NoStarvation against WFNextSpec. What does TLC report?

  1. No violation: the system can no longer stop, so every waiting process gets in.
  2. A violation: a lasso in which steps keep happening, and one process waits for ever.
  3. A violation: the same kind of lasso as before, ending with nothing happening for ever.

Answer A violation: a lasso in which steps keep happening, and one process waits for ever.

02Lab

Lab · Scheduler GameWhat you’re looking at: every state FairLock can reach and every step between them. You choose each step. Try to keep process B waiting for ever.

NoStarvation violated · loop

● 1● 23● 4567● 8

This is a teaching view. Real checking happens with TLC on your machine.

Trace

  1. State 1 Initial state lock = FALSE pc = (A :> "idle" @@ B :> "idle")
  2. State 2 Request(A) pc (A :> "idle" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "idle")
  3. State 3 Request(B) pc (A :> "waiting" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "waiting")
  4. State 4 Acquire(B) lock FALSE becomes TRUE pc (A :> "waiting" @@ B :> "waiting") becomes (A :> "waiting" @@ B :> "cs")
  5. Loop stutters forever in state 4
Text view: every state and step
States and their successors
StatepclockSteps (action → state)
initial 1(A :> "idle" @@ B :> "idle")FALSERequest(A) → 2; Request(B) → 3
2(A :> "waiting" @@ B :> "idle")FALSERequest(B) → 4; Acquire(A) → 5
3(A :> "idle" @@ B :> "waiting")FALSERequest(A) → 4; Acquire(B) → 6
4(A :> "waiting" @@ B :> "waiting")FALSEAcquire(A) → 7; Acquire(B) → 8
5(A :> "cs" @@ B :> "idle")TRUERequest(B) → 7; Release(A) → 1
6(A :> "idle" @@ B :> "cs")TRUERequest(A) → 8; Release(B) → 1
7(A :> "cs" @@ B :> "waiting")TRUERelease(A) → 3
8(A :> "waiting" @@ B :> "cs")TRUERelease(B) → 2

You are the scheduler, and you are hostile. Play twice, and write your strategy down each time before you read on.

  1. Keep B waiting for ever, any way you like.
  2. Play again under one rule: you may never stop while some step is possible.

The lab also shows the verdicts TLC gave for each fairness assumption. Read only the "no fairness" one for now. Then run both checks:

shell
./tla check chapters/10-fairness/specs/FairLock.live.cfg
./tla check chapters/10-fairness/specs/FairLock.wfnext.live.cfg
Expected output 10-fairness/specs/FairLock.live
10-fairness/specs/FairLock.live  FairLock.live.cfg  (A=A, B=B)
parse ok
model check: temporal property NoStarvation violated; trace of 4 states, then stuttering forever; 8 distinct states
TLC warning: The stuttering counterexample above may be caused by the absence of a fairness constraint in the behavior specification Spec defined at line 55, col 1 to line 55, col 29 of module FairLock. To rule out such counterexamples, conjoin a suitable fairness constraint to Spec (compare Chapter 8, page 87ff of Specifying Systems at https://lamport.azurewebsites.net/tla/book.html).
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
     (stutter)  nothing changes from here on, forever

Reveal for commit a

TLC reports that NoStarvation is violated and prints a lasso: 4 states, then "nothing changes from here on, forever". It also prints its own warning that a stuttering counterexample "may be caused by the absence of a fairness constraint". The process waiting in the last state never gets the lock, because nothing ever happens again.

Reveal for commit b

Still violated. This time the lasso has 6 states and loops back to an earlier one: every step in the loop is a real step, and one process waits through all of them. The other process asks, takes the lock, gives it back, and asks again. The system never stops. It never gets round to the waiting process.

03Reason

If you picked "no violation" for the first question, you read "every step is possible" as "every step will happen". A spec is a formula that is true of the behaviors you allow. Spec allows every behavior that starts in Init and takes only Next steps or stuttering steps, including one that takes a few steps and then stutters for ever. Chapter 09 showed you that doing nothing is legal. Here it is again, with a lock.

Here is TLC's first lasso:

  1. State 1 Initial state pc = (A :> "idle" @@ B :> "idle") lock = FALSE
  2. State 2 Request pc (A :> "idle" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "idle")
  3. State 3 Request pc (A :> "waiting" @@ B :> "idle") becomes (A :> "waiting" @@ B :> "waiting")
  4. State 4 Acquire pc (A :> "waiting" @@ B :> "waiting") becomes (A :> "waiting" @@ B :> "cs") lock FALSE becomes TRUE
  5. Loop stutters forever in state 4

So the scheduler has two weapons. In the lab you probably found both.

WeaponWhat the scheduler doesRuled out by WF_vars(Next)?
StopAfter some point, takes no step at allYes
FavouritesKeeps choosing other steps, so the victim's step never comesNo

WF_vars(Next) is the teammate's conjunct. WF stands for weak fairness; the next lesson takes it apart. For now, read it as the rule you played under in round two: if some Next step stays possible, such steps keep happening, infinitely often. Any Next step. The rule never says whose. A scheduler that runs whichever thread it likes, as long as it runs something, satisfies it. So does one that always picks the thread with the lowest number. Neither is what you meant by fair, and the formula did not say what you meant.

Write something before openingin TLC's second lasso, which steps does the waiting process take inside the loop?

None. A waiting process has exactly one possible step, Acquire, and taking it would end its wait. So inside a loop that starves it, it never moves. Every step in the loop belongs to the other process, which goes round Request, Acquire, Release. That holds for any counterexample to NoStarvation under WFNextSpec, not only the one TLC printed: the starving process must wait in every state of the loop, and the rule forces the loop to contain real steps, so someone else has to take them.

What would it take to beat favouritism? A rule about one process's step, not about the system's. That rule is the next lesson. Notice where the evidence for it came from: not from reading the spec, but from losing to a hostile scheduler twice. Every fairness condition in this chapter is a cheat that you take away from the scheduler, one at a time, because you watched it win.

Write something before openingin round two of the lab, could you starve B without ever letting B ask for the lock?

You could keep B idle for ever, but that is not starvation: NoStarvation only speaks about a process while it waits. An idle process is not owed anything. That is deliberate. Nothing in FairLock forces a process to want the lock, and no fairness in this chapter will either. Forcing users, clients or philosophers to act is a different kind of assumption, and lesson 10-04 shows what it can hide.

04In your editor

The specs are in chapters/10-fairness/specs/. Check this lesson's exercises and the specs it uses with:

shell
./tla check 10-01

The second run, with the system never allowed to stop:

Output 10-fairness/specs/FairLock.wfnext.live
10-fairness/specs/FairLock.wfnext.live  FairLock.wfnext.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 4

Read a liveness counterexample from the bottom. The last line says where the loop closes. The states from there down are the part that repeats for ever; everything above it is the stem that gets you there. Ask who moves inside the loop and who never does.

05Practice

Warm-up. Write NoStarvation yourself and watch TLC refute it under WFNextSpec.

Core. Be the scheduler for real. Write your hostile strategy as an action, and let TLC confirm that it is legal under the no-stopping rule and that B starves.

10-01-ex1Warm-up5 min

10-01-ex1 · Name the starvation

Tier: warm-up. Time: about 5 minutes.

FairLock.tla is the lock from lesson 10-01, with one line missing: the requirement. FairLock.cfg checks it against WFNextSpec, the spec in which the system as a whole may never stop.

  1. Run ./tla check 10-01-ex1. All four cfgs pass, because NoStarvation is TRUE. A requirement that is TRUE cannot be broken, so the pass tells you nothing.
  2. Fill gap g1: every process that waits eventually gets the lock. Use ~>, read "leads to", and say it for every process.
  3. Run the check again. FairLock.cfg must now fail with a lasso. Read it from the bottom: who moves inside the loop, and who never does?

The check also runs FairLock.grader.cfg, the course's test of your claim. It uses strong fairness, under which every waiting process really does get in, and in which a process may stay idle for ever. Your NoStarvation must pass there. If it fails, your claim demands more than the requirement does, for example that idle processes ask for the lock. It also runs MCFairLock.cfg, where only process A can starve, and MCFairLock.mirror.cfg, where only B can: your claim must fail under both, so a claim about one process alone is caught. Leave these files, and MCFairLock.tla, as they are.

Hints: HINTS.md, under "10-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/10-fairness/exercises/01-ex1-name-the-starvation/; open it after you have a passing answer of your own.

The goals differ: "goal met: property NoStarvation" for FairLock.cfg and both MCFairLock cfgs, and "goal met: pass" for FairLock.grader.cfg.

Done when: ./tla check 10-01-ex1 prints "4 of 4 checks met their expected outcome".

./tla check 10-01-ex1

Repo chapters/10-fairness/exercises/01-ex1-name-the-starvation/

Hints

Hint 1

The requirement has the shape "whenever this, later that", for each process. Chapter 09 gave you one operator for exactly that shape.

Hint 2

"This" is a process waiting; "that" is the same process in its critical section. The property must hold for every process in Procs, so the quantifier goes outside.

Hint 3 (nearly the answer)

For all p in Procs, pc[p] = "waiting" leads to pc[p] = "cs". If the grader cfg fails, your formula asks something of idle processes; if FairLock.cfg or one of the two MCFairLock cfgs passes, your formula speaks for only one process, so check the quantifier.

10-01-ex2Core8 min

10-01-ex2 · Be the scheduler

Tier: core. Time: about 8 minutes.

In the lab you starved B by clicking. Now write your strategy down, so that TLC can check every behavior it allows.

MCFairLock.tla extends the lock. HostileNext is your strategy: an action that says, in each state, which FairLock steps you allow. HostileSpec runs it without ever stopping while it allows a step. MCFairLock.cfg then checks two properties of every behavior your strategy produces:

  • WFNextSpec: it is a legal behavior of FairLock, even under the rule that the system may never stop while some step is possible;
  • BStarves: from some point on, B waits for ever.
  1. Run ./tla check 10-01-ex2. The starter strategy lets B ask and then allows nothing. Read which property fails, and why.
  2. Fill gap g1. Your strategy may only allow FairLock actions, such as Request(A) or Acquire(A), and it must never leave the system with nothing to do while a FairLock step is possible. Think about what you allow while B is idle, and what you allow once it waits.
  3. Run the check. It must pass, with the witness Contended reached. Its coverage line counts the top-level disjuncts of your HostileNext, not FairLock's actions; WFNextSpec is the check that every step you allow is a FairLock step.

If WFNextSpec fails, read its lasso: in its last state some step was possible, and your strategy allowed none of them. If BStarves fails, your strategy let B in.

Leave MCFairLock.cfg, HostileSpec and BStarves as they are.

Hints: HINTS.md, under "10-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/10-fairness/exercises/01-ex2-be-the-scheduler/; open it after you have a passing answer of your own.

Done when: ./tla check 10-01-ex2 prints "goal met: pass".

./tla check 10-01-ex2

Repo chapters/10-fairness/exercises/01-ex2-be-the-scheduler/

Hints

Hint 1

Your strategy has two phases. While B is idle, the only way to make B starve later is to let it ask. Once B waits, every step you allow must be a step that does not let B in.

Hint 2

The rule says you may not stop while some FairLock step is possible. While B waits, Acquire(B) is sometimes possible, so you must always offer some other step. Which process can always move, whatever the state of the lock?

Hint 3 (nearly the answer)

Write HostileNext as two disjuncts guarded by pc[B]: when B is idle, allow only Request(B); when B waits, allow any of A's three actions. Check that in every state reachable this way, at least one of the steps you allow is enabled.

06Explain it

Open NOTES.md and answer the three prompts under 10-01, about the Core exercise.

output
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-01 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "My HostileNext lets B ask, then allows only A's steps while B waits. It is a legal behavior of WFNextSpec because A can always move, so the system never stops while a step is possible; and B starves because no allowed step is Acquire(B). It relies on WF_vars(Next) being the only fairness: weak fairness on B's own Acquire would not be broken by this strategy, but strong fairness on it would. TLC checked two processes; it did not check that any real scheduler plays this way, only that one could."

A weak answer: "I made B wait and the check passed." It does not say why the strategy is legal under the no-stopping rule, or which fairness assumption would defeat it.

Done?