How to resolve the algorithm Dining philosophers step by step in the PicoLisp programming language
How to resolve the algorithm Dining philosophers step by step in the PicoLisp 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 PicoLisp programming language
Source code in the picolisp programming language
(de dining (Name State)
(loop
(prinl Name ": " State)
(state 'State # Dispatch according to state
(thinking 'hungry) # If thinking, get hungry
(hungry # If hungry, grab random fork
(if (rand T)
(and (acquire leftFork) 'leftFork)
(and (acquire rightFork) 'rightFork) ) )
(hungry 'hungry # Failed, stay hungry for a while
(wait (rand 1000 3000)) )
(leftFork # If holding left fork, try right one
(and (acquire rightFork) 'eating)
(wait 2000) ) # then eat for 2 seconds
(rightFork # If holding right fork, try left one
(and (acquire leftFork) 'eating)
(wait 2000) ) # then eat for 2 seconds
((leftFork rightFork) 'hungry # Otherwise, go back to hungry,
(release (val State)) # release left or right fork
(wait (rand 1000 3000)) ) # and stay hungry
(eating 'thinking # After eating, resume thinking
(release leftFork)
(release rightFork)
(wait 6000) ) ) ) ) # for 6 seconds
(setq *Philosophers
(maplist
'((Phils Forks)
(let (leftFork (tmp (car Forks)) rightFork (tmp (cadr Forks)))
(or
(fork) # Parent: Collect child process IDs
(dining (car Phils) 'hungry) ) ) ) # Initially hungry
'("Aristotle" "Kant" "Spinoza" "Marx" "Russell")
'("ForkA" "ForkB" "ForkC" "ForkD" "ForkE" .) ) )
(push '*Bye '(mapc kill *Philosophers)) # Terminate all upon exit
You may also check:How to resolve the algorithm Spiral matrix step by step in the Factor programming language
You may also check:How to resolve the algorithm Last Friday of each month step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm First-class functions step by step in the Maxima programming language
You may also check:How to resolve the algorithm Non-decimal radices/Convert step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Five weekends step by step in the Sidef programming language