Shop OBEX P1 Docs P2 Docs Learn Events
semaphore — Parallax Forums

semaphore

parskoparsko Posts: 501
edited 2006-05-10 18:39 in Propeller 1
I got to this part of the manual, and started to get confused.· I feel that I grasp about 75% of the concept.·

Regardless would it be possible·for someone to explain this concept with real life tangible objects, like apples and oranges.· I feel that if I were to read an example of the concept behind these things, I would be much more in the know of how to use them.· Being a mechanical guy, it is easiest for me to concieve a thought with real life objects, not just a register that, for what it is worth, barely exists.

A.K.A.· Why do these exist in the propeller?· Why/how do I use them?

Any thoughts?

-Parsko

PS- PLEASE do not refer me to a wikipedia article.

PPS- I get this warm and fuzzy that someone at Parallax has been waiting for someone like me to ask just such a question, and is REALLY eager to 'splain this to us!·smilewinkgrin.gif

Comments

  • GadgetmanGadgetman Posts: 2,436
    edited 2006-05-10 08:27
    A semaphore is simply a 'Busy' flag, and is to be used when a COG or object needs to have exclusive access to a common resource.

    Say that you have a block of memory(larger than a Long) which contains some sort of record, and one object has as its main task to update this record, and that while it is being updated, other COG/Objects must not read the memory block as it may be inconsistent.

    Object_update runs like this:
    (Pseudocode)
    Loop
    If SET_Semaphore
    Update_datablock
    Release_Semaphore


    The other objects must also try to SET_semaphore to see if they can gain access to the resource.
    If they get access, they can read it, if not they'll have to try again, until they get access, or go do something else.

    What kind of resource that you choose to protect is up to you, though.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't visit my new website...
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-05-10 11:04
    For some reason semaphores intimidate me too. I worry that I will forget that I have to use them and be rejected. [noparse][[/noparse]Nobody likes rejection].

    They are to avoid 'cross Cog clobbering' when sharing information and related services from the hub. It is like preventing everyone from talking at the same time on a party line.

    Am I so old that I am the only one that understands what a party line is? That is when four homes share the same phone number to save money. I suspect it is long gone, like the paper soda straw.

    If you really have a need for it, you will understand it. I hope and imagine that it will be part of the tutorial.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)

    ······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
  • parskoparsko Posts: 501
    edited 2006-05-10 11:27
    "cross cog clobbering" prevention is part of the 75% I understand. It is like a key, to a certain extent. How do they relate to variables? Variable are "cross cogged", so what is the difference. Isn't it true that variables can only be used by one cog at a time? Gotta work, more questions later...

    -Parsko
  • GadgetmanGadgetman Posts: 2,436
    edited 2006-05-10 11:41
    Yes, variables can only be used by one COG/Object at a time, but that time is like... a couple of clock cycles...

    So, any time that you want to stop one COG/Object from changeing a variable while another one uses it, you protect it with a semaphore.
    Of course, it's you as a programmer who must decide which resources are to be protected, and that all COG/Objects adhere to it by using Semaphores.

    No, there's no 'map' to connect a resource of any kind to a Semaphore.
    That is something you will have to do yourself.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't visit my new website...

    Post Edited (Gadgetman) : 5/10/2006 11:45:25 AM GMT
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-05-10 11:59
    One LONG appears to be no problem, just use it and run away.

    These are need when one Spin Cycle on the HUB isn't enough to complete the task.
    But if you think in terms of ARRAYS, then one COG has to do all its reading, writing, or housekeeping before another COG tries to mess with the same ARRAY.

    So, the semaphone just keeps the others waiting until the busy COG gets done.

    I still do have a clue what kind of task I will be needing them for, but I am sure that will come.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)

    ······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-05-10 15:53
    Ok, you wanted·a real world example, Ive got one for you, its not a perfect match but it will work enough for you to get a better understanding.

    Imagine there is a restaurant which seats by reservation only. There are X number of tables in the establishment (memory locations). If you have only one maître d' (cog) scheduling reservations for the tables, you dont have any problems, the maître d' has access to the reservation book (index into the memory locations indicating use), and can immediately see if a table is open for the time requested. Now say you have a larger restaurant and need 2 phone lines and 2 maître d' taking reservations, it happens the two phone are located a distance away from each other, but you still only have one reservation book located on a pedestal·between the two phones. Say two calls for·a reservation come in at the same time, and they are trying to reserve a table for the same time, and there is only one table left open for that time. The first maître d' goes over·to the reservation book, sees the opening, and returns to the phone to express that they are in luck, there is one table left open. Meanwhile the other maître d' goes over, also sees their is one table left open (the first hadn't gotten to marking it as reserved yet) and returns to the phone to express that they are in luck, they got the last table availible, and to his suprise when he hangs up the phone and returns to the reservation book,·finds the first maître d' had just marked last table as booked.

    This is essentially what can happen when more than one·cog·is trying to access the same data structure when they are both trying to add, or take away from the data structure at the same time (this is called multi-producer, or multi-consumer systems). Using a semaphore is the same as when a maître d' receives a request, he goes over to the reservation book, picks it up and takes it back to the phone where he tells the person on the phone the table is availible and marks it as reserved before returning it to the pedestal. When the second maître d' receives a call for a reservation, he sees the book is being used and says "Can you wait one moment before I can see if the table you requested is availible".

    Hope this example helps you to understand where semaphores are useful.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1+1=10

    Post Edited (Paul Baker) : 5/10/2006 4:01:00 PM GMT
  • parskoparsko Posts: 501
    edited 2006-05-10 16:13
    Yes, that seems to clear it up for me, thanks Paul. It seems more simple than I was thinking it was. Also, I suppose I was confused about the variable concept that Gadgetman also cleared up.

    So, the reservation book (semaphore) cannot be changed by anyone other than the maitre dude (hub). BUT, once one is sitting at the table (has control over the variable via one of 8 semaphores), he can order whatever he wants (manipulate the "reserved" variable) all he wants while it is checked out. When the semaphore is checked back in, the variable may exist (be) as a different value.

    Is there a reason for only 8 semaphores (is there a reason for more is a better question)? My guess is due to there being 8 bits in a byte and/or 8 cogs/prop.

    In theory, if Parallax were to design a new Propeller, could one have more? My guess, yes.

    -Parsko
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-05-10 16:36
    actually, its a little different. The problem that arises that requires semaphores, is with the pointers used to indicate a position in data structure (the reservation book actually represents to pointer). Because it takes a couple hub rotations to modify the pointer (read it, change it, write it back), theres the possibility of another cog getting the old copy (after the first reading) modifying it, then writing it back.

    Say the first cog get the pointer to the head of a FIFO, sees its value is 3, writes the data to location 3, increments the pointer to 4, then writes back 4 into the head pointer. This takes a minimum of 3 hub rotations (pointer read, data write, pointer write; properly coded you would actually do pointer read, pointer write, data write: but there is still one hub rotation in there for the pointer update to occur). Now say there is another cog, also trying to add something to the fifo, and it reads the pointer right after the first cog read it, it also sees the value is 3, writes data to location 3, increments the pointer to 4 and writes back 4 to the head pointer. So now after two cogs have accessed the FIFO, the FIFO only shows one data having been added, and the first cog's data has be written over by the second cog.

    A semaphore fixes this, by controlling who can access the pointer. If the semaphore is locked, any other cog will see this and know it isn't safe to use yet.

    You'll find through experience, that 8 semaphores is more than enough. The reason is that multi-consumer/multi-producer data structures are not that common. Most times you'll have a single producer and a single consumer (such as a FIFO between two cogs to pass data from one to the other). In this case there is no need for semaphores because each cog has its own pointer that only it uses (the producer of data uses the head pointer and the consumer of data uses the tail pointer), so no semaphore is nessesary.

    The need of semaphores is actually rare enough, that I requested they provide a dual use in the next generation propeller for conditional execution within assembly programs (like IF_SEM0/IF_NSEM0). In this role they could serve as a general flag (say a half carry, or perhaps a condition settable by another cog whether to execute a portion of code). I do not know whether this will be implemented though.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1+1=10
  • Tracy AllenTracy Allen Posts: 6,656
    edited 2006-05-10 18:39
    There is also this informative thread:
    http://forums.parallax.com/showthread.php?p=574710

    How do we translate the ma
Sign In or Register to comment.