Dining Philosophers Problem

Java

Public Domain

Some work from college

Download (right click, save as, rename as appropriate)

Embed

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
Three different files here.

DiningPhilosopher.java
---------------------
import java.util.Scanner;

public class DiningPhilosopher 
{
	public static int num_philosophers = 0;
	public static int random_think1 = 0;
	public static int random_think2 = 0;
	public static int eat_time = 0;
	
	public static void main(String[] args) 
	{
		
		Scanner in = new Scanner(System.in);
		
		System.out.println("Please enter the number of Philosophers:");
		num_philosophers = in.nextInt();
		if(num_philosophers < 0)
		{
			System.out.println("Input Error!");
			System.exit(0);
		}
		
		System.out.println("Please enter the first random number to 'think'(smaller number first)");
		random_think1 = in.nextInt();
		System.out.println("Please enter the second random number to 'think'(larger number second)");
		random_think2 = in.nextInt();
		
		if(random_think2 < random_think1 || random_think1 < 0 || random_think2 < 0)
		{
			System.out.println("Input Error!");
			System.exit(0);
		}
		
		System.out.println("Please enter the amount to eat");
		eat_time = in.nextInt();
		
		if(eat_time < 0)
		{
			System.out.println("Input Error!");
			System.exit(0);
		}
		
		forks fork = new forks();
		
		int x = 1;
		while(x <= num_philosophers)
		{
			philosopher p1 = new philosopher(fork, fork, x);
			Thread t1 = new Thread(p1);
			t1.start();
			x++;
		}
			
	}






} 

forks.java
------------
import java.util.Random;
import java.util.concurrent.Semaphore;

public class forks
{

	private int temp = DiningPhilosopher.num_philosophers;	
	private Random rand = new Random();
	private Semaphore sem = new Semaphore(temp);
	private boolean right_fork = false;
	private boolean left_fork = false;


	public void use(int philosopher)
	{
		int think_time = rand.nextInt(DiningPhilosopher.random_think2) + DiningPhilosopher.random_think1;
		
		try
		{
			if(sem.availablePermits() == 0)
			{
				
				think_time = rand.nextInt(DiningPhilosopher.random_think2) + DiningPhilosopher.random_think1;
				System.out.println("philosopher "+philosopher+" is thinking for "+think_time+".");
				Thread.sleep(think_time);
			
			}
			else
			{
				left_fork = sem.tryAcquire();
				if(!left_fork)
				{
					
					think_time = rand.nextInt(DiningPhilosopher.random_think2) + DiningPhilosopher.random_think1;
					System.out.println("philosopher "+philosopher+" is thinking for "+think_time+".");
					Thread.sleep(think_time);

				}
				else
				{
					System.out.println("philosopher "+philosopher+" got the left fork");
					right_fork = sem.tryAcquire();
					if(!right_fork)
					{
						think_time = rand.nextInt(DiningPhilosopher.random_think2) + DiningPhilosopher.random_think1;
						System.out.println("philosopher "+philosopher+" is waiting for another fork for "+think_time+".");
						Thread.sleep(think_time);
						
					}
					else
					{
						
						System.out.println("philosopher "+philosopher+" is eating.");
						Thread.sleep(rand.nextInt(DiningPhilosopher.eat_time));
						//sem.release();
					}


				}
			}
		}catch (InterruptedException ie)
		{
			ie.printStackTrace();
		} finally
		{
			System.out.println("philosopher "+philosopher+" puts the forks down.");
			sem.release();
		}


	}





}

philosopher.java
------------------
import java.util.Random;

public class philosopher implements Runnable
{
	private Random rand = new Random();

	private forks right_fork;
	private forks left_fork;

	private int philosopher;


	public philosopher(forks f_right, forks f_left, int philosopher)
	{
		this.right_fork = f_right;
		this.left_fork = f_left;
		this.philosopher = philosopher;
		Thread t1 = new Thread(this);
		t1.start();	

	}
	public void run()
	{
		try
		{
			Thread.sleep(rand.nextInt(5000));
			right_fork.use(philosopher);
			left_fork.use(philosopher);
		}
		catch (InterruptedException ie)
		{
			ie.printStackTrace();				
		}





	}


}