VMWare Inc Interview Question
Software Engineer / DevelopersSpinlock : A process will be busy waiting for the shared resource to be set free by other process for use
Advantage: No context switch overhead - useful when locks are expected to be held for short times
Mutex: The waiting process is swapped and put on the waiting list of the shared resource - it gives way to other processes to use the CPU
Where to use:
Spinlocks are efficient if threads are only likely to be blocked for a short period of time, as they avoid overhead from operating system process re-scheduling or context switching. For this reason, spinlocks are often used inside operating system kernels. However, spinlocks become wasteful if held for longer durations, both preventing other threads from running and requiring re-scheduling. The longer a lock is held by a thread, the greater the risk that it will be interrupted by the O/S 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 a semi-deadlock 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.
Spinlock : A process will be busy waiting for the shared resource to be set free by other process for use
- Anonymous March 21, 2009Advantage: No context switch overhead - useful when locks are expected to be held for short times
Mutex: The waiting process is swapped and put on the waiting list of the shared resource - it gives way to other processes to use the CPU