PDA

View Full Version : Whats a Semaphores?



board Lizard
04-09-2003, 09:50 PM
Not sure if anyone remembers or not.. but a few weeks ago I had a project with threads (pthreads to be exact) and didn't know what a thread was. Well, after following some of the leads given to me by you guys and with my own research, I was able to complete the program and recieved a grade of 97ish (195 out of 200 points).

Well now its project #2 time and this time I have to use semaphores in a different type of program and my question to you guys is.. whats a semaphore?

jgorrell
04-09-2003, 09:52 PM
/sigh.. use search fool! Not here, use dictionary.com! j/k :-P

sem·a·phore ( P ) Pronunciation Key (sm-fôr, -fr)
n.
1. A visual signaling apparatus with flags, lights, or mechanically moving arms, as one used on a railroad.
2. A visual system for sending information by means of two flags that are held one in each hand, using an alphabetic code based on the position of the signaler's arms.


Dunno if that helps; i didn't put alot of effort into it as you can tell, but maybe it will spark a little bulb.

Good Luck.

cavemanbob
04-09-2003, 10:15 PM
It's a programming concept used to prevent more than one thread at a time from accessing shared data which will usually cause very hard to find bugs if the data is modified from different threads. So those 2 definitions are fairly applicable. There are various types of sephamores that can be used depending on the situation as well.

Noob666
04-09-2003, 10:18 PM
Try and see if u can get a hold of a book called
"operating systems" written by William stallings (not sure if it matters, but my copy is fourth edition)
shouldn't be too hard finding as ebook
It has a simple explanation of how to use semaphores in coding ( pseudo code at least).
Sorry, but i'm too lazy to type stuff from the book at 6.25 in the morning :)
Hope that helps

To put it simple : its a way to make one process wait for/comunicate with another, With the use of simple signals

Noob666

Mr Guy
04-10-2003, 09:20 AM
Both good help.


Further information:

Premise:

Threads are not "children" in the same way that a fork creates a seperate play area for children. Threads are the siamese twins of programming. They share their memory, and are quite capable of pissing in each other's pool.

The semaphore is how you limit data to one thread at a time.

Example of why:

Old timey systems allowed for data to be modified by more than one actor at a time. This was used for the following bank exploits:

Teller 1: Query balance
Balance is $200
Teller 2: Query balance
Balance is $200
Teller 1: Withdraw 200, adjust balance to (200 - 200)
Balance is $0
Teller 2: Deposit 200, adjust balance to (200 + 200)
Balance is $400

This sort of thing actually could and DID happen.

A semaphore fixes the problem by forcing the actors to wait for each other. It works something like this:

Teller 1: Can I check the balance?
-> Gets mutex ( Mutual Exclusive Lock)
Teller 1: Query Balance
Balance is $200
Teller 2: Can I check balance?
-> NO MUAHAHAHAHA BASTARD WAIT LIKE THE SCUM YOU ARE
Teller 1: Withdraw $200, adjust balance to (200 - 200)
Balance is $200
Teller 2: Can I check the balance?
-> DIE MOTHER FUCKER
Teller 1: Release mutex, I'm done
Teller 2: Can I check the balance?
-> Gets mutex
Teller 2: Query Balance
Balance $0
Teller 2: Deposit $200, adjust balance to (0 + 200)
Balance $200
Teller 2: Release mutex




Big difference.

The key concepts to learn (or google for):

"Atomic transactions"
"Memory locking"
"Producer/Consumer Problem"
"Race Conditions"

casey
04-10-2003, 11:29 AM
Originally posted by board Lizard
whats a semaphore?

a traffic light

SlowNLazy
04-10-2003, 11:51 AM
Originally posted by Mr Guy
Both good help.

Teller 1: Can I check the balance?
-> Gets mutex ( Mutual Exclusive Lock)
Teller 1: Query Balance
Balance is $200
Teller 2: Can I check balance?
-> NO MUAHAHAHAHA BASTARD WAIT LIKE THE SCUM YOU ARE
Teller 1: Withdraw $200, adjust balance to (200 - 200)
Balance is $200
Teller 2: Can I check the balance?
-> DIE MOTHER FUCKER
Teller 1: Release mutex, I'm done



