semaphores

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davidcollins001
    New Member
    • Mar 2008
    • 16

    semaphores

    Hi,

    Firstly please excuse the long ramblingness. I need some conceptual help, I am trying to make two programs that are exactly the same but take it in turns to execute something, so the output looks something like the following:

    Code:
    setup semaphores, etc
    .
    .
    proc 1 doing first thing
    proc 2 doing next thing
    proc 1 doing next thing
    proc 2 doing next thing
    proc 1 doing last thing
    .
    .
    shutdown semaphores, etc
    I have been reading the man pages for semop and semctl and trying to think of a way to make the processes take turns. So far I have implemented one semaphore that each process locks before it does it business and unlocks after. If I force some execution time (sleep(1)) the processes will take turns. If I try with just print statements only one process will execute (presumably it executes fully before the other has a chance to be scheduled).

    I had thought of having 2 semaphores that one process locked before processing but the other had to unlock to signal that it was ready to start, but this seems a bit messy. I had also thought about setting and checking semval using the fact that a positive sem_op will increment semval but I am not sure how to go about this?

    The coding is fairly straightforward , as far as I can see, I just don't understand how to use the semaphores fully so to achieve this. Any input would be appreciated.

    Thanks
    David
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    You should sleep in a process or else the other process may not get a chance to execute.
    Logic is simple, Lock the semaphore do some operation ,unlock semaphore and do some IO operations or sleep in process 1 so that the other process gets a chance and you should do the same stuff there.
    Hope i am clear.
    Raghu

    Comment

    • pootle
      New Member
      • Apr 2008
      • 68

      #3
      Using sleep in multi-threaded or multi-process software is not so great. It is better to use signals to achieve this behaviour. This is because if your program grows you can end up putting sleep all over the place. If you are interested, the boost library www.boost.org has a good implementation for signals and slots.

      Regards

      Comment

      • davidcollins001
        New Member
        • Mar 2008
        • 16

        #4
        Originally posted by gpraghuram
        You should sleep in a process or else the other process may not get a chance to execute.
        Logic is simple, Lock the semaphore do some operation ,unlock semaphore and do some IO operations or sleep in process 1 so that the other process gets a chance and you should do the same stuff there.
        Hope i am clear.
        Raghu

        Thankyou both for replying, I should have said I am using C.

        Raghu, I already have a working version of what you suggest. I don't much like the idea of using sleep to synchronise processes, I don't feel like it is true synchronisation . I would really like it so that the first process waits forever if the second was never started, instead of doing all the work on its own. It is probably a bit contrived but I would like to be able to manipulate the processes. I was wondering if there is neater way to do it from within the semaphore framework? Or should I be using semaphores and signals?

        Any pointers would be appreciated, I am just starting to scratch the surface of semaphores and I would like to understand in what ways I can use their power.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Are you able to use pthreads? If so have a look at the mutexes. They're ideal
          for locking and unlocking and you definitely don't need to use sleeps because
          that wastes CPU time.

          kind regards,

          Jos

          Comment

          • davidcollins001
            New Member
            • Mar 2008
            • 16

            #6
            Originally posted by JosAH
            Are you able to use pthreads? If so have a look at the mutexes. They're ideal
            for locking and unlocking and you definitely don't need to use sleeps because
            that wastes CPU time.

            kind regards,

            Jos
            I have only briefly looked at mutexes, but don't they only enforce only one process to be allowed into the region much the same as a semaphore?

            I have managed to figure out how to get the processes to take turns. For completeness and for anyone else who is wondering I did it with 3 semaphores:

            [CODE]
            sem[0] = 1
            sem[1] = 0
            sem[2] = 1
            [\CODE]

            proc 1
            [CODE]
            while(condition )
            lock sem[0]
            lock sem[2]
            do stuff
            unlock sem[2]
            unlock sem[1]
            [\CODE]

            proc 2
            [CODE]
            while(condition )
            lock sem[1]
            lock sem[2]
            do stuff
            unlock sem[2]
            unlock sem[0]
            [\CODE]

            So sem[1] starts locked blocking proc 2 and becomes unlocked after proc 1 has finished, as proc 2 now free it locks sem[0] blocking proc 1

            Of course, I didn't come up with this on my own, I found a couple of pdf on google - I just can't find the link now.

            David

            Comment

            Working...