← 回題庫

卷別

作業系統

選擇

國立政治大學 2025 · 考試 · 第 19 題

In the monitor solution for dining-philosophers problem, a philosopher may start eating (A) at the end of the pickup() function before exiting the function. (B) in the beginning of the putdown() function (C) after exiting the pickup() function and before entering the putdown() function. (D) All of the above.

monitor DiningPhilosophers
{
   enum {THINKING, HUNGRY, EATING} state[5];
   condition self[5];

   void pickup(int i) {
      state[i] = HUNGRY;
      test(i);
      if (state[i] != EATING)
         self[i].wait();
   }

   void putdown(int i) {
      state[i] = THINKING;
      test((i + 4) % 5);
      test((i + 1) % 5);
   }

   void test(int i) {
      if ((state[(i + 4) % 5] != EATING) &&
          (state[i] == HUNGRY) &&
          (state[(i + 1) % 5] != EATING)) {
            state[i] = EATING;
            self[i].signal();
      }
   }

   initialization_code() {
      for (int i = 0; i < 5; i++)
         state[i] = THINKING;
   }
}
  • (A)
    at the end of the pickup() function before exiting the function.
  • (B)
    in the beginning of the putdown() function
  • (C)
    after exiting the pickup() function and before entering the putdown() function.
  • (D)
    All of the above.

標準解析

AI 已複查