Oh sheez man, you need to write a proggy book.. I'd buy it just to read it ... You also need to look into writing Search for Dummies & Clues for Dummies...

Oh and write all the error messages for proggies.. Would defintely make my job that much more enjoyable to hear people asking why they were called a dumb ass mutha fooker for deleting system files in their lame-ass attempt to "clean-up" their hard drives!!

Lyroschen
04-10-2003, 03:30 PM
Great post Guy! \\=^ )

Mr Guy
04-11-2003, 08:01 AM
I remain convinced that if more computer concepts could be explain in terms of money freshman would have less trouble in intro. Computers are complicated, money has a way of making things clear. This is why the same retard that can't even explain what he wants on his pizza to you with a list of available toppings in front of him instantly knows if you shorted him two cents.

Ari
04-11-2003, 12:34 PM
This is just a shot in the dark, but as a supplement to all the insightful responses you get here, it might be helpful if you attended the lectures and/or read the text for this class.

Just a suggestion.

board Lizard
04-11-2003, 12:36 PM
freshman? boy, i'm a junior

casey
04-11-2003, 02:49 PM
Originally posted by board Lizard
freshman? boy, i'm a junior

hahahahahaha

you're one to try and belittle your fellow posters, always posting OT and asking intro questions instead of reading your texts and doing some thinking for yourself.

boy, let me guess, your a 'junior' because you went and took all the language AP's and now have 20 hours in 3 different langues and racked up enough AP hours to sit in junior standing during youer second semester at college?

PROTIP: buy the textbook next semester.

board Lizard
04-12-2003, 06:43 PM
Actually I’m a junior because I’ve been attending a university for 3 years now (at the end of this semester.)

I also know full well what a semaphore is and how to implement one, as for all of my other posts.. In case you haven't noticed by now, probably due to the high levels of retard in your blood stream, I’m a troll.

And if you're in doubt of my trolling ability, then I would point you to the first post I made (which was later renamed something along the lines of "How to lurk something") and was also stickied in the RRRRRRRRR section for some time, but i'm far too lazy.

I guess I’m going to have to pick a new user name and use a different proxy now though if I want to continue posting here and making a select few of you look dumb, but that’s not going to be very difficult at all.

PROTIP: Buying (MOST) textbooks is for fucking idiots when much of the material can be located in various online sources or through people who have already taken said course. In other words: way to waste an extra $300 per semester that could be better used on beer and bitches.

casey
04-13-2003, 10:30 PM
way to try and save face, but i dont buy it.

but then again, i also stayed clear of the religious/political stuff you decided to post in general.

and i agree with your PROTIP in a general case, but in the context of your posts, it seems a neccesary expense. After all, if you are really wanting to play troll, dont be surprised you get treated like a retard while wearing a retard uniform.

A good troll would have stayed in character, rather than breaking off to take an easy shot at your assailants.

PS: im glad to see you liked the Lurk-HOWTO, it turned out rather well (esp hai_hai's part in it, but drama just seems to follow him wherever he goes)

board Lizard
04-14-2003, 01:53 PM
A good troll likes a challenge, this is not a challenge.

And yes the HowTo lurk did turn out very well, esp. the part where you said "Try to lurk more" to which I replied "Whats lurk mean? Sorry but I don't know much about linux".. your 3 paragraphs explaining how to lurk after that comment really made me roffles

maggotboy
04-14-2003, 03:09 PM
Mutexes are used to restrict access by multiple threads to a single resource. Mutexes are mutual-exclusive locks. If one thread owns the lock, then another thread must wait until the lock is released.

Semaphores are more of a funnel gate which allows up to X number of threads to access a shared resource before blocking access.

Semaphores start out with a maximum lock count. When a thread locks the semaphore this lock count is reduced until it reaches zero. When no more locks are available and a thread attempts to obtain one, that thread is blocked until one of the other threads releases its lock.

Naturally a semaphore with a max lock count of 1 is considered a mutex.

Maggotboy

cbreaker
04-14-2003, 10:55 PM
That's a good explination.