(turn
flag 值造成嚴重的 shared memory contention,依照 generic approach 的精神,可以 確定這個 P-loop 非常適合加上 local spin,其它的 loop 不但屬於 S-loop 而且加上 local spin
會造成 dead lock。由於 local spin 是額外加上 variables 和 spin 動作,並沒有變動與 safety property 有相關的 code,所以仍維持原有的 safety property,實際上 generic approach 所 得之 algorithm D2,如 Algorithm 3,只在原始的 algorithm D0加上額外三行 code(以方 框標識)。
在加上 local spin 之後比較 D0與 D2行為,基本上驗證了前面 the generic approach 章 節所述,參考 progress property 證明,稍加瞭解 P-loop 中 processes 互動方式,即可找到 loser path 上的適當位置加 local spin,原本許多 processes 在 P-loop 不斷地重新讀取turn 值 與 flag(turn)值的動作因為 local spin 機制的加入而被暫停。因為 local spin 位置選擇成 功,讓我們援用 D0的 progress property 證明即可以輕易仿製成 D2的 progress property 證 明,知道必然存在一個 process j 可以進 CS。那麼當 process j 出了 CS 在 exit region 執 行 line E2 釋放所有 local-spinning processes 後,就可以讓系統其他 processes 繼續進行,
而原本造成非常嚴重的 shared memory contention 也因為 local spin 有了大幅度的改善。
在與 2-dimensional permitted bits 相較之下,很顯然 generic approach 造成的 shared memory contention 是 比 較 嚴 重 的 , 因 為 在 exit region 釋 放 的 processes 數 量 在 2-dimensional permitted bits 模式會≤ 在 generic approach 模式,我們用 tree 來呈現這兩者 的不同,若 process α 在等待 process β的釋放,那麼 process β為 process α 的 parent node,依照這樣的規則可以畫出示意圖 Figure 4,root 就代表可以進入 CS 的 process,
當 root 釋放 children 時,這些 processes 會競爭成為 root,沒有成為 root 的 processes 將 會成為新 root 的 children,或是 level 更深的位置,利用 Figure 4 我們也可以發現 2-dimensional permitted bits 中 level 越深的 processes 比在 D0中更容易發生 starvation,雖 然降低了 shared memory contention,但是某些 processes 得承擔更嚴重的 starvation,而 generic approach 形成的 tree 只有 2 levels,釋放所有 local-spinning processes 會讓整體系 統的行為回復到跟 D0一樣。
另外值得注意的是在 exit region 中釋放 processes 的動作是在 extended CS 之外,也 就是說有可能會有兩個以上的 processes 在做釋放的動作,但是這個並沒有影響 algorithm 的正確性,因為任何一個被釋放的 process 都必須再依照原本的 algorithm 執行,為 algorithm 加上 local spin 機制時只擔心 processes 沒有被釋放,但是不擔心被重複釋放或 同時釋放,而 focused release 可以減少這種重複性釋放。
Figure 4: The waiting-release relation
4.2 Knuth’s mutual exclusion algorithm and its variation 4.2.1 The original version: Algorithm K
0Knuth’s[6]和 Dijkstra’s[4]最大的不同點在於它將turn:=i這個動作移到了 extended CS 中,如 Algorithm 4,這不但減少了 processes 對turn 複寫的競爭,也確保 turn 值在任 何的瞬間只有一個 process 可以變更其值。在 safety property 的設計上維持與 Dijkstra’s[4]
一致的做法,而在維持 progress property 上為兼顧 bounded waiting 的特性就有不同的設 計。
Shared variables:
z turn∈{0,...,n−1}, initially arbitrary, writable by all processes
z for every i , 0≤i≤n−1, flag(i)∈{idle,want−in,in−cs}, initially idle , writable by process i and readable by all processes
Process i :
(private variable j: integer) R: ** Remainder Region **
Claim:K0具備 progress property。
Proof by contradiction:假設在某時刻S 無任何 process 在 CS 而且已有 process 在 trying region,在 S 之後無任何 process 可進 CS。因為改變turn 值的動作 line T12 及 E1 都在 extended CS 之中,所以在 S 之後 turn 值不會有任何變動。因為 processes 數量是有 限個,不失ㄧ般性假設在 S 之後存在某個時刻 S1,在 S1 之後所有的 processes 都在 remainder region 或 trying region 中,且沒有任何 process 再進入 trying region 中,則存在 一個 index 最靠近 turn 值(包含等於)的 process α 可以順利通過 line T4-T7 的測試,其