In this tutorial, we’ll discuss the concept of busy waiting as it is used in operating systems. We’ll look into defining busy waiting, give examples, and the purpose of using it. We’ll also review the benefits and problems that exist with it, and give pointers as to how these problems are addressed.
2. What Is Busy Waiting?
Busy waiting, also known as spinning, or busy looping is a process synchronization technique in which a process/task waits and constantly checks for a condition to be satisfied before proceeding with its execution. In busy waiting, a process executes instructions that test for the entry condition to be true, such as the availability of a lock or resource in the computer system.
For resource availability, consider a scenario where a process needs a resource for a specific program. However, the resource is currently in use and unavailable at the moment, therefore the process has to wait for resource availability before it can continue. This is what is known as busy waiting as illustrated below:
There are two general approaches to waiting in operating systems: firstly, a process/task can continuously check for the condition to be satisfied while consuming the processor – busy waiting.
Secondly, a process can wait without consuming the processor. In such a case, the process/task is alerted or awakened when the condition is satisfied. The latter is known as sleeping, blocked waiting, or sleep waiting.
3. When Is Busy Waiting Used For?
Busy looping is usually used to achieve mutual exclusion in operating systems. Mutual exclusion prevents processes from accessing a shared resource simultaneously. A process is granted exclusive control to resources in its critical section without interferences from other processes in mutual exclusion. A critical section is a section of a program code where concurrent access must be avoided.
4. Problems With Busy Waiting
In some operating systems, busy waiting can be inefficient because the looping procedure is a waste of computer resources. In addition, the system is left idle while waiting. This is particularly wasteful if the task/process at hand is of low priority. In that case, resources that can be diverted to complete high-priority tasks are hogged by a low-priority task in busy waiting.
A workaround solution for the inefficiency of busy waiting that is implemented in most operating systems is the use of a delay function. Also known as a sleep system call, a delay function places the process involved in busy waiting into an inactive state for a specified amount of time. In this case, resources are not wasted as the process is “asleep”. A delay function is illustrated below:
After the sleep time has elapsed, the process is awakened to continue its execution. If the condition is still not satisfied, the sleep time is incremented until the condition can be satisfied.
Another approach is to modify the definition of the waiting procedure to accommodate blocking processes with semaphores. A process in busy waiting is blocked and placed on a waiting queue where it does not consume resources. Once the conditions are satisfied, the process is restarted and placed on a ready queue.
5. Desirable Traits of Busy Waiting
Although inefficient, busy waiting can be beneficial in mutual exclusion if the waiting time is short and insignificant. Additionally, busy waiting is quick and simple to understand and implement.
In some operating systems, busy waiting is beneficial for implementing spinlocks. A spinlock enforces a spin/waiting loop on a process that is trying to access a shared resource. i.e it enforces mutual exclusion. Once a spinlock is released, the process continues its execution process. Spinlocks are generally used in operating systems where the number of shared resources is not high enough to cause contentions.
However, just like busy waiting, spinlocks are considered a bad idea when the waiting time is long.
In this article, we’ve discussed busy waiting as used in operating systems. We’ve reviewed the conditions that lead to busy waiting, as well as the alternatives that exist. Finally, we’ve discussed the benefits and problems of using busy waiting.