How to resolve the algorithm Dining philosophers step by step in the Perl programming language
How to resolve the algorithm Dining philosophers step by step in the Perl 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 Perl programming language
Source code in the perl programming language
use threads;
use threads::shared;
my @names = qw(Aristotle Kant Spinoza Marx Russell);
my @forks = ('On Table') x @names;
share $forks[$_] for 0 .. $#forks;
sub pick_up_forks {
my $philosopher = shift;
my ($first, $second) = ($philosopher, $philosopher-1);
($first, $second) = ($second, $first) if $philosopher % 2;
for my $fork ( @forks[ $first, $second ] ) {
lock $fork;
cond_wait($fork) while $fork ne 'On Table';
$fork = 'In Hand';
}
}
sub drop_forks {
my $philosopher = shift;
for my $fork ( @forks[$philosopher, $philosopher-1] ) {
lock $fork;
die unless $fork eq 'In Hand';
$fork = 'On Table';
cond_signal($fork);
}
}
sub philosopher {
my $philosopher = shift;
my $name = $names[$philosopher];
for my $meal ( 1..5 ) {
print $name, " is pondering\n";
sleep 1 + rand 8;
print $name, " is hungry\n";
pick_up_forks( $philosopher );
print $name, " is eating\n";
sleep 1 + rand 8;
drop_forks( $philosopher );
}
print $name, " is done\n";
}
my @t = map { threads->new(\&philosopher, $_) } 0 .. $#names;
for my $thread ( @t ) {
$thread->join;
}
print "Done\n";
__END__
#!/usr/bin/perl
use common::sense;
use Coro;
use AnyEvent;
use Coro::AnyEvent;
use EV;
my @philosophers = qw(Aristotle Kant Spinoza Marx Russell);
my @forks = (1..@philosophers);
my @fork_sem;
$fork_sem[$_] = Coro::Semaphore->new for (0..$#philosophers);
for(my $i = $#philosophers; $i >= 0; $i--){
say $philosophers[$i] . " has fork #" . $forks[$i] . " and fork #" . $forks[$i-1];
async {
my ($name, ,$no, $forks_got) = (@_);
$Coro::current->{desc} = $name;
Coro::AnyEvent::sleep(rand 4);
while(1){
say $name . " is hungry.";
$$forks_got[$no]->down();
Coro::AnyEvent::sleep(rand 1); #Let's make deadlock!
$$forks_got[$no-1]->down();
say $name . " is eating.";
Coro::AnyEvent::sleep(1 + rand 8);
$$forks_got[$no]->up();
$$forks_got[$no-1]->up();
say $name . " is thinking.";
Coro::AnyEvent::sleep(1 + rand 8);
}
}($philosophers[$i], $i, \@fork_sem);
}
EV::loop;
You may also check:How to resolve the algorithm Maximum triangle path sum step by step in the zkl programming language
You may also check:How to resolve the algorithm File size step by step in the E programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the BASIC programming language
You may also check:How to resolve the algorithm Find if a point is within a triangle step by step in the Racket programming language
You may also check:How to resolve the algorithm Function definition step by step in the AppleScript programming language