Write a c program that implements the dining philosophers' problem , and demonstrate how deadlocks can be
Question:
Write a c program that implements the dining philosophers' problem, and demonstrate how deadlocks can be dealt with or completely avoided. Assume that there is a round table with five dining philosophers, each having a plate of food in front of him. There is also a fork between any two adjacent philosophers. Each philosopher thinks for a random amount of time and then feels hungry. In order to feed, a hungry philosopher requires the forks on two sides of him. If he can grab those, he eats for some time, and then leaves back the forks from where he has taken those, and restarts thinking. This may lead to deadlock (think about the situation when all the philosophers have grabbed their left forks, and are waiting for their right neighbors to release the right forks). If this deadlock happens, some philosopher(s) need to be preempted (that is, forced to return its/their left forks) so that all the philosophers do not starve indefinitely. Another option is to avoid deadlocks altogether. In each case, a process forks five child processes which simulate the behavior of the philosophers. Each child process runs an infinite loop of thinking and feeding. Generate random periods for thinking and feeding, and simulate these operations by appropriate sleep calls.
MY Task IS:
Write a C program to implement the deadlock-free version as taught in the class. More precisely, each philosopher attempts to grab both the forks (left and right) simultaneously. If it can, it eats. If not, it waits. This means that if a philosopher sees that only one of the forks is available, he does not grab it. Use semaphores to synchronize the think-and-eat loops run by the five philosophers.
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill