Question
The Dining Philopsophers Problem The Philosophers Begin by creating n philosophers, each identified by a number 0...(n-1). Each philosopher will run as a separate thread.
The Dining Philopsophers Problem
The Philosophers
Begin by creating n philosophers, each identified by a number 0...(n-1). Each philosopher will run as a separate thread. Philosophers alternate between thinking and eating. To simulate both activities, have the thread sleep for a random period between one and five seconds. When a philosopher wishes to eat, she invokes the function
pickup_forks(int philosopher_number) where philosopher_number identifies the number of the philosopher wishing to
eat. When a philosopher finishes eating, she invokes
return_forks(int philosopher_number)
Pthreads Condition Variables
Condition variables in Pthreads behave similarly to those described in Slides (Process Management PartK, Slides 8-12). However, in that part, condition variables are used within the context of a monitor, which provides a locking mechanism to ensure data integrity. Since Pthreads is typically used in C programsand since C does not have a monitorwe accomplish locking by associating a condition variable with a mutex lock. Pthreads mutex locks are covered in the slides (Process Management PartH). We cover Pthreads condition variables here.
Condition variables in Pthreads use the pthread_cond_t data type and are initialized using the pthread_cond_init() function. The following code creates and initializes a condition variable as well as its associated mutex lock:
pthread_mutex_t mutex; pthread_cond_t cond_var; pthread_mutex_init(&mutex,NULL); pthread_cond_init(&cond_var,NULL);
The pthread_cond_wait() function is used for waiting on a condition variable. The following code illustrates how a thread can wait for the condition a == b to become true using a Pthread condition variable:
pthread_mutex_lock(&mutex); while (a != b)
pthread_cond_wait(&mutex, &cond_var);
pthread_mutex_unlock(&mutex);
The mutex lock associated with the condition variable must be locked before the pthread_cond_wait() function is called, since it is used to protect the data in the conditional clause from a possible race condition. Once this lock is acquired, the thread can check the condition. If the condition is not true, the thread then invokes pthread_cond_wait(), passing the mutex lock and the condition variable as parameters. Calling pthread_cond_wait() releases the mutex lock, thereby allowing another thread to access the shared data and possibly update its value so that the condition clause evaluates to true. (To protect against program errors, it is important to place the conditional clause within a loop so that the condition is rechecked after being signaled.)
A thread that modifies the shared data can invoke the pthread_cond_signal() function, thereby signaling one thread waiting on the condition variable. This is illustrated below:
Pthread_mutex_lock(&mutex); a = b; pthread_cond_signal(&cond_var); pthread_mutex_unlock(&mutex);
It is important to note that the call to pthread_cond_signal() does not release the mutex lock. It is the subsequent call to pthread_mutex_unlock()that releases the mutex. Once the mutex lock is released, the signaled thread becomes the owner of the mutex lock and returns control from the call to pthread_cond_wait().
Part I Assignment
Design and implement a solution for the dining-philosophers problem using the Pthreads library. Your solution must be deadlock-free. Following are some sample
outputs expected from your solution:
...... Philosopher 2 is eating: eating for 1 seconds Philosopher 4 is eating: eating for 4 seconds Philosopher 2 is thinking: thinking for 5 seconds Philosopher 1 is eating: eating for 3 seconds Philosopher 4 is thinking: thinking for 4 seconds Philosopher 3 is eating: eating for 2 seconds Philosopher 1 is thinking: thinking for 1 seconds Philosopher 0 is eating: eating for 3 seconds Philosopher 3 is thinking: thinking for 4 seconds Philosopher 2 is eating: eating for 2 seconds Philosopher 0 is thinking: thinking for 5 seconds Philosopher 4 is eating: eating for 1 seconds Philosopher 2 is thinking: thinking for 2 seconds Philosopher 1 is eating: eating for 5 seconds Philosopher 4 is thinking: thinking for 5 seconds Philosopher 3 is eating: eating for 1 seconds Philosopher 3 is thinking: thinking for 1 seconds Philosopher 3 is eating: eating for 2 seconds Philosopher 1 is thinking: thinking for 5 seconds Philosopher 0 is eating: eating for 4 seconds ......
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started