1. Introduction
Process synchronization in operating systems means syncronizing threads or processes in a concurrent environment. The main aim is to prevent race conditions and deadlocks.
In this tutorial, we’ll investigate the differences between a semaphore and a spinlock. We can use both of them to synchronize processes.
2. Process Synchronization Tools
First of all, let’s recap some definitions.
A lock blocks other threads from accessing some resources. Only one thread having the lock has access. Other threads requiring the resource need to wait.
A spinlock achieves process synchronization by busy waiting. Hence, threads trying to access the locked section are only looping. Note that the thread doesn’t perform actual work during the loop:
A semaphore is a synchronization tool that doesn’t enforce busy waiting. Simply put, it’s an integer value shared between processes. Above all, it signals multiple processes accessing limited resources in a concurrent environment. Its two operations, namely wait and signal, changes the value of the semaphore:
There are two types of semaphores. A binary semaphore has only two values, as the name suggests. It ensures only one process has access to the critical section.
A counting semaphore can take any positive integer value. This value defines the number of processes allowed to access the shared resource concurrently.
Another well-known process synchronization tool is a mutex. It’s another locking mechanism, not a signaling mechanism like a semaphore.
3. Spinlock vs. Semaphore
In this section, let’s have a deeper look at the spinlock and semaphore.
A spinlock enforces a thread trying to access it to wait in a loop. The thread doesn’t perform a task during this wait time. It only checks if the lock is available or not.
On the other hand, a semaphore achieves process synchronization without busy waiting. Its wait operation puts the process into sleep. Hence, busy waiting doesn’t waste system resources.
When the resource becomes available, the signal operation changes the semaphore value. Then the next process in the waiting list will be wakened up. However, in spinlock, a process waiting for the lock will need to get access by continuously testing it in the loop.
The spinlock implementation is straightforward. It’s preferable when we require the lock for a minimal time. For longer waits, semaphores are more efficient.
A spinlock ensures that only one thread has access to the lock. Likewise, binary semaphores provide mutual exclusion. However, counting semaphores allow multiple processes to access the resource.
If the system only has a single processor, then a spinlock will keep it busy. The processor will be underutilized since a process without the lock would be stuck in a loop. In this case, using semaphores ensures higher processor utilization because the waiting processes won’t run.
As a result, it’s a good practice to disable interrupts in a system sunning spinlock. Conversely, there’s no need to disable interrupts when working with semaphores.
Similarly, preemption should be disabled when working with a spinlock. Again, preemption doesn’t affect semaphores.
4. Summary
Let’s summarize what we’ve learned in the previous section in a table:
Spinlock
Semaphore
Simple synchronization mechanism
More sophisticated process synchronization
The thread will busy-wait
The process sleeps
Wastes resources
Don’t waste resources
Preferable for a very short time
Preferable for complex tasks
Only one process can access the resource
Multiple processes can access the resource
The process holding the lock can’t sleep
The process holding the lock can sleep
Can be used in interrupts
Can’t be used in interrupts
Preemption disabled
Preemption enabled
5. Conclusion
In this article, we’ve compared two synchronization mechanisms.
Spinlock is a low-level synchronization solution. It’s quick and easy to implement. However, it wastes system resources.
Semaphores offer an advanced solution to the process synchronization problem. They don’t waste system resources as they put the waiting processes to sleep. Still, careless use of semaphores may lead to deadlocks.