- spinlock is a locking mechanism that causes the thread trying to acquire the lock to simply wait in a while loop(“spin”) and continuously check the availability of the lock.
- This is similar to Mutex, the only difference lies in the fact that mutex is a waiting lock that is if the lock is not available, its PID will be put to sleep that is in the waiting queue whereas, in the case of spinlock, it keeps spinning until the lock is available.
- There is no context switching and preemption is disabled.
- This is suitable for smaller critical sections else there will be CPU cycle wastage.
- An interrupt handler(ISR) within the kernel must never be put to sleep. It does the system will freeze. Thus spinlock is the most appropriate to be used here.
Basic API of spinlock is similar to Mutex:
pthread_spinlock_t spin_var; pthread_spin_init(pthread_spinlock_t *spin_var, int pshared) pthread_spin_lock(pthread_spinlock_t *spin_var); pthread_spin_unlock(pthread_spinlock_t *spin_var); pthread_spin_destroy(pthread_spinlock_t *spin_var);
- The value of pshared that determines whether the spinlock can be shared with only threads of same process or by any process that has access to memory where spinlock is allocated.
- The values can be PTHREAD_PROCESS_PRIVATE and PTHREAD_PROCESS_SHARED respectively.
- The return value for each API is 0 on success and non zero in case of failure.
Evaluating Spin Locks:
Any locking mechanism must be evaluated on three factors:
This is the major aspect of any locking technique that is only one thread enters the CS and spinlock stands on it.
This ensures that all the threads get a fair chance to enter the CS and do not get starved. But in the case of spinlock, this is guaranteed as maybe the thread spinning may spin forever if the CS section happens too big and if the higher priorities thread are given preference. This is especially true in the case of a single-processor system. The interrupts are disabled in Spin Lock.
This is measured in terms of time overhead because of the lock. This can be judged in a uniprocessor as well as a multiprocessor system.
If the lock is acquired by a thread and there are N-1 number of threads contending for a lock, which means each thread will spin for a duration of a time slice before giving up the CPU, it’s a waste of CPU cycles.
Time overhead(CPU cycles) wastage is comparably less than uniprocessor as there are multiple processors and threads that can be scheduled in any processor and also the critical section is smaller in the multiprocessor system.
- Since this is busy waiting that is thread is not put to sleep(thread descriptor is put to waiting queue), it does not require a context switch.
- It is helpful if the critical section(CS) is smaller.
- It wastes the CPU cycle when the lock is not available and continuously checks for the lock to be available.
- Thread Synchronization
- Critical Section
- Race Condition
- Mutual Exclusion(Mutex)
- Mutex Vs Semaphore
- Spinlock Vs Mutex
- Condition Variables
Categories: Operating system (OS)
Leave a Reply