Table of contents
1.
Introduction
2.
Spinlock
3.
Semaphores
4.
Difference between Spinlock and Semaphore
5.
Frequently Asked Questions
5.1.
What is a spinlock?
5.2.
What is a semaphore?
5.3.
What does the wait function do in semaphore locking mechanisms?
5.4.
What does the signal function do in semaphore locking mechanisms?
5.5.
What are the two types of semaphores?
6.
Conclusion
Last Updated: Mar 27, 2024

Difference between Spinlock and Semaphore

Author Teesha Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Operating Systems

Introduction

Process Synchronisation is one of the most challenging concepts and one of the most vital ones. Semaphores, as well as Spinlocks, prove extremely useful in maintaining process synchronization in the Operating System. Both the spinlock and semaphore are used as locking mechanisms for process synchronization. Both of these works by granting and releasing locks for shared variables and help in the concurrent execution of processes. 

Recommended Topic, FCFS Scheduling Algorithm, Multiprogramming vs Multitasking

Spinlock

A spinlock is a locking mechanism for process synchronization. It is useful when we are working with multiprocessor systems. It enables a thread to obtain a lock on shared variables by waiting in a cycle or spinning till the lock becomes accessible. Spinlock is only retained for a brief time. It has busy waiting as the process or thread continuously tries to acquire a lock on the desired shared variable.

Semaphores

A Semaphore is also a locking mechanism for process synchronization. This locking system includes a waiting queue for each resource present in the system and a counter that stores a non-negative number specifying the number of resources available to be allocated. Moreover, we have two atomic functions - wait() and signal() that are responsible for working this type of locking mechanism. 

The wait function grants the resource if available; else adds the process to the waiting queue and calls the block()/sleep() function for the process. The signal function releases the resource and grants it to a process from the waiting queue by calling the wakeup() function.

Semaphores are of two types:

  • Binary Semaphores: The counter has either value of 0 or 1. 1 denotes that the resource is available, and 0 denotes that the resource is not available. It is used when we have single instance resources.
  • Counting Semaphores: The counter has a non-negative value specifying the number of instances available for a particular resource.

Difference between Spinlock and Semaphore

Spinlock 

Semaphores

Spinlock is a low-level locking mechanism A semaphore is a signaling mechanism.
It is a busy-wait process. It is a sleep wait process.
Spinlocks can be used only for mutual exclusion. Binary semaphores can be used for mutual exclusion and counting semaphores can be used for both mutual exclusion and counting of available resources.
Only one process can access the critical section at a time. More than one process may access the critical section depending upon the number of instances available.
Spinlocks are valid for only one process Semaphores are used to synchronize among different processes.
Spinlocks can have only two values- LOCKED or UNLOCKED Binary semaphores have two values- 0 or 1 but counting semaphores can have any non-negative value.
Threads continuously try to acquire the locks and do not sleep when they don’t get the resources. Semaphores call the wait function that either allocates the resource or sends the process to sleep until an instance is available to be allocated.
In spinlock, a process waiting for a lock will instantly access a critical region as the process will poll continuously for the lock.   In semaphore, a process waiting for a lock might not get into the critical region as soon as the lock is free because the process would have gone to sleep, and when it is wakened up, it will enter into the critical section.

The above table sums up the difference between Priority Inversion and Priority Inheritance.

Check out this problem - Count Inversions

Frequently Asked Questions

What is a spinlock?

A spinlock is a locking mechanism for process synchronization. It enables a thread to obtain a lock on shared variables by waiting in a cycle or spinning till the lock becomes accessible. Spinlock is only retained for a brief time.

What is a semaphore?

A Semaphore is also a locking mechanism for process synchronization. It makes use of two functions wait and signal.

What does the wait function do in semaphore locking mechanisms?

The wait function grants the resource if available; else adds the process to the waiting queue and calls the block()/sleep() function for the process.

What does the signal function do in semaphore locking mechanisms?

The signal function releases the resource and grants it to a process from the waiting queue by calling the wakeup() function.

What are the two types of semaphores?

There are two types of semaphores - Binary semaphores and Counting semaphores. Binary semaphores can have either 0 or 1 denoting the availability and non-availability of resources. Counting semaphores have a non-negative value denoted by the available instances of resources. 

Conclusion

In this article, We have discussed the differences between spinlock and semaphores. 

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy Coding!

Live masterclass