Batch - 10 OS
Batch - 10 OS
BATCH No. – 10
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.
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.
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
#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]);
} }
Conclusion
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/
Abraham Silber Schatz Peter B. Galvin and Greg Gagne, 8th Edition, Wiley, 2008.
1 12
Than
k You