How to resolve the algorithm Dining philosophers step by step in the Nim programming language
How to resolve the algorithm Dining philosophers step by step in the Nim programming language
Table of Contents
Problem Statement
The dining philosophers problem illustrates non-composability of low-level synchronization primitives like semaphores. It is a modification of a problem posed by Edsger Dijkstra. Five philosophers, Aristotle, Kant, Spinoza, Marx, and Russell (the tasks) spend their time thinking and eating spaghetti. They eat at a round table with five individual seats. For eating each philosopher needs two forks (the resources). There are five forks on the table, one left and one right of each seat. When a philosopher cannot grab both forks it sits and waits. Eating takes random time, then the philosopher puts the forks down and leaves the dining room. After spending some random time thinking about the nature of the universe, he again becomes hungry, and the circle repeats itself. It can be observed that a straightforward solution, when forks are implemented by semaphores, is exposed to deadlock. There exist two deadlock states when all five philosophers are sitting at the table holding one fork each. One deadlock state is when each philosopher has grabbed the fork left of him, and another is when each has the fork on his right. There are many solutions of the problem, program at least one, and explain how the deadlock is prevented.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Dining philosophers step by step in the Nim programming language
Source code in the nim programming language
import threadpool, locks, math, os, random
# to call randomize() as a seed, need to import random module
randomize()
type Philosopher = ref object
name: string
food: string
forkLeft, forkRight: int
const
n = 5
names = ["Aristotle", "Kant", "Spinoza", "Marx", "Russell"]
foods = [" rat poison", " cockroaches", " dog food", " lemon-curd toast", " baked worms"]
var
forks: array[n, Lock]
phils: array[n, Philosopher]
threads: array[n, Thread[Philosopher]]
proc run(p: Philosopher) {.thread.} =
# random deprecated, use rand(x .. y)
sleep rand(1..10) * 500
echo p.name, " is hungry."
acquire forks[min(p.forkLeft, p.forkRight)]
sleep rand(1..5) * 500
acquire forks[max(p.forkLeft, p.forkRight)]
echo p.name, " starts eating", p.food, "."
sleep rand(1..10) * 500
echo p.name, " finishes eating", p.food, " and leaves to think."
release forks[p.forkLeft]
release forks[p.forkRight]
for i in 0..<n:
initLock forks[i]
phils[i] = Philosopher(
name: names[i],
food: foods[rand(0 .. n) mod n],
forkLeft: i,
forkRight: (i + 1) mod n
)
createThread(threads[i], run, phils[i])
joinThreads(threads)
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the SQL programming language
You may also check:How to resolve the algorithm Classes step by step in the Julia programming language
You may also check:How to resolve the algorithm Bitmap step by step in the JavaScript programming language
You may also check:How to resolve the algorithm GUI component interaction step by step in the R programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the jq programming language