0% found this document useful (0 votes)
12 views

Batch - 10 OS

This document provides an overview of the Dining Philosophers Problem and its Monitor solution. The problem involves 5 philosophers sharing a limited number of forks, with the challenge being how to prevent deadlocks from occurring as philosophers simultaneously try to access shared forks. The document outlines the problem framework, related work on solutions like using semaphores, code for a Monitor solution that uses mutex locks on forks to allow exclusive access, and conclusions on how it demonstrates process synchronization challenges.

Uploaded by

Ekansh Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Batch - 10 OS

This document provides an overview of the Dining Philosophers Problem and its Monitor solution. The problem involves 5 philosophers sharing a limited number of forks, with the challenge being how to prevent deadlocks from occurring as philosophers simultaneously try to access shared forks. The document outlines the problem framework, related work on solutions like using semaphores, code for a Monitor solution that uses mutex locks on forks to allow exclusive access, and conclusions on how it demonstrates process synchronization challenges.

Uploaded by

Ekansh Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1

Dining Philosophers Problem and Monitor Solution


Module – 1, T-3

BATCH No. – 10

221FJ01071 - SURAJ KUMAR SINGH


221FJ01053 - N . Mercy grace
221FJ01062 - Saramalla Manikanta
221FJ01075 – Ummareddy Varshini
BCA- II Year, I semester
Department of Information Technology,
Vignan's Foundation for Science, Technology & Research
(Deemed to be University),
Guntur, Andhra Pradesh, India
1 2

Contents

1. Abstract
2. Introduction
3. Related work
4. Framework for particular model
5. Experiments/case study
6. Conclusions
7. References
Abstract 1 3

This presentation delves into the classic concurrency problem known as the "Dining
Philosophers Problem." We explore its challenges, various solutions, and focus on the
Monitor solution approach.

- Dining Philosophers Problem : A classic synchronization and concurrency problem.


- Scenario : Five philosophers sit around a circular table, each having a plate of spaghetti
and a fork on either side.
- Goal : Philosophers alternate between thinking and eating using forks, but they must avoid
deadlocks and resource contention (two philosophers attempting to pick up the same fork).
- Constraints : Philosophers can only pick up both forks adjacent to them to eat.
- Challenge : Synchronize access to forks to prevent conflicts.
Introduction 1 4
 The Dining Philosophers problem is a classic computer science challenge where five philosophers
sit at a table, each needing two forks to eat their spaghetti. They must coordinate and avoid
deadlocks while sharing the forks.

•Dining Philosophers Scenario:


• Five philosophers at a round table.
• Each philosopher thinks and eats.
• They share a common set of forks (resources) placed
between them.
•Challenge:
• Philosophers need two forks to eat.
• Simultaneous access to shared forks can lead to conflicts.
• Avoiding deadlocks (where philosophers are stuck waiting) is
crucial.
Related Work 1 5

Deadlock: If all philosophers reach for their left fork simultaneously, they can become
deadlocked, as they each hold one fork and wait indefinitely for the other.
Solution: Various strategies exist to address this, such as introducing an order in which
philosophers pick up forks or using semaphores to control access to forks.
Semaphore: A synchronization primitive that controls access to resources. Philosophers
use semaphores to represent forks and manage their availability.
Concurrency Challenges: The problem highlights issues like deadlock.
Relevance: The Dining Philosophers problem illustrates the need for proper synchronization
mechanisms to ensure safe concurrent execution and resource sharing in computer
systems.

 Deadlock Avoidance  Resource Allocation


Framework for particular model 1 6
- Scenario : Imagine five philosophers sitting around a circular table. They spend their time thinking and eating
spaghetti.
- Goal : Philosophers want to think and eat without conflicts (deadlocks) arising.
- Challenge : They share a limited number of forks (resources) placed between them.
- Rules :
1. A philosopher must have two forks to eat.
2. Philosophers can only pick forks on their left and right.
3. They can't talk, only pick and release forks.
- Critical Section Problem : Ensuring that only one philosopher can pick up a fork at a time to avoid conflicts.
- Solutions :
1. Mutex Solution : Introduce a mutex (lock) for each fork.
- Philosophers must acquire both mutexes (forks) to eat.
- Releases forks and mutexes when done.
2. Semaphore Solution : Use semaphores (counting mechanism) to control fork access.
- Limits the number of philosophers who can access forks.
- Helps avoid deadlock and ensures fairness.
1
Framework for particular model 7

 Challenges :
 Deadlock: All philosophers holding one fork and waiting for another.
• Starvation: A philosopher never gets the chance to eat.

 Prevention :
 Timeout : Set a limit for how long a philosopher can hold a fork without eating.
 Randomized delay : Philosophers delay picking up forks to reduce conflicts.
 Implementation : These solutions can be applied using various programming techniques and
synchronization mechanisms, such as locks, semaphores, and condition variables.
 Purpose : Demonstrates challenges of concurrent programming, resource sharing, and
synchronization issues in distributed systems.
Monitor Code 1 8

#include <stdio.h> while (1) {


#include <stdlib.h>
#include <pthread.h>
printf("Philosopher %d is thinking\n", philosopher_id);
#include <unistd.h>
usleep(rand() % 2000000); // Sleep for up to 2 seconds

#define NUM_PHILOSOPHERS 5
printf("Philosopher %d is hungry\n", philosopher_id);
pthread_mutex_t forks[NUM_PHILOSOPHERS]; pthread_mutex_lock(&forks[left_fork]);
pthread_t philosophers[NUM_PHILOSOPHERS]; pthread_mutex_lock(&forks[right_fork]);
printf("Philosopher %d is eating\n", philosopher_id);
void *philosopher(void *arg) {
usleep(rand() % 1000000); // Eat for up to 1 second
int philosopher_id = *(int *)arg;
// Put down forks
int left_fork = philosopher_id;
pthread_mutex_unlock(&forks[right_fork]);
int right_fork = (philosopher_id + 1) %
pthread_mutex_unlock(&forks[left_fork]);
NUM_PHILOSOPHERS;
}
1 9
return NULL;
}
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
int main() { pthread_join(philosophers[i], NULL);
srand(time(NULL)); }

for (int i = 0; i < NUM_PHILOSOPHERS; i++) { for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_mutex_init(&forks[i], NULL); pthread_mutex_destroy(&forks[i]);
} }

for (int i = 0; i < NUM_PHILOSOPHERS; i++) { return 0;


int *id = malloc(sizeof(int)); }
*id = i;
pthread_create(&philosophers[i], NULL, philosopher, id);
}
1 10

Conclusion

 Process synchronization is defined as no two processes have access to


the same associated data and resources.
 The Dining philosopher problem is an example of process
synchronization problem.
 Philosopher is an analogy for process and chopstick for resources, we
can try to solve process synchronization problems using this.
 The solution of Dining Philosopher problem focuses on the use of
semaphores.
1

Reference 11

Scaler
https://www.scaler.com/topics/operating-system/dining-philosophers-problem-in-os/

Geeksforgeeks
https://www.geeksforgeeks.org/dining-philosopher-problem-using-semaphores/

“Operating System Concepts”

 Abraham Silber Schatz Peter B. Galvin and Greg Gagne, 8th Edition, Wiley, 2008.
1 12

Than
k You

You might also like