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

Prog 3 part 2: semaphores (hold and wait) Your program, \"prog32.c\", will modif

ID: 3729138 • Letter: P

Question


Prog 3 part 2: semaphores (hold and wait) Your program, "prog32.c", will modify the "Dinning Rogers" program we wrote in class (posted on Blackboard as 09_rogers.c) to prevent deadlock by eliminating hold and wait. You should ONLY modify the code in the "roger" function... specifically the entry and exit section semaphore code. Instead of using the sem_wait() method to acquire each chopstick, you will use the sem_trywait) method. The signature for sem trywait is... int sem_trywait (sem_t *sem)5 This method is non-blocking, so it returns immediately, regardless of whether it was able to acquire the semaphore. It returns 0 if the semaphore was acquired and -1 otherwise. You will modify the entry, critical, and exit section code to work in the following way: 1. try to get the left chopstick if that doesn't work, skip everything else and let the infinite for loop start over again if it does work then do 2 a. b. try to get the right chopstick 2. if that doesn't work, release the left chopstick, skip everything else and let the infinite for loop start over again if it does work, then do 3 a. b. 3. eat 4. release both chopsticks

Explanation / Answer

As per the question you are only allwoed to change the roger function, and the roger function after change will be like this..

void *roger(void *arg) {
int r = (int)(intptr_t)arg;
for(;;)
    {
      if(sem_trywait(&(chopstick[r])) == -1) // we will check if the return value of the sem_trywait is -1 or not
      continue;
      if(sem_trywait(&(chopstick[(r+1) % ROG_CNT]))== -1)
      continue;

      //sem_wait(&(chopstick[r]));
      //sem_wait(&(chopstick[(r+1) % ROG_CNT]));
      eat[r]++;

      sem_post(&(chopstick[r]));
      sem_post(&(chopstick[(r+1) % ROG_CNT]));
    }
return NULL;
}

Point to be noted that the program given in the bottom initializes the semaphore with initial value 0 , Are you sure you want to initialize semaphore with 0 value?

P.S: Please give a thumbs up if you like the answer. If you dont like the answer or you have any doubts please let us know it will help use improve our answer.

Happy Chegging!

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