# Dinning philosophers problem using semaphore

Page 1 of 1

## 5 Replies - 532 Views - Last Post: 03 November 2017 - 08:12 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407346&amp;s=6fdc54d630217253cb5e0d74eb0851f3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kuroshiro

Reputation: 0
• Posts: 3
• Joined: 02-November 17

# Dinning philosophers problem using semaphore

Posted 02 November 2017 - 08:28 AM

I'm trying to describe the dining philosophers problem using semaphore. but it causes errors. I checked and I think its algorithm is correct. But my program stopped when a someone put the two fork down. please help me. I'm very stuck
this is my code

Fork.class
```public class Fork {

private boolean inuse;
Semaphore sem;

public Fork(){

inuse = false;
sem = new Semaphore(1);
}
public void getFork()
{
try
{
while(inuse)
{
try
{
sem.acquire();
}
catch(InterruptedException e) {}
}
inuse = true;
}catch(Exception e){}
}
public void putFork()
{
try
{
inuse = false;
sem.release();
}
catch (Exception e){}
}
public boolean isUsed()
{
return inuse;
}
}
```

philosopher class
```public class philosopher extends Thread {

public int number;
public Fork leftFork;
public Fork rightFork;

philosopher(int num, Fork left, Fork right) {
number = num;
leftFork = left;
rightFork = right;
}

public void run()
{
think();
eat();
}

void think()
{
System.out.println("Philosopher philo"+number+ " thinking...");
Log.Delay(500);
}

public void take_fork()
{
leftFork.getFork(); System.out.println("Philosopher philo"+number+ ": take left fork");
rightFork.getFork(); System.out.println("Philosopher philo"+number+ ": take right fork");

}
public void put_fork()
{
leftFork.putFork(); System.out.println("Philosopher philo"+number+ ": put left fork");
rightFork.putFork();System.out.println("Philosopher philo"+number+ ": put right fork");
}

public void eat()
{
if(!leftFork.isUsed())
{
if(!rightFork.isUsed())
{
take_fork();
System.out.println("Philosopher philo"+number+ " eating...");
Log.Delay(500);

put_fork();
}
}
}

}
class Log{

public static void msg(String msg){
System.out.println(msg);
}
public static void Delay(int ms){
try{
}
catch(InterruptedException ex){ }
}
}

```

Main class

```public class dinner_philosopher2 {

public static void main(String argv[]) {

int philosophersNumber = 5;

final philosopher philosophers[] = new philosopher[philosophersNumber];
Fork forks[] = new Fork[philosophersNumber];

for (int i = 0; i < philosophersNumber; i++) {
forks[i] = new Fork();
}

for (int i = 0; i < philosophersNumber; i++) {

Fork leftfork = forks[i];
Fork rightfork = forks[(i+1)% philosophersNumber];

if (i == philosophers.length - 1) {

// The last philosopher picks up the right fork first
philosophers[i] = new philosopher(i,rightfork, leftfork);
} else {
philosophers[i] = new philosopher(i,leftfork, rightfork);
}

t.start();

}

//	    while (true) {
//	      try {
//
//
//	        for (Fork f : forks) {
//	          if (f.isUsed()==false) {
//	            break;
//	          }
//	        }
//	          break;
//	        }
//	      }
//	      catch (Exception e) {
//	        e.printStackTrace(System.out);
//	      }
//	    }
//	    System.exit(0);
}

}

```

and the result

Philosopher philo0 thinking...
Philosopher philo2 thinking...
Philosopher philo1 thinking...
Philosopher philo3 thinking...
Philosopher philo4 thinking...
Philosopher philo3: take left fork
Philosopher philo3: take right fork
Philosopher philo4: take left fork
Philosopher philo0: take left fork
Philosopher philo1: take left fork
Philosopher philo1: take right fork
Philosopher philo1 eating...
Philosopher philo3 eating...
Philosopher philo3: put left fork
Philosopher philo0: take right fork
Philosopher philo4: take right fork
Philosopher philo4 eating...
Philosopher philo1: put left fork
Philosopher philo0 eating...
Philosopher philo3: put right fork
Philosopher philo1: put right fork
Philosopher philo0: put left fork
Philosopher philo0: put right fork
Philosopher philo4: put left fork
Philosopher philo4: put right fork

This post has been edited by modi123_1: 02 November 2017 - 08:40 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.

Is This A Good Question/Topic? 0

## Replies To: Dinning philosophers problem using semaphore

### #2 modi123_1

• Suitor #2

Reputation: 14094
• Posts: 56,469
• Joined: 12-June 08

## Re: Dinning philosophers problem using semaphore

Posted 02 November 2017 - 08:41 AM

### #3 kuroshiro

Reputation: 0
• Posts: 3
• Joined: 02-November 17

## Re: Dinning philosophers problem using semaphore

Posted 02 November 2017 - 08:51 AM

modi123_1, on 02 November 2017 - 08:41 AM, said:

Its error is stop, if it's true then it will run forever or when you turn it off

modi123_1, on 02 November 2017 - 08:41 AM, said:

When philo4 puts the fork down, the program stops. It should continue to run

### #4 IOExceptional

Reputation: 27
• Posts: 140
• Joined: 15-September 16

## Re: Dinning philosophers problem using semaphore

Posted 02 November 2017 - 09:21 AM

One possible reason for the lack of error messages is the lack of error handling.

Quote

```catch(InterruptedException e) {}
```

Quote

```catch (Exception e){}
```

Try to report the thrown exceptions (a simple e.printStackTrace() will do), and see if the output changes.

### #5 kuroshiro

Reputation: 0
• Posts: 3
• Joined: 02-November 17

## Re: Dinning philosophers problem using semaphore

Posted 03 November 2017 - 07:52 AM

IOExceptional, on 02 November 2017 - 09:21 AM, said:

One possible reason for the lack of error messages is the lack of error handling.

Quote

```catch(InterruptedException e) {}
```

Quote

```catch (Exception e){}
```

Try to report the thrown exceptions (a simple e.printStackTrace() will do), and see if the output changes.

I tried it. but nothing happened

### #6 IOExceptional

Reputation: 27
• Posts: 140
• Joined: 15-September 16

## Re: Dinning philosophers problem using semaphore

Posted 03 November 2017 - 08:12 AM

Post the code.