Question

Dining Philosopher problem solution- Vulnerable to starvation in c language or either java - using threads(can use semaphores but with threads) - with proper explanations and comments Please provide o...

Dining Philosopher problem solution- Vulnerable to starvation in c language or either java - using threads(can use semaphores but with threads) - with proper explanations and comments Please provide output of program and explanation of how to run program.

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

file name dinning.c

#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdio.h>

#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N

int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };

sem_t mutex;
sem_t S[N];

void test(int phnum)
{
   if (state[phnum] == HUNGRY
       && state[LEFT] != EATING
       && state[RIGHT] != EATING) {
       // state that eating
       state[phnum] = EATING;

       sleep(2);

       printf("Philosopher %d takes fork %d and %d\n",
                   phnum + 1, LEFT + 1, phnum + 1);

       printf("Philosopher %d is Eating\n", phnum + 1);

       // sem_post(&S[phnum]) has no effect
       // during takefork
       // used to wake up hungry philosophers
       // during putfork
       sem_post(&S[phnum]);
   }
}

// take up chopsticks
void take_fork(int phnum)
{

   sem_wait(&mutex);

   // state that hungry
   state[phnum] = HUNGRY;

   printf("Philosopher %d is Hungry\n", phnum + 1);

   // eat if neighbours are not eating
   test(phnum);

   sem_post(&mutex);

   // if unable to eat wait to be signalled
   sem_wait(&S[phnum]);

   sleep(1);
}

// put down chopsticks
void put_fork(int phnum)
{

   sem_wait(&mutex);

   // state that thinking
   state[phnum] = THINKING;

   printf("Philosopher %d putting fork %d and %d down\n",
       phnum + 1, LEFT + 1, phnum + 1);
   printf("Philosopher %d is thinking\n", phnum + 1);

   test(LEFT);
   test(RIGHT);

   sem_post(&mutex);
}

void* philospher(void* num)
{

   while (1) {

       int* i = num;

       sleep(1);

       take_fork(*i);

       sleep(0);

       put_fork(*i);
   }
}

int main()
{

   int i;
   pthread_t thread_id[N];

   // initialize the semaphores
   sem_init(&mutex, 0, 1);

   for (i = 0; i < N; i++)

       sem_init(&S[i], 0, 0);

   for (i = 0; i < N; i++) {

       // create philosopher processes
       pthread_create(&thread_id[i], NULL,
                   philospher, &phil[i]);

       printf("Philosopher %d is thinking\n", i + 1);
   }

   for (i = 0; i < N; i++)

       pthread_join(thread_id[i], NULL);
}

to run the program use
gcc dinning.c -pthread -o din

Output

./din
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 5 is thinking
Philosopher 1 is Hungry
Philosopher 2 is Hungry
Philosopher 4 is Hungry
Philosopher 3 is Hungry
Philosopher 3 takes fork 2 and 3
Philosopher 3 is Eating
Philosopher 5 is Hungry
Philosopher 5 takes fork 4 and 5
Philosopher 5 is Eating
Philosopher 3 putting fork 2 and 3 down
Philosopher 3 is thinking
Philosopher 2 takes fork 1 and 2
Philosopher 2 is Eating
Philosopher 5 putting fork 4 and 5 down
Philosopher 5 is thinking
Philosopher 4 takes fork 3 and 4
Philosopher 4 is Eating
Philosopher 3 is Hungry
Philosopher 2 putting fork 1 and 2 down
Philosopher 2 is thinking

In this program when you create an pthread all the threads run independently, so any philosopher can take the fork and put the fork back.

