What is the best way to learn about semaphores?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
EliteLegend
Posts: 11
Joined: Sun Oct 21, 2007 6:13 pm

What is the best way to learn about semaphores?

Post by EliteLegend »

I've been trying to get a grip on semaphores but for some reason unable to understand them fully.. Is there a place where I can get some explanation through some animations or videos or pictures?
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

Dekker inventer of the P and V operations got his idea from train signal posts. So you might want to check those out.
Author of COBOS
Avarok
Member
Member
Posts: 102
Joined: Thu Aug 30, 2007 9:09 pm

Post by Avarok »

:D

Semaphores... well... there's a problem with processors. They only do one thing at a time.

If two processors, A and B, use the same box... processor A puts a number in a box, it does some stuff, checks what's in the box and expects it to be there. But then just between checking and getting the stuff out of the box, process B changes it. Process A will keep on going with the wrong stuff.

So the smart programmer folk, they came up with this way to keep process B from :twisted: it all up.

They use a number to say who owns what's in the box. If a processor don't own what's in the box, it's not allowed to open the box and has to wait.

That's the strategy behind semaphores and mutexes, more or less, right?
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies.
- C. A. R. Hoare
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

IIRC a mutex is just an abstract idea of a 'thing' that causes mutual exclusion to a resource. It is up to things like semaphores to concrete that abstraction.

The common idea of a 'mutex' is IIRC just a binary semaphore.

EDIT: google define: mutex pulls this up as the first result:
Google wrote: Mutually-exclusive-access flag. Another name for a Semaphore.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

A mutex offers the ability to take and replace it, where only one task will have the mutex at any time. Taking it when it isn't there amounts to blocking your thread until it is.

A semaphore offers the ability for a counter, which blocks your process when you try to take one off when it is 0. A mutex can be implemented based on a semaphore, where the value is set to 1 initially.
EliteLegend
Posts: 11
Joined: Sun Oct 21, 2007 6:13 pm

Post by EliteLegend »

Thank you so much and sorry for the delay in replying... I was wondering on how to decide upon the number of semaphores that have to be used in your problem. Is there something to look for in a problem that helps us decide upon the usage of the total number of semaphores?

As I perceive, there should be one mandatory semaphore mutex (or whatever it is known as) to provide Mutual Exclusion. But what about the others?
Post Reply