e., the
waiting process or thread had to wait continually check to see if the condition had changed). While these papers
were groundbreaking, one would not use this approach today. For one thing, these approaches are limited to situations
where only two processes are cooperating, and for another, the busy wait is wasteful of CPU time.
A hardware approach to synchronization is relatively simple, but does have the limitation that it, too,
requires a busy wait. All that is necessary is a single machine instruction that tests some condition and also sets
the condition in the same instruction execution cycle. Since it can??™t be interrupted, such a ???test-and-set??? instruction
can be used for coordination. In pseudocode, here is what a test-and-set instruction does:
boolean testAndSet( boolean lock ) {
if( !lock ) {
lock = true;
return true;
}
else return false;
}
This code says:
The routine name is testAndSet, and
it will return either ???true??™ or ???false??™ (a boolean value).
testAndSet operates on a variable we will call ???lock??™,
and ???lock??™ can have the value ???true??™ or ???false??™ (it??™s a boolean variable).
104 OPERATING SYSTEMS [CHAP. 6
If lock is not true (!lock), then
make lock true, and return the value true to the calling program.
Pages:
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287