From a8b293d935e39bea69583d9cc2006780a92ab26c Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Mon, 17 Mar 2025 10:09:23 +0100 Subject: [PATCH] vault backup: 2025-03-17 10:09:23 --- .obsidian/workspace.json | 16 +++++++------- .../notes/4c - Dining Philosophers.md | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index b4675d6..0cb290e 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -34,10 +34,10 @@ "type": "pdf", "state": { "file": "Concurrent Systems/slides/class 4.pdf", - "page": 17, - "left": -36, - "top": 91, - "zoom": 0.4133016627078385 + "page": 18, + "left": -26, + "top": 163, + "zoom": 0.57541567695962 }, "icon": "lucide-file-text", "title": "class 4" @@ -196,7 +196,8 @@ } ], "direction": "horizontal", - "width": 364.5 + "width": 364.5, + "collapsed": true }, "left-ribbon": { "hiddenItems": { @@ -217,10 +218,10 @@ }, "active": "6edd4157a160e462", "lastOpenFiles": [ + "Concurrent Systems/slides/class 4.pdf", + "Concurrent Systems/notes/4c - Dining Philosophers.md", "Concurrent Systems/notes/images/Pasted image 20250317100456.png", "Concurrent Systems/notes/4b - Monitors.md", - "Concurrent Systems/notes/4c - Dining Philosophers.md", - "Concurrent Systems/slides/class 4.pdf", "Concurrent Systems/slides/class 5.pdf", "HCIW/slides/4 HUI2016-6-forcefeedback.pdf", "HCIW/slides/3b Haptic slides.pdf", @@ -251,7 +252,6 @@ "Concurrent Systems/notes/images/Pasted image 20250304090219.png", "Concurrent Systems/notes/images/Pasted image 20250304090207.png", "Concurrent Systems/notes/images/Pasted image 20250304084901.png", - "Concurrent Systems/notes/images/Pasted image 20250304084537.png", "Foundation of data science/notes/1 CV Basics.md", "Foundation of data science/notes/7 Autoencoders.md", "Foundation of data science/notes/6 PCA.md", diff --git a/Concurrent Systems/notes/4c - Dining Philosophers.md b/Concurrent Systems/notes/4c - Dining Philosophers.md index 6bcb9f9..c0dac2b 100644 --- a/Concurrent Systems/notes/4c - Dining Philosophers.md +++ b/Concurrent Systems/notes/4c - Dining Philosophers.md @@ -5,3 +5,25 @@ The first real practical example of a concurrent system. - a philosopher must pick up first one chopstick, then the second one, not both at once ![[Pasted image 20250317100456.png|100]] +**PROBLEM:** *Devise a deadlock-free algorithm for allocating these limited resources (chopsticks) among several processes (philosophers).* + +#### A wrong solution +each chopstick is governed by a mutual exclusion semaphore that prevents any other philosopher from picking up the chopstick when it is already in use by another philosopher + +``` +semaphore chopstick[5] initialized to 1 +Philosopher(i) := + while(1) do + chopstick[i].down() + chopstick[(i+1)%N].down() + // eat + chopstick[(i+1)%N].up() + chopstick[i].up() +``` +No two neighbors can eat simultaneously, but we can have a deadlock if all philosophers grab their right chopstick simultaneously. + +#### Solution 1 +Give a number to all forks and always try with the smaller. +All philosophers must first pick left and then right, except for the last one that first picks right and then left. + +So there will be c \ No newline at end of file