官术网_书友最值得收藏!

Software

Software-defined mutual exclusion implementations are all based on busy-waiting. An example is Dekker's algorithm, which defines a system in which two processes can synchronize, employing busy-wait to wait for the other process to leave the critical section.

The pseudocode for this algorithm is as follows:

    variables
wants_to_enter : array of 2 booleans
turn : integer

wants_to_enter[0] ← false
wants_to_enter[1] ← false
turn ← 0 // or 1
p0:
wants_to_enter[0] ← true
while wants_to_enter[1] {
if turn ≠ 0 {
wants_to_enter[0] ← false
while turn ≠ 0 {
// busy wait
}
wants_to_enter[0] ← true
}
}
// critical section
...
turn ← 1
wants_to_enter[0] ← false
// remainder section
p1:
wants_to_enter[1] ← true
while wants_to_enter[0] {
if turn ≠ 1 {
wants_to_enter[1] ← false
while turn ≠ 1 {
// busy wait
}
wants_to_enter[1] ← true
}
}
// critical section
...
turn ← 0
wants_to_enter[1] ← false
// remainder section

(Referenced from: )

In this preceding algorithm, processes indicate the intent to enter a critical section, checking whether it's their turn (using the process ID), then setting their intent to enter the section to false after they have entered it. Only once a process has set its intent to enter to true again will it enter the critical section again. If it wishes to enter, but turn does not match its process ID, it'll busy-wait until the condition becomes true.

A major disadvantage of software-based mutual exclusion algorithms is that they only work if out-of-order (OoO) execution of code is disabled. OoO means that the hardware actively reorders incoming instructions in order to optimize their execution, thus changing their order. Since these algorithms require that various steps are executed in order, they no longer work on OoO processors.

主站蜘蛛池模板: 洱源县| 海伦市| 临桂县| 宁河县| 永善县| 漯河市| 平湖市| 安西县| 内乡县| 新巴尔虎左旗| 大洼县| 六盘水市| 高唐县| 凤翔县| 乌拉特中旗| 丹棱县| 喀什市| 黄浦区| 勐海县| 旺苍县| 汶川县| 盐源县| 田阳县| 蒙山县| 华容县| 蕉岭县| 邻水| 宁乡县| 黄浦区| 鹿泉市| 海伦市| 临夏市| 黄冈市| 九台市| 石泉县| 公安县| 松阳县| 天津市| 金沙县| 日照市| 依兰县|