How to resolve the algorithm Dining philosophers step by step in the EchoLisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Dining philosophers step by step in the EchoLisp 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 EchoLisp programming language

Source code in the echolisp programming language

(lib 'tasks)

(define names #(Aristotle Kant Spinoza Marx Russell))
(define abouts #("Wittgenstein" "the nature of the World" "Kant"  "starving" 
    "spaghettis" "the essence of things" "Ω" "📞" "⚽️" "🍅" "🌿" 
    "philosophy" "💔"  "👠" "rosetta code" "his to-do list" ))
(define (about) (format "thinking about %a." (vector-ref abouts (random (vector-length abouts)))))

;; statistics
(define rounds (make-vector 5 0))
(define (eat i) (vector-set! rounds i (1+ (vector-ref rounds i))))
 
;; forks are resources = semaphores
(define (left i) i)
(define (right i) (modulo (1+ i) 5))
(define forks (for/vector ((i 5)) (make-semaphore 1)))
(define (fork i) (vector-ref forks i))

(define laquais (make-semaphore 4))

;; philosophers tasks
(define (philo i)
;; thinking
       (writeln (vector-ref names i) (about))
    (sleep (+ 2000 (random 1000)))
    (wait laquais)
;; get forks
       (writeln (vector-ref names i) 'sitting)
    (wait (fork (left i)))
    (wait (fork (right i)))
       (writeln (vector-ref names i) 'eating)
       (eat i)
       (sleep (+ 6000 (random 1000)))
;; put-forks
    (signal (fork (left i)))
    (signal (fork (right i)))
    (signal laquais)
  i)
(define tasks (for/vector ((i 5)) (make-task philo i)))


(define (observe dummmy)
		(writeln 'observer 'rounds= rounds)
		#t)
(define observer (make-task observe #t ))

(define (dinner) 
	(task-run observer 5000)
	(for ((t tasks)) (task-run t)))

(dinner)


  

You may also check:How to resolve the algorithm History variables step by step in the J programming language
You may also check:How to resolve the algorithm First-class functions step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the FutureBasic programming language