Dining Philosophers problem in C

In this article, you will learn about the C program to solve Dining Philosophers problem.

The Dining Philosophers Problem:

Let there be 5 philosophers (for example) sitting around a round table for dinner. Each philosopher needs two forks to eat but there are only 5 forks (equal to the number of philosophers around the table).

A philosopher may eat if he/she can pick up two adjacent forks. But the 5 philosophers cannot eat at the same time because they can have only 1 fork on their one hand and empty on the other and not able to eat. After that, each of them waits for the adjacent philosopher to finish which leads to a deadlock situation and each of the philosopher starve.

The problem is to create a behaviour (algorithm) for the philosophers such that each philosopher would not starve and able to alternate between eating and thinking.

Program for Dining Philosophers Problem in C

The output of the Dining philosopher problem in c programming: