Click here to Skip to main content
15,924,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
With the help of "Monitor" I made philosopher take 2 forks at one time to eat,how can I make that philosopher takes one fork and then waits for another one?
Posted
Updated 26-Apr-11 11:07am
v2

A classical problem in its pure form looks like this: each form is a thread looping a communication tasks from a thread representing a philosopher. This communication primitive can be EventWaitHandler or… a socket. First step in the loop waits for "PickUp", a second one for "PutDown".

A simplified "cheating" variant is of a form is a shared resource protected by the regular lock. A philosopher simply gets the lock of left form then the lock of right fork, than sleeps for a wile then releases the locks in the same order. With certain timing it will create a famous "circular" deadlock when each philosopher gets one fork and waits for another one forever.

Are you supposed to demonstrate this deadlock or its resolution as well. Normally, it is required to make both versions.

Unfortunately, the task could absorb some hour of my time or so, which I cannot afford; and you're supposed to solve the problem independently. It's very good for your learning to do this exercise!

Good luck,
—SA
 
Share this answer
 
v2
Comments
tnd2000 27-Apr-11 12:13pm    
I didn't vote, but I think the OP wanted to know how Monitor works. Not how different threads can communicate via events or sockets.
Sergey Alexandrovich Kryukov 27-Apr-11 15:21pm    
I'm explaining classical problem. I did this implementation with two or tree variants. Nothing special about Monitor, roughly idea is close to the one of lock.
--SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Apr-11 17:27pm    
Not a very good article; also, it does not follow original requirements (kind of cheating). Nevertheless, the reference deserves my 4.
--SA
Sergey Alexandrovich Kryukov 26-Apr-11 17:38pm    
I put some details on the problem, please see my answer.
--SA
Tirmit 27-Apr-11 2:59am    
thx ,it helped me,but now i`ve got 1 exception sometimes:
cause 1 philosopher can take his fork of one fork of his two neighbour's.And from time to time the is exception in Monitor.Exit(fork[number]);
Tirmit 27-Apr-11 3:04am    
and how did they do this Monitor.TryEnter(right, ref lockedR); I mean ref lockedR ,I `ve got only the possibility to write "int milisec. Timeout"
tnd2000 27-Apr-11 11:59am    
Monitor.TryEnter has many overloads, one of them takes bool as a second parameter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900