Question

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)

{

if(state[i] == HUNGRY && state[LEFT]!= EATING && state[RIGHT] != EATING) {

state[i] = EATING;

up(&s[i];

}

}

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

File Edit Format View Help #include<stdio.h> #include« semaphore.h> #include«pthread.h> #define N 5 #define THINKING 0 #defin

void take_fork(int ph_num) sem wait (&mutex); state[ph num]HUNGRY printf(Philosopher %d test (ph_num); sem_post (&mutex); se

Add a comment
Know the answer?
Add Answer to:
Using the following pseudocode write a c program that solves the dining philosophers problem. #define N...
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
  • How to change this C code to java? dining_philosopher.c #define N 5 /* number of philosophers...

    How to change this C code to java? dining_philosopher.c #define N 5 /* number of philosophers */ void philosopher(int i) /* i: philosopher number, from 0 to 4 */ { while (TRUE) { think(); /* philosopher is thinking */ take_fork(i); /* take left fork */ take_fork((i+1) % N); /* take right fork; % is modulo operator */ eat(); /* yum-yum, spaghetti */ put_fork(i); /* put left fork back on the table */ put_fork((i+1) % N); /* put right fork back...

  • 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 ()...

  • 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[...

  • write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n,...

    write a C program!! Q2 and Q3 Write the following functioned int search(int a[], int n, int key, int **loc); a is an array to be searched, n is the number of elements in the array, key is the search key, and loc is a pointer to the first location of the search key in array a (if found) or NULL otherwise. The function returns 1 if key is found in a, and returns 0 otherwise. Write a main() and...

  • CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from...

    CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from row and // column wise sorted matrix #include<iostream> #include<climits> using namespace std;    #define INF INT_MAX #define N 4    // A utility function to youngify a Young Tableau. This is different // from standard youngify. It assumes that the value at mat[0][0] is // infinite. void youngify(int mat[][N], int i, int j) {     // Find the values at down and right sides of...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • Topics: Arrays in C. For this assignment, you will write a C program that uses its...

    Topics: Arrays in C. For this assignment, you will write a C program that uses its first command line parameter to compute and display a histogram of characters that occur in it. Requirements: Your program must compile and run correctly using the gcc compiler on ale. You must write the corresponding function definitions for the following function prototypes: // set all elements of the histogram to zero void init_histogram(int histo[]); // construct the histogram from string void cons_histogram(char string[], int...

  • How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses...

    How do I do this C++ in a Unix Environment assignment Given dot1m.c 1. The program (dot1m.c) uses mutex to lock and unlock the shared resource (dotstr.sum) for access control as shown below. pthread_mutex_lock (&mutexsum); dotstr.sum += mysum; printf("Thread %ld did %d to %d: mysum=%f global sum=%f\n", offset,start,end,mysum,dotstr.sum); pthread_mutex_unlock (&mutexsum); 2. Modify dot1m.c program to use reader-writer lock (instead of mutex).      Replace the codes (for mutex) by the codes (for reader-writer lock).            To initialize reader-writer lock, pthread_rwlock_initializer.            At the end,...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

  • Write a program, called wordcount.c, that reads one word at a time from the standard input....

    Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...

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