Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CSC 205: DINING PHILOSOPHER PROBLEMS

GROUP 2: Dining Philosopher Problem Solving using


Binary Semaphore

Group members
1.OLUBODUN BOLAJI IMRAN|230591242
2. ADEKOYA ENIAYO SAMUEL|220591025
3. AFOLABI EMMANUEL AYOMIDE| 220591340
4. Ayodele Treasure Morounfoluwa|
220591101
5. Adedeji Esther Ebunoluwa |220591019
6. Oluwatosin Adedeji Emmanuel |220591245
7. Tijani Sheu Olamilekan |220591299
8. Sadiq Haneef Olanrewaju Greene|
220591280
9. Adeniyi Aanuoluwapo Ayobami |220591029
10. Ayomiposi Adeniyi Oluwapelumi |
220591364
11. Babatunde Williams oluwadurotimi
| 220591109
12. Abisogun Oluwatimilehin Morakinyo
| 220591010
13. SURU SESENKO SEMOMI |220591295
14. 220591163
15. 220591039
CSC 205: DINING PHILOSOPHER PROBLEMS

DINING PHILOSOPHER PROBLEM


2
The Dining Philosophers Problem is a classic example in computer science and concurrency theory that
illustrates challenges with resource allocation and deadlock avoidance in concurrent systems. The problem is
framed around a group of philosophers seated around a dining table, with a chopstick placed between each
pair of adjacent philosophers.

Here's a brief explanation of the concept

Setup:
There are N philosophers sitting around a circular dining table.

Each philosopher spends their time alternating between two activities: thinking and eating.

To eat, a philosopher needs two chopsticks, one placed to their left and one to their right.

Challenge:

The challenge arises when multiple philosophers attempt to eat simultaneously.

If each philosopher tries to pick up both chopsticks at the same time, a deadlock can occur. This situation
arises when each philosopher picks up one chopstick and then waits indefinitely for the second chopstick to
become available, preventing any philosopher from making progress.

Solution:
•To solve the Dining Philosophers Problem, concurrency control mechanisms are used to ensure that the
philosophers can access the chopsticks without causing deadlock.

•Various solutions to the problem exist, including using semaphores, mutexes, monitors, or other
synchronization primitives to enforce mutual exclusion and prevent multiple philosophers from accessing
the same chopstick

•Solutions must also ensure that no philosopher starves (i.e., waits indefinitely) for access to the chopsticks.

The Dining Philosophers Problem is a classic illustration of challenges with resource sharing and concurrency
control in multi-threaded or multi-process environments. It serves as a foundational example in the study of
concurrent programming and distributed systems.

Synchronization Tool (Binary semaphores)


CSC 205: DINING PHILOSOPHER PROBLEMS

Binary semaphores, also known as mutexes (short for mutual exclusion), are synchronization primitives used
in concurrent programming to control access to shared resources, particularly in multi-threaded or multi- 3
process environments. Here's an explanation of binary semaphores:

Definition: A binary semaphore is a synchronization object that can have two states: 0 and 1. It is used to
coordinate access to shared resources in a way that ensures mutual exclusion, meaning only one thread or
process can access the resource at a time.

Operations:
•Wait (P) Operation: Also known as "acquire" or "down" operation. When a thread or process wants to
access the shared resource, it performs a wait operation on the binary semaphore. If the semaphore's value
is 1 (indicating the resource is available), the semaphore value is decremented to 0, and the thread or
process can proceed to access the resource. If the semaphore's value is already 0 (indicating the resource is
currently being used), the thread or process is blocked until the semaphore value becomes 1 again.

•Signal (V) Operation: Also known as "release" or "up" operation. When a thread or process has finished
using the shared resource, it performs a signal operation on the binary semaphore. This increments the
semaphore value from 0 to 1, indicating that the resource is now available for use by another thread or
process.

Usage:
•Binary semaphores are commonly used to protect critical sections of code, where access to shared
resources must be serialized to prevent race conditions and ensure data consistency.

•They can also be used to implement locks, mutexes, and other synchronization mechanisms in concurrent
programs.

•Binary semaphores are often used in conjunction with condition variables to coordinate threads or
processes in more complex synchronization scenarios

In summary, binary semaphores are synchronization primitives used to enforce mutual exclusion and
coordinate access to shared resources in concurrent programming environments. They help prevent race
conditions and ensure the integrity of shared data in multi-threaded or multi-process applications.

Solution using the synchronization Tool (Binary Semaphores)


The Dining Philosophers Problem can be resolved using binary semaphores to enforce mutual exclusion and
prevent deadlock. Here's a high-level overview of how binary semaphores can be used to solve the problem

1. Chopsticks as Resources: Represent each chopstick as a binary semaphore. Each semaphore can have two
states: 0 (unavailable) and 1 (available).
CSC 205: DINING PHILOSOPHER PROBLEMS

2. Philosophers as Processes/Threads: Represent each philosopher as a separate process or thread. Each


philosopher can be in one of the following states: thinking, hungry, or eating. 4
3. Acquiring Chopsticks: When a philosopher wants to eat, they first need to acquire both chopsticks
adjacent to them.

To acquire a chopstick, a philosopher performs a wait operation (P) on the corresponding semaphore. If the
semaphore value is 1 (available), the philosopher decrements the semaphore value to 0 (unavailable) and
proceeds to pick up the chopstick. If the semaphore value is 0, indicating that another philosopher is using
the chopstick, the philosopher waits until the semaphore value becomes 1 again.

4. Releasing Chopsticks: After finishing eating, the philosopher releases both chopsticks by performing a
signal operation (V) on the corresponding semaphores. This increments the semaphore values from 0 to 1,
indicating that the chopsticks are available for other philosophers to use.

5. Preventing Deadlock: To prevent deadlock, philosophers should only attempt to pick up both chopsticks if
they can acquire both simultaneously. This can be achieved by enforcing a rule that philosophers must pick
up chopsticks in a specific order, such as always starting with the lower-numbered chopstick.

6. Starvation Prevention: To prevent starvation, ensure that philosophers take turns eating and that no
philosopher can monopolize the chopsticks indefinitely. This can be achieved by introducing mechanisms
such as a maximum waiting time or limiting the number of philosophers allowed to be eating simultaneously.

By using binary semaphores to coordinate access to the chopsticks and enforce mutual exclusion, the Dining
Philosophers Problem can be solved while avoiding deadlock and starvation. The key is to carefully manage
the acquisition and release of resources to ensure that philosophers can take turns eating without

interfering with each other's access to the chopsticks.

You might also like