views:

41

answers:

1
+2  Q: 

CMU: Semaphores!

Check My Understanding of semaphores, please!

I understand the idea behind counting semaphores and binary semaphores. However the difference between a spinlock and semaphore implemented with signal() and wait() kind of blend together to me.

For example a spinlock has basically two values (a binary true/false for locked or unlocked). Therefore a spinlock is basically a binary semaphore, correct?

Any process attempting to enter the critical section while another process is inside will be unable to while it's locked, and will spin and continually check the lock status until it is unlocked and then is able to enter and lock it.

A semaphore using a signal() and wait() function essentially add or subtract a value from a variable of some kind. There is a constraint regarding the critical section. It will only be opened when the variable is of some kind of value. An example implementation for a consumer process would be wait(full), then when it's full it executes and at the end it signal(empty). Whereas a producer process may wait(empty) and execute when empty is true, and then when it finishes it signal(full).

What is the difference between wait() and a spinlock that is essentially 'waiting' in a loop?

+1  A: 

Unlike semaphores, spinlocks may be used in code that cannot sleep, such as interrupt handlers.

http://www.makelinux.net/ldd3/chp-5-sect-5.shtml

http://www.linuxjournal.com/article/5833

aaa
As I also understand, spinlocks shouldn't be used in a single CPU system unless it supports a preemptive kernel, if it doesn't have a preemptive kernel it's not a good idea to use a spinlock. A spinlock should disable interrupts while the critical section is being read/written to, correct? // edited
Google
@Google sorry, I do not know much about nonpreemptive kernels
aaa