Dinning Philosophers' Problem Using Conditional Variables

In summary, the problem in this pseudo-code is the incorrect order of the signal calls in the putdown() method.
  • #1
knowLittle
312
3

Homework Statement


I can't see where the problem is in the following pseudo-code.
I recall from class that the problem is based on the signal calls and wait call in pickup() and putdown()

Thank you.

Homework Equations


Code:
class Monitor R()
{
	bool forks[5]; // all true

	condition c[5];

	void pickup(i)
	{
		left =i;
		right=(i+1)%5;

		if (forks[left])
			forks[left]=F;

		if (forks[right])
			forks[right]=F;

		while( forks[left]==F || forks[right]==F )
			wait(c[i])

		forks[left]=F;
		forks[right]=F;
	}

	void putdown(i)
	{
		lphil=(i+4)%5;
		rphil=(i+1)%5;

		left=i;
		right=(i+1)%5;

		forks[left]=T;
		forks[right]=T;

		signal( c[lphil]);
		signal( c[rphil] );

	}
}
 
Physics news on Phys.org
  • #2
The ProblemThe problem with this pseudo-code is that the signal calls in the putdown() method are in the wrong order. The left fork (forks
) should be signaled first, and then the right fork (forks
) should be signaled second. This ensures that the philosophers will pick up their forks in the correct order.​
 

1. What is the Dining Philosophers' Problem?

The Dining Philosophers' Problem is a classic computer science problem that involves a group of philosophers sitting around a circular table. In front of each philosopher is a plate of food and a fork. The philosophers spend their time thinking and eating. However, to eat, a philosopher must have both forks. The problem is that there are only as many forks as there are philosophers, so they must share.

2. What is the solution to the Dining Philosophers' Problem?

The solution to the Dining Philosophers' Problem involves using conditional variables to control the access to the shared resources, which in this case are the forks. This allows each philosopher to check if the forks they need are available before attempting to pick them up. If not, they must wait until the forks become available.

3. How do conditional variables work in the solution to the Dining Philosophers' Problem?

Conditional variables work by allowing threads to wait until a certain condition is met before proceeding. In this case, the condition is the availability of the forks. When a philosopher attempts to pick up a fork and finds it is not available, they will wait on the conditional variable. Once the fork becomes available, the philosopher will be notified and can proceed to pick it up.

4. What is the role of mutual exclusion in the Dining Philosophers' Problem?

Mutual exclusion is crucial in the solution to the Dining Philosophers' Problem. It ensures that only one philosopher can access a fork at a time, preventing two philosophers from picking up the same fork simultaneously. This is achieved through the use of locks, which allow only one thread to access a shared resource at a time.

5. Are there any potential issues with the solution to the Dining Philosophers' Problem?

One potential issue with the solution to the Dining Philosophers' Problem is the possibility of deadlock. This can occur when each philosopher is holding one fork and waiting for the other, preventing any of them from proceeding. To avoid this, a timeout can be implemented, where if a philosopher does not get both forks within a certain time, they will put down the fork they are holding and try again later.

Similar threads

Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Advanced Physics Homework Help
Replies
0
Views
645
  • Engineering and Comp Sci Homework Help
Replies
0
Views
477
  • Calculus and Beyond Homework Help
Replies
3
Views
225
  • Thermodynamics
Replies
4
Views
1K
Back
Top