From 29465e9a2c782cdba5217ee32921564ee913d520 Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Mon, 17 Mar 2025 10:19:23 +0100 Subject: [PATCH] vault backup: 2025-03-17 10:19:23 --- .obsidian/workspace.json | 4 +-- .../notes/4c - Dining Philosophers.md | 30 ++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index 0ff0932..352734c 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -34,9 +34,9 @@ "type": "pdf", "state": { "file": "Concurrent Systems/slides/class 4.pdf", - "page": 23, + "page": 24, "left": -26, - "top": 359, + "top": 340, "zoom": 0.57541567695962 }, "icon": "lucide-file-text", diff --git a/Concurrent Systems/notes/4c - Dining Philosophers.md b/Concurrent Systems/notes/4c - Dining Philosophers.md index ff2a304..52dcacb 100644 --- a/Concurrent Systems/notes/4c - Dining Philosophers.md +++ b/Concurrent Systems/notes/4c - Dining Philosophers.md @@ -67,4 +67,32 @@ In this case we break the symmetry by letting an odd number of philosophers sit #### Solution 4 - Monitors! Pick up 2 chopsticks only if both are free -a philosopher moves to his/her eating state only if both neighbors are not in their eating states à need to define a state for each philosopher • if one of my neighbors is eating, and I’m hungry, ask them to signal me when they’re done à thus, states of each philosopher are: thinking, hungry, eating à need condition variables to signal waiting hungry philosopher(s) This solutoin very well fits with the features of monitors! \ No newline at end of file +- a philosopher moves to his/her eating state only if both neighbors are not in their eating states + - need to define a state for each philosopher +- if one of my neighbors is eating, and I’m hungry, ask them to signal me when they’re done +- thus, states of each philosopher are: thinking, hungry, eating + - need condition variables to signal waiting hungry philosopher(s) + +``` +monitor DP + status state[N] all initialized at thinking; + condition self[N]; + +Pickup(i) := + state[i] = hungry; + test(i); + if (state[i] != eating) then + self[i].wait; + +Putdown(i) := + state[i] = thinking; + test((i+1)%N); + test((i-1)%N); + +test(i) := + if (state[(i+1)%N] != eating + && state[(i-1)%N] != eating + && state[i] == hungry) then + state[i] = eating; + self[i].signal(); +``` \ No newline at end of file