Question: Consider the below pseudocode where 5 philosophers are implemented as threads that run phl: int status[5]; // all equals to 1 (thinking) -- can
Consider the below pseudocode where 5 philosophers are implemented as threads that run "phl": int status[5]; // all equals to 1 (thinking) -- can be 0 (ready to eat), 1 (thinking), 2 (eating) semaphore mtx; // equals to 1 semaphore p[5]; // all equals to 0 void phl(int index) { while(1) { think(); takefork(index); eat()); putfork(index); } } void takefork(int index) { wait(mtx); status[index]=0; test(index); signal(mtx); wait(p[index]); } void putfork (int index) { wait(mtx); status[index] = 1; } test(left_side(index)); test(right_side(index)); signal(mtx); int left_side(int index) { return (index + 4) % 5; } int right_side(int index) { return (index + 1) % 5; } void test(int index) { if (status[index]= 0 && status[left_side(index)] !=2 && status[right_side(index)] !=2) { status[index] = 2; signal(p[index]);} Briefly describe a scenario where starvation is observed. Suggest a modification to the code in order to address starvation.
Step by Step Solution
3.36 Rating (159 Votes )
There are 3 Steps involved in it
Each philosopher is represented by a thread There are five semaphoresone for each fork and one mutex The status array keeps track of the state of each philosopher thinking hungry eating The takefork f... View full answer
Get step-by-step solutions from verified subject matter experts
