Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the procedure put_forks below. Suppose that the variable state[i] was set to THINKING after the two calls to test, rather than before. How would

Consider the procedure "put_forks" below. Suppose that the variable state[i] was set to THINKING after the two calls to test, rather than before. How would this change affect the solution? No code needs to be written; I just need to know how the change of the state would affect the solution.

#define N 5 /* number of philosophers */ #define LEFT (i+N-1)%N  /* number of i's left neighbor */ #define RIGHT (i+ 1)%N  /* number of i's right neighbor */ #define THINKING 0 /* philosopher is thinking */ #define HUNGRY 1  /* philosopher is trying to get forks */ #define EATING 2  /* philosopher is eating */ typedef int semaphore; /* semaphores are a special kind of int */ int state[N]; /* array to keep track of everyone's state */ semaphore mutex = 1 ;  /* mutual exclusion for critical regions */ semaphore s[N]; /* one semaphore per philosopher */ void philosopher(int i) /* i: philosopher number, from 0 to N-1 */ { while (TRUE) {  /* repeat forever */ think();  /* philosopher is thinking */ take_ forks(i);  /* acquire two forks or block */ eat(); /* yum-yum, spaghetti */ put_ forks(i);  /* put both forks back on table */ } } void take_forks(int i)  /* i: philosopher number, from 0 to N-1 */ { down(&mutex);  /* enter critical region */ state[i] = HUNGRY;  /* record fact that philosopher i is hungry */ test(i);  /* try to acquire 2 forks */ up(&mutex);  /* exit critical region */ down(&s[i]); /* block if forks were not acquired */ } void put_forks(i) /* i: philosopher number, from 0 to N-1 */ { down(&mutex); /* enter critical region */ state[i] =THINKING;  /* philosopher has finished eating */ test(LEFT);  /* see if left neighbor can now eat */ test( RIGHT); /* see if right neighbor can now eat */ up(&mutex);  /* exit critical region */ } void test(i) /* i: philosopher number, from 0 to N-1 */ { if (state[i] ==HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) { state[i] = EATING; up(&s[i]); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions