Dining Philosophers Problem and Solution in JavaThe Dining Philosophers Problem is an example of a concurrency problem dealing with the allocation of limited resources among competing processes. In this section, we will understand how to avoid deadlock conditions in dining philosophers problem. It is the undesirable condition of concurrent systems. It is marked as in a circular waiting state. First, we will discuss the Dining Philosophers Problem that is used in the operating systems after that we will move towards the solution. Also, we will implement the solution in a Java program. Dining Philosophers ProblemThe pictorial representation of the problem is as follows. The above figure represents that there are five philosophers (labeled with P1, P2, P3, P4, and P5) sitting around a circular dining table. There are five plates of noodles to eat when philosophers feel hungry. To eat noodles, there are five forks/ chopsticks (labeled with 1 to 5) placed between each philosopher. Each philosopher eats and thinks alternatively. There are the following conditions followed by each philosopher:
Initially, all philosophers are thinking. After some time, gets hungry and wants to eat noodles. The philosopher looks for the forks on either side. When the philosopher gets both the forks, he starts eating. After eating, he puts down the forks and starts thinking again. When the philosopher puts down the forks, those forks may be used by neighbor philosophers. In such a case, there is the possibility of deadlock, a condition in which two or more processes cannot continue execution. The problem is used to design a scheduling technique in such a way no philosopher will starve. The Solution to the Dining Philosophers ProblemThe solution to the dining philosophers' problem is to use Semaphore. It is a tool that is used for concurrent processes. There is a drawback of using Semaphore as a solution. It may lead to deadlock. Suppose a scenario when all philosophers pick up the left fork and wait for the right fork. The situation leads to a deadlock. To avoid the deadlock solution, there are some measures that should have been taken are:
Implementation of Dining Philosophers ProblemIn the following program, first, we have initialized the number of philosophers (5). The two arrays philosophers[] and chopsticks[] initialized with the number of philosophers (5). In order to implement the logic for chopsticks, we have created a class named Chopstick. Inside the class, we have created a constructor of the Semaphore class and defined three methods namely grab(), release(), and isFree(). The grab() method invokes the acquire() method that acquires a permit from this semaphore. It reduces the number of permits by 1. If there is no permit is available the current thread becomes disabled. The user-defined release() method invokes the release() method of the Semaphore class. It releases the given number of permits and increments the permit by 1. The isFree() method checks for the availability of permits in the semaphore. It invokes the availablePermits() method of the Semaphore class that returns the number of permits available in the Semaphore. After that, we have created another class named Philosopher that extends the Thread class. Inside the class, we have defined three variables number, leftchopstick, and rightchopstick. Also, we have created a constructor of the Philosopher class and the eat() method. The run() method of the Thread class is called if the thread was constructed using a separate Runnable object. The run() method executes when a philosopher has both chopsticks (right and left). A philosopher starts eating by invoking the eat() method and holds the fork for a specified time (sleep time). In order to determine the sleep time, we have used the ThreadLocalRandom. current(). nextInt() method. The method returns a pseudorandom, uniformly distributed integer value between 0 and 1000. The value is determined by the nextInt() method passed in the sleep() method that sleeps the thread for a specified time. In our case, if sleep time is more than 1000ms, the program will exit. So, we can set the range of pseudorandom numbers (sleep time). Inside the main() method, we have defined the two for loops one for chopsticks, and the other for philosophers. After that, we have checked for deadlock conditions. If a deadlock occurs, it means each philosopher is eating by acquiring the chopsticks. The execution of the program breaks. The deadlock condition will not occur until resources (a single chopstick) is available. DiningPhilosophersProblem.java Output: Let's do some minor changes in the above program. Inside the while loop, change the thread sleep time 1000 to 2000 ms. Now execute the above program, we get the following output. Note: You may get a different output from the above output. Because the output changes each time when we execute the program.Next TopicShallow Copy Vs. Deep Copy in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India