Question: Project 2The Dining Philosophers Problem In Section 5.7.3, we provide an outline of a solution to the dining-philosophers problem using monitors. This problem will require

Project 2The Dining Philosophers Problem

In Section 5.7.3, we provide an outline of a solution to the dining-philosophers

problem using monitors. This problem will require implementing a solution

using Pthreads mutex locks and condition variables.

The Philosophers

Begin by creating five philosophers, each identified by a number 0 . . 4. Each

philosopher will run as a separate thread. Thread creation using Pthreads is

covered in Section 4.4.1. Philosophers alternate between thinking and eating.

To simulate both activities, have the thread sleep for a random period between

one and three 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 Section

5.8. However, in that section, 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 monitor we accomplish locking by associating a condition variable with

a mutex lock. Pthreads mutex locks are covered in Section 5.9.4. 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 == bto 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().

Output:

Project 2The Dining Philosophers Problem In Section 5.7.3, we provide an outline

c dp-main.c Ea main 2 matches. + Aa Contains | | | Done a Find 113 114 115 116 srandom init); printf(" printf(" printf(" 117 printf("Phil e Phil1 Phil 2 Phil 3 Phil 4n) 118 119 showA11Philosophers) for (int 1-0; |

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!