473,503 Members | 3,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dinning Philosopher's Solution

26 New Member
hi,

i have just started learning java threads. i have been given the task of solving dinning philosopher's problem by using only wait() and notify() methods. i cant use semaphores or monitors.
i have created philosophers as 5(0-4) threads and forks as objects upon which wait() and notify() methods can b called. i have writetn the code which i m posting 4 ur kind consideraton. my problem is that 0,2 and 4 philosophers are switching forks among them and the remaining 2 threads(1,3) are suffering 4m starvation. i dont know y?????
can anyone plz tell me that what i m doing wrong in this code and how its solution can be developed such that there is no deadlock,starvation or livelock.
plz plz plz help me.....


P.java(Philosopher's class)


public class P implements Runnable
{

String n;
F f1,f2;
Thread t;
int s=0;
int r=0;

public P(F ff1,F ff2,String name)
{
n=name;
f1=ff1;
f2=ff2;
t=new Thread(this,n);
t.start();
}






public void run()
{

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

r=f1.get(); // get 1st fork
if(r==5) // if 1st fork has been acquired, then request 2nd one
s=f2.get();

f1.eat(n);

f1.left(n,f2); // place tae 2 forks back on table

try
{Thread.sleep(50);}
catch(Exception e)
{System.out.println(e);}



}// end for

}// end run

}


T.java(Main class)


public class T
{


public static void main(String args[])
{

F f[]=new F[5];
P p[]=new P[5];


for(int i=0;i<5;i++)
{
f[i]=new F();
}// end for


for(int i=0;i<5;i++)
{
String name="Philosopher # "+i;
if(i==0)
{p[0]=new P(f[4],f[i],name);} // sharing 2 fork objects with every philosopher
else
{p[i]=new P(f[i-1],f[i],name);}
}


}// end main


}//end class



F.java(Forks class)



public class F
{

private int fs=1; //represents availablity of forks

//if fs=1 -----> fork is available
//if fs=-1 -----> fork is not available

public F() //constructor
{
fs=1; // making all forks available at start of the program
}





public synchronized int get()
{
while(fs==-1)
{
try{wait();}
catch(Exception e)
{System.out.println(e);}
}//end while
fs=-1;
notifyAll();
return(5);

}// end get




public synchronized void eat(String h)
{
System.out.println(h+" is eating...");
try{Thread.sleep(1300);}
catch(Exception e)
{System.out.println(e);}
}// end eat




public synchronized void left(String h,F f2)
{
System.out.println(h+ " has finished eating ....");
this.fs=1;
notifyAll();
f2.fs=1;
notifyAll();
}// end left

}
Sep 21 '07 #1
1 1297
JosAH
11,448 Recognized Expert MVP
I don't think you should make a Fork wait until it's available. A philosopher should
wait until both his/her forks are available. A philosopher who puts down his/her
forks should notify all other philosophers.

kind regards,

Jos

ps. the way you name your classes, members and identifiers is quite confusing;
please write them out in full; it pays back w.r.t. the readability and understandability
of your source code.

kind regards,

Jos
Sep 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2363
by: Wayne Wengert | last post by:
I have a solution. I want to clone it to try some different options. Is there a way to open Solution A, save it as Solution B and then be able to try some things in Solution B without affecting...
47
18844
by: Matt Kruse | last post by:
http://www.mattkruse.com/temp/css_expressions.html One of the standard CSS questions is "how can I shade every other table row a different color with CSS?" The answers are usually 1) you can't...
10
510
by: Simon Jefferies | last post by:
Hello, I'm getting a strange problem where when I load up my solution that has three projects:- a managed C++ .NET, one C++ lib, and a VB library. It locks up and doesn't load completely, I...
0
1626
by: TEK | last post by:
Hello We have a quite huge project. To limit the solution size, rebuild time and so on we have divided the project in two different solution. One solution that holds the buiness entities, or...
5
2203
by: kai | last post by:
Hi, I use XP Pro and VS2005. I find when I create a new Website, I only see the Project node not Solution. After I add sedcond project under the solution tree, then I see the solution node. How...
3
2142
by: Manikandan | last post by:
Hi, I'm copying projects from a solution in the vss. In my local system i created solution ,added that project(make modification related to solution) When i added this solution to vss, projects...
0
2328
by: techie | last post by:
List of Solution manuals: Engineering Circuit Analysis 6Ed - Hayt Solutions Manual Norbury - Solutions manual for mechanics and thermodynamics Physics For Scientists And Engineers - Solution...
9
2366
by: GaryDean | last post by:
I know this is a vs issue but nobody seems to answer posts over there... In VS 2008 I can't seem to get a multiple project solution. If I create a blank solution and add a project to it, it...
16
3973
by: =?Utf-8?B?RHdlZWJlcmVsbGE=?= | last post by:
I created an Access 2007 application for my customer. The application is shared by three employees on a server. It maintains a contact list including financial data and social security numbers. ...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.