You are on page 1of 7

Oprating System

II BCA I Semester (2023-24)


BCA- II Year, I semester
Department of Information Technology,
Vignan's Foundation for Science, Technology & Research
(Deemed to be University),
Guntur, Andhra Pradesh, India

IT- Department Vignan university,vadlamudi


Batch – 10
221FJ01071- Suraj Kumar Singh
221FJ01062- Saramalla Manikanta
221FJ01075- Ummareddy Varshini
221FJ01053- N . Mercy grace

This paper delves into the intricacies of the


Dining Philosophers problem, discussing
Title: Dinning philoshpher in critical section .
its relevance in concurrent systems and its
representation using formal models like
Petri nets or finite state machines.
1. Abstract : Furthermore, it explores different strategies
The Dining Philosophers problem, a for tackling the problem, including
classic synchronization and concurrency solutions based on deadlock avoidance,
challenge in computer science, resource allocation graphs, and other
encapsulates the complexities of managing concurrency control techniques. The
shared resources among multiple processes importance of efficient synchronization
in a concurrent system. This problem serves mechanisms in enhancing system
as a fundamental illustration of the critical performance while mitigating deadlocks
section problem, highlighting the need for and resource contention is also discussed.
proper synchronization mechanisms to
prevent race conditions and ensure the
integrity of shared resources. 2. Introduction :
The Dining Philosophers problem is a classic
The critical section problem underlines the synchronization and concurrency problem in
necessity for processes to execute certain computer science and operating systems. It
segments of code exclusively to prevent illustrates challenges related to resource
data corruption or unintended outcomes. In sharing and coordination among multiple
the context of the Dining Philosophers processes in a parallel computing environment.
problem, the critical section pertains to the The problem was formulated by E.W. Dijkstra
act of acquiring forks to eat. Various in 1965 to illustrate the difficulties of managing
synchronization mechanisms, such as concurrent access to shared resources.
semaphores or mutexes, can be applied to
address this issue.
variables that should not be accessed
simultaneously by multiple threads or
processes. In the context of the Dining
Philosophers problem, the critical section refers
to the code segment where a philosopher
attempts to pick up the forks to their left and
right in order to eat. Since the forks are shared
resources, they need to be accessed in a
mutually exclusive manner to avoid conflicts.

Figure 1 To solve the Dining Philosophers problem,


synchronization mechanisms like semaphores,
The Dining Philosophers problem presents a
mutexes (mutual exclusion), and other
scenario where a certain number of
concurrency control techniques can be
philosophers are seated around a circular dining
employed. These mechanisms ensure that only
table. Each philosopher alternates between
one philosopher at a time can access the forks
thinking and eating. There are forks placed
and thus avoid contention.
between each pair of adjacent philosophers, and
to eat, a philosopher needs to pick up both the
fork to their left and the fork to their right.
A common solution to the problem involves the
following steps:

Each philosopher must acquire the right and left


forks before eating. If both forks are available,
the philosopher can enter the critical section
and eat.

To prevent deadlocks, philosophers are given a


protocol to follow: they must pick up the forks
in a way that avoids circular waiting. For
Figure 2 instance, philosophers with even indices could
first pick up their left fork, while those with odd
indices could first pick up their right fork.
The key challenge in this scenario arises when
multiple philosophers attempt to pick up the
same fork(s) concurrently. If not properly If a philosopher cannot acquire both forks,
synchronized, this can lead to deadlocks, they release any forks they have picked up
livelocks, or resource contention issues. To and return to a thinking state.
ensure that the philosophers can dine without
any issues, proper synchronization mechanisms
must be implemented.
After a philosopher finishes eating, they
release the forks, allowing other
philosophers to access them.
The Critical Section:

The use of synchronization primitives like


The term "critical section" refers to a portion of semaphores or mutexes ensures that only
the code that accesses shared resources or one philosopher can access the critical
section (pick up forks) at a time, preventing Rules and Constraints:
conflicts.

A philosopher can only do one of two things at


