Question

Consider the Dining-Philosophers problem (Chapter 5.7.3), in which a set of philosophers sit around a table...

Consider the Dining-Philosophers problem (Chapter 5.7.3), in which a set of philosophers sit around a table with one chopstick between each of them. Let the philosophers be numbered from 0 to n−1 and be represented by separate threads. Each philosopher executes Dine(i), where “i” is the philosopher’s number. Assume that there is an array of semaphores, Chop[i] that represents the chopstick to the left of philosopher i. These semaphores are initialized to 1.

void Dine( int i )

{

Chop[ i ] .P(); /∗ Grab left chopstick ∗/

Chop[( i+1)%n ] .P(); /∗ Grab right chopstick ∗/

EatAsMuchAsYouCan ();

Chop[ i ] .V(); /∗ Release left chopstick ∗/

Chop[( i+1)%n ] .V(); /∗ Release right chopstick ∗/

}

This solution can deadlock. Assume that it does. List the four conditions of deadlock and explain why each of them is satisfied during the deadlock.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#define n 4 // number of processes are 4
using namespace std;
int compltedPhilo = 0,i;
struct fork{
    int taken;
}ForkAvil[n];//fork structures
struct philosp{
    int left;
    int right;
}Philostatus[n]; //philosphers structures

void goForDinner(int philID){
//same like threads concept here cases implemented
    if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
        cout<<"Philosopher "<<philID+1<<" completed his dinner\n";
    //already completed dinner then
    else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
            //if just taken two forks
            cout<<"Philosopher "<<philID+1<<" completed his dinner\n";

            Philostatus[philID].left = Philostatus[philID].right = 10; //remembering that he completed dinner by assigning value 10
            int otherFork = philID-1;

            if(otherFork== -1)
                otherFork=(n-1);

            ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; //releasing forks
            cout<<"Philosopher "<<philID+1<<" released fork "<<philID+1<<" and fork "<<otherFork+1<<"\n";
            compltedPhilo++;
        }
        else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){ //left already taken, trying for right fork
                if(philID==(n-1)){
                    if(ForkAvil[philID].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
                        ForkAvil[philID].taken = Philostatus[philID].right = 1;
                        cout<<"Fork "<<philID+1<<" taken by philosopher "<<philID+1<<"\n";
                    }else{
                        cout<<"Philosopher "<<philID+1<<" is waiting for fork "<<philID+1<<"\n";
                    }
                }else{
    //except last philosopher case
                    int dupphilID = philID;
                    philID-=1;
                     if(philID== -1)
                        philID=(n-1);
                     if(ForkAvil[philID].taken == 0){
                        ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
                        cout<<"Fork "<<philID+1<<" taken by Philosopher "<<dupphilID+1<<"\n";
                    }else{
                        cout<<"Philosopher "<<dupphilID+1<<" is waiting for Fork "<<philID+1<<"\n";
                    }
                }
            }
            else if(Philostatus[philID].left==0){ //nothing taken yet
                    if(philID==(n-1)){
                        if(ForkAvil[philID-1].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
                            ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
                            cout<<"Fork "<<philID<<" taken by philosopher "<<philID+1<<"\n";
                        }else{
                            cout<<"Philosopher "<<philID+1<<" is waiting for fork "<<philID<<"\n";
                        }
                    }else{
     //except last philosopher case
                        if(ForkAvil[philID].taken == 0){
                            ForkAvil[philID].taken = Philostatus[philID].left = 1;
                            cout<<"Fork "<<philID+1<<" taken by Philosopher "<<philID+1<<"\n";
                        }else{
                            cout<<"Philosopher "<<philID+1<<" is waiting for Fork "<<philID+1<<"\n";
                        }
                    }
        }else{}
}int main(){
    for(i=0;i<n;i++)
        ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;

    while(compltedPhilo<n){
        /* while loop will run until all philosophers complete dinner
        Actually problem of deadlock occur only try to take at same time
        This for loop will say that they are trying at same time.
        */
        for(i=0;i<n;i++)
            goForDinner(i);
        cout<<"\nTill now num of philosophers completed dinner are "<<compltedPhilo<<"\n\n";
    }
    return 0;
}

ElHomeworkLib dinning.exe Fork 1 taken by Philosopher 1 Fork 2 taken by Philosopher 2 Fork 3 taken by Philosopher 3 Philosopher 4 i

