📜  自旋锁和信号量的区别

📅  最后修改于: 2021-09-14 02:11:15             🧑  作者: Mango

1.信号量
信号量基本上是一种用于通过使用简单的整数值来管理并发进程的技术,该值用于控制对多个进程的访问并避免系统中的临界区问题,例如操作系统中的多任务处理。它是一个非负变量,在线程之间共享。它包括进程的等待列表、计数器,还支持两种不同类型的原子操作,即进程同步的等待和信号。它分为二进制信号量和计数信号量。

  • 二进制信号量——二进制信号量只有 0 和 1 两个值。它处理或消除多进程临界区的问题。二进制信号量也称为互斥锁。
  • 计数信号量– 它有助于控制对包含多个实例的资源的访问。这些值具有不受限制的值域。它计算可用资源的数量。

2. 自旋锁:
自旋锁是一种锁定系统机制。它允许线程获取它以简单地循环等待直到锁可用,即线程在循环或自旋中等待直到锁可用。自旋锁被持有一小段时间。自旋锁在多处理器系统中很有用。

自旋锁和信号量的区别:

S.No.

SPINLOCK

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 on 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.