By properly managing the critical section, a time: think or eat.
synchronization mechanisms ensure that
To eat, a philosopher must pick up the two forks
philosophers can dine without deadlocks or
to their left and right.
resource conflicts. However, implementing
an efficient solution requires careful A philosopher must wait until both forks they
consideration of the synchronization want to use are available.
strategy to avoid potential issues like
livelocks or inefficient resource utilization. Once a philosopher finishes eating, they put
down the forks, allowing others to use them.
Philosophers should not interfere with each
other's actions, i.e., they cannot forcibly take a
fork from another philosopher.
3. Related Work
Deadlocks (when philosophers are stuck
waiting for each other to release a fork) and
The problem is typically stated as follows: resource contention (multiple philosophers
trying to access the same forks) should be
avoided.
Problem Statement: Solution:
There are five philosophers sitting around a Various solutions to the Dining Philosophers
circular dining table. Each philosopher spends problem exist, demonstrating different
their time thinking and eating. There is a single strategies for avoiding deadlocks and ensuring
fork placed between each pair of adjacent fair access to resources. One classical solution
philosophers, and they require two forks to eat. uses semaphores or mutex locks to manage the
The goal is to design a synchronization scheme forks' access. Here's a high-level overview of
that ensures that the philosophers can alternate the solution:
between eating and thinking without causing
deadlocks, starvation, or resource conflicts.
Each philosopher is represented as a separate
thread.
Each fork is associated with a semaphore or
mutex lock to control access to it.
Before attempting to eat, a philosopher must
acquire both the left and right forks. If one or
both forks are unavailable, the philosopher will
wait until they are available.
After eating, the philosopher releases both
forks, allowing others to use them.
To avoid potential deadlocks, philosophers are
organized such that they follow a consistent
Figure 3 pattern when picking up forks (e.g., always
picking up the lower-numbered fork first).
A global mechanism is used to limit the number
of philosophers allowed to pick up forks
Each philosopher is represented as a separate
simultaneously, reducing contention.
thread.
Each fork is associated with a semaphore or
The problem is typically stated as follows: mutex lock to control access to it.
Before attempting to eat, a philosopher must
acquire both the left and right forks. If one or
Problem Statement:
both forks are unavailable, the philosopher will
There are five philosophers sitting around a wait until they are available.
circular dining table. Each philosopher spends
After eating, the philosopher releases both
their time thinking and eating. There is a single
forks, allowing others to use them.
fork placed between each pair of adjacent
philosophers, and they require two forks to eat. To avoid potential deadlocks, philosophers are
The goal is to design a synchronization scheme organized such that they follow a consistent
that ensures that the philosophers can alternate pattern when picking up forks (e.g., always
between eating and thinking without causing picking up the lower-numbered fork first).
deadlocks, starvation, or resource conflicts.
A global mechanism is used to limit the number
of philosophers allowed to pick up forks
simultaneously, reducing contention.
Rules and Constraints:

A philosopher can only do one of two things at


a time: think or eat.
To eat, a philosopher must pick up the two forks
to their left and right.
4. Framework for particular model
A philosopher must wait until both forks they
want to use are available. Dining Philosophers Problem:

Once a philosopher finishes eating, they put The Dining Philosophers problem is a
down the forks, allowing others to use them. classic synchronization and concurrency
problem in computer science and operating
Philosophers should not interfere with each systems. It was formulated by E.W.
other's actions, i.e., they cannot forcibly take a Dijkstra in 1965 to illustrate the challenges
fork from another philosopher. of managing multiple processes that share
Deadlocks (when philosophers are stuck resources in a concurrent environment.
waiting for each other to release a fork) and The problem imagines five philosophers
resource contention (multiple philosophers sitting around a circular dining table. Each
trying to access the same forks) should be philosopher alternates between thinking
avoided. and eating. There's a bowl of spaghetti in
Solution: the center, and each philosopher needs two
forks to eat. However, there are only five
Various solutions to the Dining Philosophers forks available, one between each pair of
problem exist, demonstrating different philosophers.
strategies for avoiding deadlocks and ensuring
fair access to resources. One classical solution
uses semaphores or mutex locks to manage the The challenge is to design a protocol (a set
forks' access. Here's a high-level overview of of rules) that allows the philosophers to
the solution: alternate between thinking and eating in a
way that avoids deadlocks (where all It seems like you're asking about the dining
philosophers are waiting for a resource) and philosophers problem and how to explain it in
avoids resource contention (where multiple the context of critical sections. The dining
philosophers try to access the same fork philosophers problem is a classic
simultaneously). synchronization and concurrency problem used
to illustrate issues that can arise in multi-
threaded or multi-process environments. It
Critical Section: helps highlight the challenges of resource
allocation and synchronization in parallel
The critical section refers to a part of a computing systems.
program or code where shared resources
(like variables, data structures, or devices)
are accessed or modified. In a multi-
Problem Statement:
threaded or multi-process environment, it's
crucial to ensure that only one thread or
process can be inside the critical section at
The dining philosophers problem presents a
a time. This prevents race conditions, where
scenario in which a group of philosophers sit
multiple threads might manipulate the
around a circular dining table. Each
shared resource simultaneously and lead to
philosopher alternates between thinking and
unpredictable behavior.
eating. There is a bowl of rice placed between
Now, tying these concepts together: each pair of adjacent philosophers, and a single
fork is placed between each pair of
philosophers. To eat, a philosopher needs two
The Dining Philosophers problem can be forks, one from each side of their plate.
seen as an analogy for the challenges of
managing access to critical sections in a
concurrent system. Each philosopher The challenge here is to design a solution that
represents a thread or process, and the forks allows the philosophers to take turns between
represent the shared resources that they thinking and eating, while preventing
need to access (analogous to a critical deadlocks (where no philosopher can proceed
section). The challenge is to design a because they are all waiting for a fork held by
synchronization mechanism that allows the another philosopher) and avoiding resource
philosophers to "eat" (access the critical contention.
section) without running into problems like
deadlocks or resource contention.
Solution and Explanation:

Solving the Dining Philosophers problem


requires careful consideration of To address the dining philosophers problem,
synchronization techniques like various synchronization strategies can be
semaphores, mutexes, or monitors, which employed. One common approach is to use a
are tools used in programming to control mutex (short for mutual exclusion) for each
access to shared resources. These fork. A mutex is a synchronization primitive
techniques ensure that only a limited that allows only one thread to access a critical
number of philosophers can access the section of code (in this case, accessing the
forks (critical section) at any given time, forks) at a time.
preventing conflicts and ensuring orderly
access.
Here's a high-level explanation of a solution
5. Experiments/case study :
using mutexes:
1. Each philosopher is represented as a separate Remember that the primary goal of the
thread or process. experiment is to illustrate the challenges of
concurrent resource access and how
2. Each fork is associated with a mutex to
synchronization techniques like mutexes can be
ensure exclusive access.
used to prevent issues like deadlocks.
3. A philosopher can only pick up both forks if
they are available (mutexes are unlocked).
4. When a philosopher wants to eat, they try to 6. Conclusion :
acquire both forks' mutexes. If one of the forks In conclusion, the dining philosophers problem
is unavailable (mutex locked by another serves as a powerful illustration of the
philosopher), they release any acquired forks, complexities and challenges that arise in
go back to thinking, and retry later. concurrent and parallel computing
5. After eating, the philosopher releases both environments. This classic problem highlights
forks' mutexes so that other philosophers can several key concepts and lessons:
use them.

Resource Contention: In a multi-threaded or


This solution ensures that philosophers can multi-process environment, multiple entities
access the forks without conflicts and prevents might need access to shared resources
deadlocks by ensuring that a philosopher can simultaneously. This can lead to conflicts and
only pick up both forks simultaneously or none contention, causing inefficiencies and potential
at all. deadlocks.

Experiment: Deadlocks: Deadlocks occur when multiple


processes or threads are unable to proceed
because they are all waiting for resources held
by each other. The dining philosophers problem
An experiment to demonstrate the dining
demonstrates how improper resource allocation
philosophers problem would involve creating a
and synchronization can lead to deadlocks.
simulation with multiple philosopher
threads/processes and forks, following the
solution approach outlined above. You would
need to implement the synchronization Synchronization: Synchronization
mechanisms (mutexes) and the logic for mechanisms, such as mutexes, semaphores, and
philosophers to think, pick up forks, eat, and locks, are essential for coordinating access to
release forks. The experiment can be shared resources. These mechanisms ensure
implemented using a programming language that only one thread or process can access a
with support for multi-threading or multi- critical section of code at a time, preventing
processing, such as Python with the `threading` race conditions and conflicts.
module.

Concurrency Issues: The problem emphasizes


The experiment should showcase scenarios the need for carefully designed solutions to
where philosophers alternate between thinking manage concurrency issues. Incorrect
and eating, while avoiding deadlocks and synchronization can lead to bugs that are hard
resource contention. You can observe how the to reproduce and diagnose.
synchronization mechanisms ensure that the
philosophers can share resources peacefully.
Trade-offs: Solutions to the dining
philosophers problem often involve trade-offs
between efficiency and correctness. Ensuring concurrent programming and the significance
that philosophers can always access forks of synchronization mechanisms. By addressing
might lead to lower throughput, while allowing the dining philosophers problem, we gain a
multiple philosophers to grab a fork could deeper understanding of how to design and
result in contention and deadlocks. implement reliable, efficient, and scalable
multi-threaded and multi-process systems.

Algorithm Design: The problem encourages


algorithmic creativity. Designing a solution
that allows philosophers to eat without
deadlocks requires careful consideration of the
order in which they attempt to pick up forks. 7. References:

Real-world Relevance: While the dining


philosophers problem might seem abstract, it
Scaler
reflects real-world challenges in distributed https://www.scaler.com/topics/operating-
systems, operating systems, and parallel system/dining-philosophers-problem-in-os/
computing. It offers insights into how to design
reliable and efficient systems. Geeksforgeeks
https://www.geeksforgeeks.org/dining-
philosopher-problem-using-semaphores/
In the context of critical sections and
synchronization, the dining philosophers “Operating System Concepts”
problem teaches us about the importance of
managing access to shared resources to avoid ❑ Abraham Silber Schatz Peter B. Galvin and
conflicts and deadlocks. It's a valuable learning Greg Gagne, 8th Edition, Wiley, 2008.
tool for computer science students and
practitioners to understand the intricacies of

THANK YOU

You might also like