![]() |
|
#1
|
|||
|
|||
Dining Philosophers programI'm face a problem when doing this program.Can any one giving me some instruction??
There are N philosophers seated at a circular table, sharing chopsticks, so that no two adjacent philosophers may eat at the same time. Each philosopher is modeled by a process executing the following code: CPP / C++ / C Code:
I need do write the routine get_hungry_and_try_to_eat() and think().And the routine must obtain a chopstick from the left and right side.Once it has both chopsticks, it should eat() and then release them. Since the philosophers are concurrent threads/process, any access they make to common data must be appropriately protected. This program also need too avoid busy-waiting. I think a few days,but still can't get the concept.Any one can help me......Thanks very very much..... Need help poor person. Last edited by dsmith : 12-Feb-2004 at 10:35.
Reason: C syntax highlighting.
|
|
#2
|
||||
|
||||
|
Hi Sam. I know that you may disagree, but what a cool problem!
I am thinking a doubly-linked list representing the philosophers is the way that I would go, then to access the adjacent philosophers would simply be a call to prev and next. An array would work as well, but not as dynamic and without the end looping to create the "circle" A couple of questions come to mind:
Anyway, I would love to see this when you get it done. If you are struggling, you should try to set up your structures, etc. and post some code if you are struggling with it so that the people here can help with the specific problems. BTW - You can post highlighted c code by using [C] and [/C]. I editted your post to do this. |
|
#3
|
|||
|
|||
Thank DsmithThank Dsmith
Refer to your question, let said now we hv 5 people then we only hv 5 chopsticks,in both left and right.Not 5 set.That mean N people then got N Chopsticks. You right,they have to get one from the left and one from the right to eat.But each time,only one ppl can eat, other ppl hv to wait after it finish eating and pass the chopsticks again to other ppl. Think() is actually the philosopher can do anything.LIke maybe they eating, hungry , waiting their turn to eat or etc. However thank for you adviser,if you got any idea,you are welcome to tell me.10 |
|
#4
|
||||
|
||||
|
Hi Sam. I think I understand a bit better now. Each philosopher has a chopstick, but he can not use it to eat. He has to obtain the chopsticks from his neighbor. And according to the original post, philosophers seated next to each other can not eat at the same time.
I still like the doubly-linked array because it is dynamic. You can decide how many philosophers you have at run time and it would be easy to add more as you go on. But the way that you have written your philosopher function makes it appear that you are going to use an array, which will work. I would define a philosopher by a structure like: CPP / C++ / C Code:
Each of these is a flag. Each philosopher should be initialized to have a chopstick (ie chopstick = 1) and to not be eating (ie eating = 0). Then to find out if a philosopher can eat would be something like: CPP / C++ / C Code:
Then when he is done eating CPP / C++ / C Code:
You would have to check your bounds in the above examples. So maybe you could track N (number of philosophers starting at 0) as a global and then in your try_to_eat function, start it with CPP / C++ / C Code:
Hope this helps. I would appreciate you posting back with this when you get it done. I would like to see how it ends up... |
|
#5
|
||||
|
||||
ahaaaaa!I just realized that the following code:
CPP / C++ / C Code:
CPP / C++ / C Code:
Also, note a couple of corrections in the original code. |
|
#6
|
||||
|
||||
|
That is a great math trick Walt! I tested it because I didn't quite follow the logic. I would never had got that in a million years. If you told me to write that as a one liner I would have written:
CPP / C++ / C Code:
Yours is much cleaner. I am going to have to remember that one! |
|
#7
|
|||
|
|||
|
Thank dsmith and WaltP, i hv finish the part of the program. But still encounter a problem, so any one of you can help me to solve it??
Thanky.Following is the code of the program, still got some bug inside : CPP / C++ / C Code:
|
|
#8
|
||||
|
||||
|
Hi Sam. I haven't gone through the entire logic yet, but there are a few things that are holding it from compiling:
Try to get this to compile and see what you come up with. I don't know if I 100% follow your logic yet. Get your code to compile and repost it. If it doesn't function like you expect, I will take a closer look. |
|
#9
|
|||
|
|||
Program with bug(s)... Need help!!Dear dSmith,
Hello. Below is my developed program but got some problems. Can you help me to solve them? CPP / C++ / C Code:
Actually this problem I need to use semaphore and threads to solve it but i didn't. I just use random time to control the process. And everytime when I run this program, for example I key in 2 or 4 philosophers, don't know why the first philosopher who will eat loses his right chopstick. But if I key in other values like 5 or 6, it won't come out such problem. While key in other larger values, it will say some of the philosophers' chopsticks are not available at the run time but actually they are supposed be available. Can you help me find out why? I think it is the rand() problem, is it? Besides since I must define the N value, so I define 100. But when the program starts, I let the user keys in the number of philosophers who will eat together. Does it make sense? Can I do like such way? Anyway, I'm appreciate for your help!! Thanks a lot!! May you have a nice day! Bye! |
|
#10
|
||||
|
||||
|
Quote:
Quote:
I would use dynamic memory allocation for this instead of arrays. So define your philarray as a pointer and then allocate it after you ask for the number of philosophers. And then access it with a pointer. CPP / C++ / C Code:
If this *HAS* to be done with semaphores you should really read up on the post above and do some of your own testing. If you want to work with what you have, let me know and I will take a look at some of the other errors you are getting. |
Recent GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| Type casts ? | kai85 | C++ Forum | 12 | 23-Jun-2005 13:04 |
| [TUTORIAL] Calling an external program in C (Linux) | dsmith | C Programming Language | 4 | 22-Apr-2005 14:30 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 08:10 |
| Need help with a C program (Long) | McFury | C Programming Language | 3 | 29-Apr-2004 21:06 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The