Authors Top

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

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:

spinlock semaphore spinlock

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:

spinlock semaphore 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:

Rendered by

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.

Authors Bottom

If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!