#### From deadlock freedom to bounded bypass
-> Round Robin algorithm

Let DLF be any deadlock free protocol for MUTEX. Let's see how we can make it satisfy bounded bypass:

```
lock(i) :=
	FLAG[i] <- up
	wait (TURN = i OR FLAG[TURN] = down)
	DLF.lock(i)
	return

unlock(i) :=
	FLAG[i] <- down
	if FLAG[TURN] = down then
		TURN <- (TURN + 1) mod n
	DLF.unlock(i)
	return
```


###### Is it deadlock free?
Since DLF is deadlock free, it is sufficient to prove that at least one process invokes DLF.lock.
If TURN = k and $p_k$ invoked lock, then it finds TURN = k and exits its wait.
Otherwise, any other process will find `FLAG[TURN] = down` and exits from its wait.

**Lemma 1:** if TURN = i and `FLAG[i] = up` then $p_i$ enters the CS in at most n-1 iterations

Observation 1: TURN changes only when FLAG[i] is down (after pi has completed its CS)

Observation 2: FLAG[i] = up -> either pi is in its CS or pi is competing for its CS -> it eventually invokes (if not already) DLF.lock

Observation 3: if $p_j$ invokes lock after that FLAG[i] is set, $p_j$ blocks in its wait

Let Y be the set of processes competing for the CS (suspended on the DLF.lock)
- because of Observation 2, $i \in Y$
- because of Observation 3, once FLAG[i] is set, Y cannot grow anymore
- because DLF is deadlock free, eventually one $p_{y} \in Y$ wins if $y = i$.
	- if $y = i$ we are done
	- otherwise, Y shrinks by one. And because of Observation 1, TURN and FLAG[TURN] don't change, so $p_y$ cannot enter Y again.
- Iterating this reasoning we can see that $p_i$ will eventually win, and the worst case is when is the last winner.

**Lemma 2:** If FLAG[i] = up, then TURN is set to i in at most $(n-1)^2$ iterations.

If TURN=i when FLAG[i] is set, done
By Deadlock freedom of RR, at least one process eventually unlocks
- If FLAG[TURN] = down, then TURN is increased. Otherwise, by Lemam 1, $p_{TURN}$ wins in at most n-1 iterations and increases TURN.
- If now TURN = i then we are done. Otherwise, we repeat this reasoning.

The worst case is when TURN = *i+1* mod n when FLAG[i] is set.

![[Pasted image 20250304093223.png]]

