Dinning Philosopher Problem with Semaphore and Threads.

sem_wait(Semaphore s){
while(s<=0);
s--;
}
sem_post(Semaphore s){
s++;
}
Semaphore s=new Semaphore(1) // initialize the semaphore variable
to 1
sem_wait(s);
//Critical Section here
sem_post(s);

Dinning Philosopher Problem-

Dinning philosopher problem

Dinning Philosopher Problem Solution


repeat

wait(chopstick[i]);wait(chopstick[(i+1) mod 5]);
. . .
eat
. . .
signal(chopstick[i]);
signal(chopstick[(i+1) mod 5]);
. . .
think
. . .

until false;
class Philosopher implements Runnable {
private int id;
private int amount=0;
// The chopsticks this philosopher may use
private Semaphore leftChopstick;
private Semaphore rightChopstick;

public Philosopher(int id, Semaphore leftChopstick, Semaphore rightChopstick) {
this.id = id;
this.leftChopstick = leftChopstick;
this.rightChopstick = rightChopstick;
}
public void run() {
//...
}
//other codes here
}
private void think() throws InterruptedException {
System.out.println("Philosopher " + id + " is thinking.\n");
System.out.flush();
Thread.sleep(new Random().nextInt(10));
}
private void pickUpLeftChopstick() throws InterruptedException{
if(leftChopstick.availablePermits() ==0){
System.out.println("Philosopher " +id +" is waiting for left chopstick");
}
leftChopstick.acquire();
System.out.println("Philosopher " + id + " is holding left chopstick.\n");
System.out.flush();
}
private void pickUpRightChopstick() throws InterruptedException{
if(rightChopstick.availablePermits() ==0){
System.out.println("Philosopher " +id +" is waiting for right chopstick");
}
rightChopstick.acquire();
System.out.println("Philosopher " + id + " is holding right chopstick.\n");
System.out.flush();
}
Release the locks for chopsticks in the putDownChopstick method

private void putDownChopsticks() {
leftChopstick.release();
rightChopstick.release();
System.out.println("Philosopher " + id + " ate "+amount+"% and"+" released left and right sticks \n");
}
private void eat() throws InterruptedException {
System.out.println("Philosopher " + id + " is eating.\n");
System.out.flush();
amount+=20;
Thread.sleep(new Random().nextInt(10));
}
public class DiningProblem {    
private static final int n = 5;
public static void main(String[] args) {

Semaphore[] chopsticks = new Semaphore[n];
for (int i = 0; i < n; i++) {
chopsticks[i] = new Semaphore(1);
}

Philosopher[] philosophers = new Philosopher[n];
for (int i = 0; i < n; i++) {
philosophers[i] = new Philosopher(i, chopsticks[i], chopsticks[(i + 1) % n]);
new Thread(philosophers[i]).start();
}
}
}
Philosopher 0 is thinking.
Philosopher 1 is thinking.
Philosopher 2 is thinking.
Philosopher 2 is holding left chopstick.
Philosopher 3 is thinking.
Philosopher 2 is holding right chopstick.
Philosopher 2 is eating.
Philosopher 4 is thinking.
Philosopher 3 is waiting for left chopstick
Philosopher 1 is holding left chopstick.
Philosopher 0 is holding left chopstick.
Philosopher 1 is waiting for right chopstick
Philosopher 0 is waiting for right chopstick
Philosopher 2 ate 20% and released left and right sticks …

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store