问题描述
这是用软件编写的测试和设置:
Here's the Test and Set written in software:
boolean TestAndSet(boolean *target) {
boolean rv = *target;
*target = TRUE;
return rv;
}
和
do {
while(TestAndSetLock(&lock))
; // do nothing
// critical section
lock = FALSE;
// remainder section
} while(TRUE);
我们可以在不支持硬件级别测试设置的CPU中使用该机制吗?如果是这样,如何确保原子性?
Can we use the mechanism in CPUs that do not support test-and-set at the hardware level? If so, how is atomicity assured?
推荐答案
在没有硬件支持的情况下,测试和设置无法在软件中实现.
Test and Set cannot be implemented in software without hardware support the way you proposed.
这是因为上面的TestAndSet
被认为是用于确保相互排斥的机制,但它不是原子的.但是,原子性只是互斥的另一个名称:TestAndSet
的内部变量必须通过确保两个进程不能同时执行其代码来加以保护.
It is because TestAndSet
above is supposed to be a mechanism to be used to ensure mutual exclusion, but it is not atomic. Atomicity, however, is just another name for mutual exclusion: the internal variables of TestAndSet
must be protected by ensuring that two processes cannot execute its code concurrently.
因此,您以这种方式定义了一种确保相互排斥的方法,它本身需要某种机制来确保相互排斥.这个技巧可以玩很多次,但是如果没有某种硬件支持,就不会有任何实质性进展.
So this way you defined a method for ensuring mutual exclusion which itself requires some mechanism to ensure mutual exclusion. This trick can be played a number of times, but there will not be any real progress without some sort of hardware support.
这篇关于可以在没有硬件支持的情况下以软件实施“测试和设置"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!