Skip to content
Related Articles

Related Articles

Difference between Spinlock and Semaphore

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 28 Sep, 2022
Improve Article
Save Article

1. Semaphore :
Semaphore is basically a technique used to manage concurrent processes by using a simple integer value that is used to control the access on multiple processes and to avoid critical section problem in the system such as multitasking in operating system. It’s a variable that is non-negative and shared between threads. It includes waiting list of process, counter and also supports two different type of atomic operations i.e. wait and signal for process synchronization. It is categorized into binary and counting semaphore.

  • Binary semaphore – Binary semaphore have only two value 0 and 1. It handles or remove the problem of critical section with multiple processes. Binary semaphore is also known as mutex lock.
  • Counting semaphore – It is helpful to control the access to a resource which include multiple instances. These values have an unrestricted value domain. It counts the number of available resource.

2. Spinlock :
Spinlock is a locking system mechanism. It allows a thread to acquire it to simply wait in loop until the lock is available i.e. a thread waits in a loop or spin until the lock is available. Spinlock is held for a short period of time. Spinlock are useful in multiprocessor system.

Difference between Spinlock and Semaphore :




1. Spinlocks can be used only for mutual exclusion. Semaphores can be used either for mutual exclusion or as a counting semaphore.
2. A spinlock is a low-level synchronization mechanism. A semaphore is a signaling mechanism.
3. Spinlocks allows only one process at any given time to access the critical section. Semaphores allow more than one process at any given time to access the critical section.
4. Spinlock can be wasteful if they are hold for a long time duration. In semaphore there is no resource wastage of process time and resources.
5. Only one thread is allowed at a time to acquire the lock and proceed it with a critical section. One or several thread is allowed to access the critical section.
6.  Spinlock are very efficient because they are blocked only for a short period of time. Semaphore are held for a longer period of time. To access its control structure it uses spin lock. 
7. In spinlock, a process is waiting for lock will keep the processor busy by continuously polling the lock.  In semaphore, a process is waiting for a semaphore will go into sleep to be woken up at a any time and the try for the lock again.
8. Spinlocks are valid for only one process. Semaphores can be used to synchronize between different processes,.
9. In spinlock, a process waiting for lock will instantly get access to critical region as the process will poll continuously for the lock.                                                                                                 In semaphore, a process waiting for a lock might not get into 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 critical section.
10. It is busy wait process.                                                                                                                              It is sleep wait process.
11. Spinlock can have only two values – LOCKED and UNLOCKED In semaphore, mutex will have value 1 or 0, but if used as counting semaphore it can have different values.
12 In uniprocessor system spinlock are not very useful because they will keep the processor busy every time while polling for the lock , thus disabling any other process from running.  In uniprocessor system semaphore are convenient because semaphore don’t keep the processor busy while waiting the lock.
13. In spinlock it is recommended to disable the interrupts while holding a spinlock.  Semaphore can be locked with interrupt enabled.
14. Thread cannot sleep while waiting for the lock when failed to get the lock, but it continues loop of trying to get locked. Thread goes sleep for waiting lock when fail to get the lock.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!