You are not logged in.

#1 2010-09-06 16:36:13

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

CMU: Semaphores!

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

#2 2010-09-06 17:33:20

Rip-Rip
Member
Registered: 2008-11-09
Posts: 32

Re: CMU: Semaphores!

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 smile

Offline

#3 2010-09-06 17:39:04

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: CMU: Semaphores!

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

#4 2010-09-06 19:09:50

Rip-Rip
Member
Registered: 2008-11-09
Posts: 32

Re: CMU: Semaphores!

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

#5 2010-09-07 04:16:07

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: CMU: Semaphores!

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

#6 2010-09-17 08:22:48

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: CMU: Semaphores!

Google wrote:

By the way, is a mutex just a binary semaphore?

Yes

Google wrote:

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

#7 2010-09-18 13:03:40

Google
Member
From: Mountain View, California
Registered: 2010-05-31
Posts: 484
Website

Re: CMU: Semaphores!

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

#8 2010-09-18 16:34:15

saline
Member
Registered: 2010-02-20
Posts: 86

Re: CMU: Semaphores!

You'll get used to it.  The vocabulary allows for specificity.

Offline

Board footer

Powered by FluxBB