Saturday, December 31, 2011

Spinlocks

Spinlock is a semaphore inwhich the process spins waiting for the lock thereby requiring busy waiting.
While a process is in its critical section,any other process that tries to enter its critical section must loop continuously in the entry code.

Disadvantages:
1.Wastes CPU cycles
2.The longer a lock is held by a thread, the greater the risk that it will be interrupted by the OS scheduler while holding the lock. If this happens, other threads will be left "spinning" (repeatedly trying to acquire the lock), while the thread holding the lock is not making progress towards releasing it. The result is an indefinite postponement until the thread holding the lock can finish and release it. This is especially true on a single-processor system, where each waiting thread of the same priority is likely to waste its quantum (allocated time where a thread can run) spinning until the thread that holds the lock is finally finished.

Advantages:
They avoid overhead from operating system process re-scheduling or context switching.Hence When locks are expected to be held for short times spinlocks are useful.
1.Implememented in ISRs
2.spinlocks are often used inside operating system kernels.
3.Implemented on multi processor systems where one thread can spin on one processor while another thread performs its critical section on another processor.

Adaptive Mutex:
Most operating systems (including Solaris, Mac OS X and FreeBSD) use a hybrid approach called "adaptive mutex". The idea is to use a spinlock when trying to access a resource locked by a currently-running thread, but to sleep if the thread is not currently running. (The latter is always the case on single-processor systems.


USEFUL LINKS:
http://www.codeproject.com/KB/threads/spinlocks.aspx
http://en.wikipedia.org/wiki/Spinlock







No comments:

Post a Comment