Add a comment
Know the answer?
Add Answer to:
Consider the Dining-Philosophers problem (Chapter 5.7.3), in which a set of philosophers sit around a table...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. Consider the variation of the Dining Philosophers problem shown in Figure 6.17, where all unused chopsticks are placed in the center of the table and any philosopher can eat with any two chopstic...

    3. Consider the variation of the Dining Philosophers problem shown in Figure 6.17, where all unused chopsticks are placed in the center of the table and any philosopher can eat with any two chopsticks. One way to prevent deadlock in this system is to provide sufficient resources. For a system with n philosophers, what is the minimum number of chopsticks that ensures deadlock freedom? Why? Owned by Philosopher Waiting for hopsticks / Philosopher Philosopher Waiting for Owned by Owned by...

  • Consider a dinner table where n dining philosophers are seated. Each philosopher has a plate of...

    Consider a dinner table where n dining philosophers are seated. Each philosopher has a plate of food; however, there is only a single eating-utensil placed in the center of the table. Eating is done at discrete rounds. At the beginning of each round, if a philosopher wishes to eat, she may attempt to obtain the utensil from the center of the table. If the philosopher obtains the utensil, she eats for the duration of the round (i.e., only one philosopher...

  • Using the following pseudocode write a c program that solves the dining philosophers problem. #define N...

    Using the following pseudocode write a c program that solves the dining philosophers problem. #define N 5 #define LEFT (i+N-1)%N #define RIGHT (i+1)%N #define THINKING 0 #define HUNGRY 1 #define EATING 2 typedef int semaphore; int state[N]; semaphore mutex = 1; semaphore s[N]; void philosopher(int i) { while(TRUE) { think(); take_forks(i); eat(); put_forks(i); } } void take_forks(int i) { down(&mutex); state[i] = HUNGRY; test(i); up(&mutex); down(&s[i]); } void put_forks(i) { down(&mutex); state[i] = THINKING; test(LEFT); test(RIGHT); up(&mutex); } void test(i)...

  • What are the three requirements for a solution to the critical-section problem? Consider the following solution...

    What are the three requirements for a solution to the critical-section problem? Consider the following solution to the dining-philosophers' problem. // Global variables. Shared among threads int state [5]; semaphore mutex; I/ Initially set to 1 semaphore s [5] I Initially s[i] is set to 0 for all i Initially statei]-THINKING for all i void philosopher (int i) f int left(int i) f // Philosopher to the left of i // % is the mod operator. while(TRUE) thinkO take.forks ()...

  • Complete: Chapter 4 Problem Set < Back to Assignment Attempts: | Average: /2 Attention: Due to a ...

    Complete: Chapter 4 Problem Set < Back to Assignment Attempts: | Average: /2 Attention: Due to a bug in Google Chrome, this page may not correctly. Click here to learn more. 11. The effect of transformations of scale on the mean and standard deviation You just completed a small research project for your psychology class concerning the effects of an event that happened two years ago on women's opinions and actions today. The mean age of participants in your study...

  • Question For this problem, consider the function y=f(x)= |x| + x 3 on the domain of...

    Question For this problem, consider the function y=f(x)= |x| + x 3 on the domain of all real numbers. (a) The value of limx→ ∞f(x) is . (If you need to use -∞ or ∞, enter -infinity or infinity.) (b) The value of limx→ −∞f(x) is . (If you need to use -∞ or ∞, enter -infinity or infinity.) (c) There are two x-intercepts; list these in increasing order: s= , t= . (d) The intercepts in part (c) divide...

  • Note: This test contains ten questions from chapter 19 to chapter 27. For full credit, you...

    Note: This test contains ten questions from chapter 19 to chapter 27. For full credit, you should show all the steps of your numerical answers. No credit would be earned for just circling the right answers. Chapter 19 - Electric Potential and Electric Potential Energy 1. Two point charges are of + 7 uC and - 4 C are held at the corners of a rectangle as shown. The lengths of the sides of rectangle are 0.15 m, and 0.05...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • Write a C program for: One technique for dealing with deadlock is called “detect and recover.” In...

    Write a C program for: One technique for dealing with deadlock is called “detect and recover.” In this scheme, some procedure is used to identify when a deadlock occurs, and then another procedure is used to deal with the blocked processes. One technique to identify a deadlock is to maintain a resource graph that identifies all processes, all resources, and the relationships between them (that is, which processes exclusively own which resources, and which processes are blocked waiting for which...

  • In Problem Set 7 you designed and implemented a Message class. This time, let's design and...

    In Problem Set 7 you designed and implemented a Message class. This time, let's design and implement a Mailbox class in a file named Mailbox java. Do the following with this class • You may use the Message class from PS 7. You will have to add new features to the Message class from PS 7 as you work through this problem. You are welcome to start with my sample solution if you wish • Suppose there are multiple mail...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT