written 3.1 years ago by | • modified 3.1 years ago |
Busy Waiting
While a process is in its critical section, any other process that
tries to enter its critical section must loop continuously in the call to acquire()
.
In fact, this type of mutex lock is also called a spinlock because the process
“spins” while waiting for the lock to become available. (We see the same issue
with the code examples illustrating the test and set() instruction and the
compare and swap() instruction.)
This continual looping is clearly a problem in a real multiprogramming system, where a single CPU is shared among many processes. Busy waiting wastes CPU cycles that some other process might be able to use productively
The definitions of the wait() and signal() semaphore
operations just described presently the same problem. To overcome the need
for busy waiting, we can modify the definition of the wait()
and signal()
operations as follows: When a process executes the wait() operation and finds
that the semaphore value is not positive, it must wait.
However, rather than engaging in busy waiting, the process can block itself. The block operation places a process into a waiting queue associated with the semaphore, and the state of the process is switched to the waiting state. Then control is transferred to the CPU scheduler, which selects another process to execute.
It is important to admit that we have not completely eliminated busy waiting with this definition of the wait() and signal() operations. Rather, we have moved busily waiting from the entry section to the critical sections of application programs. Furthermore, we have limited busy waiting to the critical sections of the wait() and signal() operations, and these sections are short (if properly coded, they should be no more than about ten instructions). Thus, the critical section is almost never occupied, and busy waiting occurs
Rarely, and then for only a short time. An entirely different situation exists with application programs whose critical sections may be long (minutes or even hours) or may almost always be occupied. In such cases, busy waiting is extremely inefficient.