Add a comment
Know the answer?
Add Answer to:
Dining Philosopher problem solution- Vulnerable to starvation in c language or either java - using threads(can use semaphores but with threads) - with proper explanations and comments Please provide o...
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
  • Hi, please provide a code solution written in the C++ language to the following programming problem....

    Hi, please provide a code solution written in the C++ language to the following programming problem. The answer you provide has to include code comments, as it is being explained to a person with no technical background. In your answer, please show which portion of the code is a .cpp and which is a .h (if used). We are asked to separate them out (ie. main.cpp, classname.cpp, headername.h). Please show the output of your program to show it compiled error...

  • Language is in Java, please explain the answer using comments, Thank you. I always rate!!! PLEASE...

    Language is in Java, please explain the answer using comments, Thank you. I always rate!!! PLEASE DO NOT import any packages to solve the problem. PLEASE use classes and methods from java.lang.*, since they are automatically imported without import statement. build a method called; public static in[] multbygrp(int[] elems, int grpsize) This method splits arr into subgroups of (equal) size groupSize, and multiply the contents of each subgroup. It returns the individual product in a new list. If splitting can't...

  • Must be done in Java. Provide the rest of the code with full comments and explanation and with proper indentation. Use...

    Must be done in Java. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. CODE PROVIDED: import java.util.ArrayList; import java.util.Scanner; public class Problem3 {    public static void main( String [] args ) {        Scanner in = new Scanner( System.in );        //PLEASE START YOUR WORK HERE        //**********************************************************                                                  //**********************************************************        // PLEASE END YOUR WORK HERE        System.out.print("END OF OUTPUT");    } } 20 points! PROBLEM 3: EXTENDED FAMILY Complete this...

  • TRUE-FALSE     Basic synchronization principles and multithreading 1. Java user threads can implement both busy-waiting and no-busy-waiting...

    TRUE-FALSE     Basic synchronization principles and multithreading 1. Java user threads can implement both busy-waiting and no-busy-waiting policy. 2. Priority inversion avoids deadlocks. 3. Spinlock mutex can be used as an adaptive mutex. 4. Java RTE can be blocked for Input/Output operation. 5. Interrupted user thread, which executes a method in a monitor, must be rolled back to undo any changes it performed. 6. The synchronization primitive by disabling interrupts can be used by an application program. 7. Bounded-waiting requirement is...

  • please read directions first. this is supposed to be a hybrid programe add comments please JAVA...

    please read directions first. this is supposed to be a hybrid programe add comments please JAVA please add comments Programming Project 1 and Programming Project 2 "Hybrid" Your goal is to write a program that combines the functionality of both Programming Project 1andProgramming Project 2 into a single program. That is, your program should read a file ( Numbers.txt) of numbers of type double that contains some duplicates and is ordered smallest to largest. Your program should ignore the duplicate...

  • Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use...

    Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 1” as in the lecture note. This program should work as follows: 1.The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The program will then create a separate threads, producer and consumer thread. 3. Producer thread generates a random number through...

  • Program by force brute in (c++ or java language )

    The question of validity is to answer the question of whether for a Boolean expression consisting of n variables . There is a combination of values of variables that make the result of the expression true or not. In this phrase each The variable can be simple or contradictory. Force brute method of all compounds . Creates and evaluates the possible and the first time the result is true (if it is) the answer to the problem Come and stop...

  • Must be in Java. Please show step by step process with proper explanation and comments. Please...

    Must be in Java. Please show step by step process with proper explanation and comments. Please maintain proper indentation. CODE PROVIDED: Doctor.java HospitalDoctor.java Q1Runner.java import java.util.Scanner; public class Q1Runner { public static void main(String[] args) { Scanner kb = new Scanner(System.in);               // Do your work here        } } You are asked to create the starting point of a system to represent several types of medical doctors. You decided to implement two classes: a superclass...

  • Please use only C language and NOT C++ or Java You are now allowed to use...

    Please use only C language and NOT C++ or Java You are now allowed to use the following • for loops • math.h and pow() function • formatting float and double numbers • switch statements • getchar() function • ASCII chart • do…while loops • break and continue statements • Logical AND (&&), logical OR (||), logical NOT (!) operators Pythagorean triples are three positive integer numbers a, b, c that form the sides of a right triangle, such that...

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