大型機彙編語言中的多進程處理是通過某些特殊指令來完成的,比如本文的CS(compare and swap)和CDS(compare double and swap),下面詳細介紹其功能。
在多進程環境中,比如在CICS環境下,某些transaction會被多個task調用,這種情況下某些全局變量(比如counter,flag),就可能同時被多個task同時修改。。。
現在介紹的CS 和CDS指令就是爲此功能而設計的,即如何在多進程中實現序列化更改或者怎麼實現鎖機制。
ICM R7,B'1111',0(R4) put original counter value into R7
LOOPCTR LR R8,R7 set up one copy to modify
LA R8,1(R8) + 1
CS R7,R8,0(R4) update the counter in storage
BC 4,LOOPCTR if the original value destroyed(CC=1), loop
上面這段代碼的功能就是爲了實現把0(R4)處的數值+1,具體流程爲:
1.先把數值move提取到一個寄存器R7裏,以便比較是否在本task更改其數據的時候,其他tasks已經成功地更改了其數據
2.設置個循環體,本循環體的目的就是實現能成功把0(R4)裏的數值+1。
3.如果+1更改成功則control繼續往下走,否則一直執行循環體
CS指令翻譯成僞代碼如下:
Lock the location 0(R4)
If R7 == 0(R4)
Store the value of R8 into 0(R4) and set the condition code to 0
// 0 indicates a successful update, and the program can proceed
Else
Load the value of 0(R4) into R7 and set the condition code to 1
// 1 indicates an unsuccessful update. which means 0(R4)
// had been changed between the time ICM executed and CS
End-if
Unlock the location 0(R4)
有問題請聯繫QQ349106216