diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index 56555f0..3e52276 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -49,9 +49,9 @@ "type": "pdf", "state": { "file": "Concurrent Systems/slides/class 3.pdf", - "page": 1, + "page": 5, "left": -9, - "top": 169, + "top": 186, "zoom": 0.6 }, "icon": "lucide-file-text", diff --git a/Concurrent Systems/notes/3.md b/Concurrent Systems/notes/3.md index 4f0d343..3802f7e 100644 --- a/Concurrent Systems/notes/3.md +++ b/Concurrent Systems/notes/3.md @@ -23,7 +23,7 @@ X.test&set() := (the function is implemented in an atomic way by the hardware by suspending the interruptions!) ``` -###### How do we use it? +###### How do we use it for MUTEX? ``` lock() := wait X.test&set() = 0 @@ -44,4 +44,50 @@ X.swap(v) := tmp <- X X <- v return tmp - \ No newline at end of file +``` + +###### How do we use it for MUTEX? +``` +lock() := + wait X.swap(1) = 0 + return + +unlock() := + X <- 0 + return +``` + +#### Compare&swap +``` +X boolean register + +X.compare&swap(old, new) := + if X = old then + X <- new + return true + return false +``` +###### How do we use it for MUTEX? +``` +Initialize X at 0 + +lock() := + wait X.compare&swap(0, 1) = true + return + +unlock() := + X <- 0 + return +``` + +#### Fetch&add +Up to now, all solutions enjoy deadlock freedom, but allow for starvation. So let's use Round Robin to promote the liveness property! + +Let X be an integer register; the Fetch&add primitive is implemented as follows: +``` +X.fetch&add(v) := + tmp <- X + X <- X+v + return tmp +``` +