You are not logged in.
Pages: 1
Check my understand please! I have been reading two books, Operating Systems Concepts by SGG and Unix Systems Programming by R&R.
I have been reading about semaphores, but they are quite complex. I have looked around online and I haven't really found answers to my questions, or at least not answers that I understand.
What I want to understand is the difference between a semaphore implemented in spinlock, and a semaphore implemented via signal() and wait() calls.
Here are my thoughts:
A spinlock 'shouldn't' be used on a single core system, but it's possible. A spinlock is very useful on a multi-processor system because it's easier to implement and can be fastest. The downside is the overhead on the system. Because it's busying waiting, it is constantly looping and checking the lock and is using CPU resources constantly. If the spinlock is busy waiting for longer than a normal process quantum it is too much of a burden to be useful, in which case the programmer should be using a signal()/wait() semaphore. In other words, spinlocks are good for quick locks. Also spinlocks are typically A to B type locking, and do not involve more than two agents. Forms of spinlocks may be Read-Write locks etc. A spinlock should disable interrupts while a process is inside the critical section.
A pseudo-implementation:
init_lock();
// non-critical code
// critical code
lock(lock);
// critical section is here
// end critical section
unlock(lock); // at this point, if a process is spinning on lock it will check and be able to enter and lock it.
A wait()/signal() implementation basically uses incrementing or decrementing of variables. It's useful on a single core system because the waiting task can block and enter a queue (in other words, not busy waiting). A blocking process will be woken up with a wakeup() call, which happens when another process signals the signal the process is waiting on. A semaphore 'should' be safe with interrupts when a process is within the critical section.
A pseudo-implementation:
Writer Process:
//non critical code
wait(EMPTY_BUFFER);
// critical code
signal(FULL_BUFFER);
Reader Process:
//non critical code
wait(FULL_BUFFER);
// critical code
signal(EMPTY_BUFFER);
In the end they both seem rather similar to me. The only difference I really see is that spinlocks don't block and semaphores block. Another possible difference may be that spinlocks need to disable interrupts and semaphores don't? Spinlocks are not safe on a single core system, whereas semaphores are safe on both?
Last edited by Google (2010-09-06 16:37:36)
Offline
Spinlock doesn't need to disable interrupts. You should disable interrupts only when you're doing kernel developpement and the critical section could be accessed by an interrupt handler. BTW you can't disable interrupts in userland
Offline
So, what happens when a process is executing in a critical section and it's interrupted? The lock stays locked and everything else deadlocks?
Offline
The lock stay locked, you're right, so others threads (there is no writable shared memory between two process), will "spin" during their execution time until the first thread unlock it.
Offline
No one else has any thoughts on semaphores? Is my understanding correct?
By the way, is a mutex just a binary semaphore? Also, is a spinlock a type of mutex?
Offline
By the way, is a mutex just a binary semaphore?
Yes
Also, is a spinlock a type of mutex?
More like a type of implementation of a mutex
"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page
Offline
Is it just me, or is synchronization a topic with way too much vocabulary being tossed around? It feels like the concepts are simple but made very complex with all these words being tossed around.
Offline
You'll get used to it. The vocabulary allows for specificity.
Offline
Pages: 1