Code in the Rogers function needs to be modified to get rid of the deadlock Prog
ID: 3730386 • Letter: C
Question
Code in the Rogers function needs to be modified to get rid of the deadlock Prog 3 part 1: semaphores Your program, "prog31.c", will modify the "Dinning Rogers" program we wrote in class (posted on Blackboard as 09_rogers.c) to prevent deadlock using resource ordering. You should ONLY modify the code in the "roger" function... specifically the entry and exit section semaphore code. Recall that resource ordering requires the chopsticks be acquired in the order chopstick[x] then chopstickly] such that x y
Explanation / Answer
The prime reason for deadlock here is the cyclic wait as the order is same for picking up the fork
The idea is to change the order of taking the forks. We can alternate the order of picking up the fork for each roger.
Here is the modified roger function.
void *roger(void *arg) {
int r = (int) (intptr_t)arg;
int fork[2];
fork[0] = fork[1] = r;
f[r&1] = (r+1)%ROG_CNT;
for(;;) {
sem_wait(&(chopstick[f[0]]));
sem_wait(&(chopstick[f[1]]));
eat[r]++;
sem_post(&(chopstick[f[0]]));
sem_post(&(chopstick[f[1]]));
}
return NULL;
}
Thus every roger has a different order of picking up the fork and it will not result in cyclic wait.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.