Dining Philosopher Problem - Os Proposal
Dining Philosopher Problem - Os Proposal
Submitted To:
SIR USMAN AHMED RAZA
Submitted By:
1. SANA RAFIQUE (2019-CS-687)
2. MAYSOON SHAHID (2019-CS-673)
Department of Computer Science
University of Engineering and Technology (New
Campus), Lahore
DINING PHILOSOPHERS PROBLEM
Objective:
The main objective of this project is to solve the Dining Philosopher Problem using
the concepts of Mutex Lock and pthread library.
Introduction:
The dining philosophers problem is a very famous and interesting problem used to
demonstrate the concept of deadlock. The dining philosopher's problem is the
classical problem of synchronization which says that Five philosophers are sitting
around a circular table and their job is to think and eat alternatively.
Here, we are going to implement this problem using the concepts of Threads and
mutex lock.
MUTEX:
A Mutex is a lock that we set before using a shared resource and release after
using it. There are a couple of mechanisms that implement locks. Mutex (mutual
exclusion) is the fundamental synchronization technique. The idea is simple:
whenever a work unit accesses the critical section, it first needs a lock that
guarantees no one else at this time is accessing the critical section. When the work
unit exits the critical section, it returns the lock for other work units to access.
PThread:
PThread library provides programmer with API for creating and managing
threads. API specifies behavior of the thread library, implementation is up to
development of the library.
Problem Description:-
Consider five philosophers (numbered 0 to 4) who spend their lives thinking and
eating. The philosophers share a circular table surrounded by five chairs, each
belonging to one philosopher. In the center of the table is a bowl of rice, and the
table is laid with five single chopsticks. When a philosopher thinks (for a random
amount of time between 1 and 3 seconds), she does not interact with her
colleagues. From time to time, a philosopher gets hungry and tries to pick up the
two chopsticks that are closest to her (the chopsticks that are between her and her
left and right neighbors). A philosopher may pick up only one chopstick at a time.
Obviously, she cannot pick up a chopstick that is already in the hand of a neighbor.
When a hungry philosopher has both her chopsticks at the same time, she eats (for
a random amount of time between 1 and 3 seconds) without releasing the
chopsticks. When she is finished eating, she puts down both chopsticks and starts
thinking again. A deadlock condition in the simulation dining philosophers
problem occurs when at one time; all the philosophers get hungry simultaneously,
and all philosophers take the chopsticks in his left hand. By the time the
philosopher will take the chopsticks in the right hand, then there was a deadlock
condition since all philosophers will both waiting for chopsticks on the right.
Methodology:-
There are several solutions which are:-
1. Allows at most four philosophers who sit together at one table.
2. Allows a philosopher take chopsticks only if both chopsticks were there
3. Allow only Odd numbered philosophers pick up the right chopstick first and
then the left, while even numbered philosophers pick up the left chopstick
first and then the right.
The first and second solution is not appropriate that’s why we use third solution in
our project to solve dining philosophers problem.