What is the best way to learn about semaphores?
-
- Posts: 11
- Joined: Sun Oct 21, 2007 6:13 pm
What is the best way to learn about semaphores?
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?
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 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
- C. A. R. Hoare
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:
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.
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.
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.
-
- Posts: 11
- Joined: Sun Oct 21, 2007 6:13 pm
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?
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?