Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote