McAfee Interview Question
Software Engineer / Developersspinlock is used in kernel mode and is blocking or in other words when wait can not be interrupted. In single processor system the thread owning spin lock gets unrestricted access to CPU resource (expect for interrupt of higher priority)
mutex and semaphore are used when wait is interruptible.
Mutex is also a concept meaning synchronization events and timers (dispatcher objects in windows OS) also allow only one thread to become ready.
Fastmutex, kernel mutex and guardmutex are also other synchronization primitives available in windows kernel which implements mutex concept and can be used at different IRQLs depending upon need.
i am perplexed why such questions are asked but i have gone thru this and here is the researched answer..
- anon July 18, 2011First say that what you are telling is about the POSIX semaphores and not the system v semaphore.
and pthread mutex.
a) semaphore is basically a synchronization primitive but mutex is a locking primitive
b) a semaphore can be a mutex but a mutex cannot be a semaphore
c) semaphore can be signalled by any of the thread by using sem_post() but a mutex can be unlocked only by the thread which owns it.
d) semaphores can be used between threads of different processes but mutex work between threads of the same process or between related process.
e) semaphores are system wide and remain in the form of files on the filesystem until cleaned but mutexes are process-wide.
Spinlocks are locks used in the kernel code where we know the code fragment which acquires and releases the lock is deterministic. They continuously poll to know the status of the lock. Busy sleep.