5 Replies - 446 Views - Last Post: 03 November 2017 - 08:12 AM Rate Topic: -----

#1 kuroshiro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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{
				Thread.sleep(ms);
			}
			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);
            }
             
	    		
	    	Thread t = new Thread( philosophers[i],"Philosopher "+ (i+1));
	    	t.start();
    	

	    }

//	    while (true) {
//	      try {
//	        
//	        Thread.sleep(500);
//
//	        // check for deadlock
//	        boolean deadlock = true;
//	        for (Fork f : forks) {
//	          if (f.isUsed()==false) {
//	            deadlock = false;
//	            break;
//	          }
//	        }
//	        if (deadlock) {
//	          Thread.sleep(500);
//	          System.out.println("Deadlock!");
//	          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

:code:

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  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,896
  • Joined: 12-June 08

Re: Dinning philosophers problem using semaphore

Posted 02 November 2017 - 08:41 AM

Please copy/paste your error messages here.
Was This Post Helpful? 0
  • +
  • -

#3 kuroshiro  Icon User is offline

  • New D.I.C Head

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

Re: Dinning philosophers problem using semaphore

Posted 02 November 2017 - 08:51 AM

View Postmodi123_1, on 02 November 2017 - 08:41 AM, said:

Please copy/paste your error messages here.

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

View Postmodi123_1, on 02 November 2017 - 08:41 AM, said:

Please copy/paste your error messages here.

When philo4 puts the fork down, the program stops. It should continue to run
Was This Post Helpful? 0
  • +
  • -

#4 IOExceptional  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 135
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 kuroshiro  Icon User is offline

  • New D.I.C Head

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

Re: Dinning philosophers problem using semaphore

Posted 03 November 2017 - 07:52 AM

View PostIOExceptional, 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
Was This Post Helpful? 0
  • +
  • -

#6 IOExceptional  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 135
  • Joined: 15-September 16

Re: Dinning philosophers problem using semaphore

Posted 03 November 2017 - 08:12 AM

Post the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1