You are on page 1of 7

Course Name: Real Time System Lab Course Code: CSP-457

Experiment-1.4

Aim
Write a program to simulate the concept of Dining-Philosophers Problem

Software Required
MATLAB R2022a (MATLAB online), Java Eclipse IDE

Description
The dining-philosophers problem is considered a classic synchronization problem because it is an example of
a large class of concurrency-control problems. It is a simple representation of the need to allocate several
resources among several processes in a deadlock-free and starvation-free manner. Consider five philosophers
who spend their lives thinking and eating. The philosophers share a circular table surrounded by five chairs,
each belonging to one philosopher. In the center of the table is a bowl of rice, and the table is laid with five
single chopsticks. When a philosopher thinks, she does not interact with her colleagues. From time to time, a
philosopher gets hungry and tries to pick up the two chopsticks that are closest to her (the chopsticks that are
between her and her left and right neighbors). A philosopher may pick up only one chopstick at a time.
Obviously, she cam1ot pick up a chopstick that is already in the hand of a neighbor. When a hungry philosopher
has both her chopsticks at the same time, she eats without releasing her chopsticks. When she is finished eating,
she puts down both of her chopsticks and starts thinking again. The dining-philosophers problem may lead to
a deadlock situation and hence some rules have to be framed to avoid the occurrence of deadlock.

Algorithm
To solve this Dead Lock situation, Last philosopher (anyone can do this)

1. First try to take right side fork and then left side fork.

2. In our example 5th person tries to take 4th Fork Instead of 5th one

3. Since 4th Fork already taken by 4th the person, he gets nothing. But he left 5th Fork

4. Now the first person will take this 5th Fork and complete dinner and make 1st and 5th available for
remaining people

5. Next 2nd person takes 1st fork and completes and releases 1st and 2nd

Name: Shubham Bharti UID: 19BCS2848


Course Name: Real Time System Lab Course Code: CSP-457

6. This continuous until all finish’s dinner.

Implementation:
import java.util.concurrent.Semaphore; import
java.util.concurrent.ThreadLocalRandom; public
class DiningPhilosophersProblem
{
static int philosopher = 5; static Philosopher philosophers[] =
new Philosopher[philosopher];
static Chopstick chopsticks[] = new Chopstick[philosopher]; static class
Chopstick
{
public Semaphore mutex = new Semaphore(1);
void grab()
{
try
{
mutex.acquire();
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}

Name: Shubham Bharti UID: 19BCS2848


Course Name: Real Time System Lab Course Code: CSP-457

void release()
{
mutex.release();
}

boolean isFree()
{
return mutex.availablePermits() > 0;
}
}
static class Philosopher extends Thread
{
public int number; public
Chopstick leftchopstick; public
Chopstick rightchopstick;

Philosopher(int num, Chopstick left, Chopstick right)


{
number = num;
leftchopstick = left; rightchopstick
= right;
}

public void run()

Name: Shubham Bharti UID: 19BCS2848


Course Name: Real Time System Lab Course Code: CSP-457

{ while
(true)
{
leftchopstick.grab(); System.out.println("Philosopher " + (number+1) + "
grabs left chopstick."); rightchopstick.grab();
System.out.println("Philosopher " + (number+1) + " grabs right chopstick."); eat();
leftchopstick.release();
System.out.println("Philosopher " + (number+1) + " releases left chopstick.");
rightchopstick.release();
System.out.println("Philosopher " + (number+1) + " releases right chopstick.");
}
}
void
eat() {
try
{
int sleepTime = ThreadLocalRandom.current().nextInt(0, 1000);
System.out.println("Philosopher " + (number+1) + " eats for " + sleepTime +"ms");
Thread.sleep(sleepTime);
}
catch (Exception e)
{
e.printStackTrace(System.out);

Name: Shubham Bharti UID: 19BCS2848


Course Name: Real Time System Lab Course Code: CSP-457
}
}
public static void main(String args[])
{ for (int i = 0; i <
philosopher; i++)
{
chopsticks[i] = new Chopstick();
}

for (int i = 0; i < philosopher; i++)


{
philosophers[i] = new Philosopher(i, chopsticks[i], chopsticks[(i + 1) % philosopher]);
philosophers[i].start();
}
while (true)
{ try
{
Thread.sleep(1000);
boolean deadlock = true; for
(Chopstick cs : chopsticks)
{
if
(cs.isFree())
{
deadlock = false;

Name: Shubham Bharti UID: 19BCS2848


Course Name: Real Time System Lab Course Code: CSP-457
break;
}
} if
(deadlock)
{
Thread.sleep(1000);
System.out.println("Everyone Eats");
break;
}
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}
System.out.println("Exit The Program!");
System.exit(0);
}
}

Name: Shubham Bharti UID: 19BCS2848


Course Name: Real Time System Lab Course Code: CSP-457
Output

Name: Shubham Bharti UID: 19BCS2848

You might also like