推 pikachu123:解釋Hoare Monitor 的性質即可 12/20 00:18
※ 引述《mqazz1 (無法顯示)》之銘言:
: 題目有點多..有沒有大大對任何一題有想法的
: 可以分享一下嗎?
: 謝謝
: 5. The following codes with semaphores can be used to implement a monitor for
: synchronization, where F is an external procedure, x is a condition
: variable. Explain the purposes of using semaphores next [5%] and x-sem[5%]?
: semaphore mutex; // (initially = 1)
: semaphore next; // (initially = 0)
: int next-count = 0;
: semaphore x-sem; // (initially = 0)
: int x-count = 0;
: wait(mutex);
: ...
: body of F;
: ...
: if (next-count > 0)
: signal(next);
: else
: signal(mutex);
: ----------------------------------------
: x.wait()
: ┌───────────────┐
: │x-count++; │
: │if (next-count > 0) │
: │ signal(next); │
: │else │
: │ signal(mutex); │
: │wait(x-sem); │
: │x-count--; │
: └───────────────┘
: x.signal()
: ┌───────────────┐
: │if (x-count > 0) { │
: │ next-count++; │
: │ signal(x-sem); │
: │ wait(next); │
: │ next-count--; │
: │} │
: └───────────────┘
semaphore mutex; // (initially = 1) Monitor之procedure的mutual exclusion控制
semaphore next; // (initially = 0)
強迫呼叫x.signal()wait
呼叫x.signal()的process因為mutual exclusion的關係
所以要讓自己進入wait狀態,接著讓呼叫x.signal()的process執行。
int next-count = 0; // 統計因呼叫x.signal()後,正在wait的process數量
semaphore x-sem; // (initially = 0)
當process呼叫x.wait()時,強迫process wait
int x-count = 0; // 統計因呼叫x.wait後,正在wait的process數量
trace看看吧,有錯的話請幫忙糾正一下
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.175.133.11