查看這個示例信號量實現(對於SMP系統),我瞭解多處理器原子檢查需要測試和設置。但是,一旦我們添加原子檢查不是禁用中斷多餘?無論如何,禁用中斷只能通過一個處理器提供原子性。信號量隊列的添加也需要保護。信號量實現:爲什麼要在測試和設置的同時禁用中斷?
class semaphore {
private int t;
private int count;
private queue q;
public semaphore(int init)
{
t = 0;
count = init;
q = new queue();
}
public void P()
{
Disable interrupts;
while (TAS(t) != 0) { /* just spin */ };
if (count > 0) {
count--;
t = 0;
Enable interrupts;
return;
}
Add process to q;
t = 0;
Enable interrupts;
Redispatch;
}
public V()
{
Disable interrupts;
while (TAS(t) != 0) { /* just spin */ };
if (q == empty) {
count++;
} else {
Remove first process from q;
Wake it up;
}
t = 0;
Enable interrupts;
}
}
這是一個家庭作業問題,要求Stack Overflow做作業。請參閱「_pages.cs.wisc.edu/~bart/537/lecturenotes/s10.html代碼來自哪裏(除了使用測試和設置?_」,我們還需要禁用中斷嗎?可能)。這個問題目前不符合http://stackoverflow.com/help/on-topic:「_Questions要求作業幫助必須包括你迄今爲解決問題所做的工作的總結,以及難度的描述你正在解決它_「 – xmojmr 2014-12-19 11:50:36
謝謝,@ xmojmr - 我只是考慮回答這個問題。現在它下降/關閉。 – 2014-12-19 11:51:31
這個問題似乎是脫離主題,因爲它是關於作業轉儲。 – 2014-12-19 11:51:54