By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

semaphores

P: 16
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:

Expand|Select|Wrap|Line Numbers
  1. setup semaphores, etc
  2. .
  3. .
  4. proc 1 doing first thing
  5. proc 2 doing next thing
  6. proc 1 doing next thing
  7. proc 2 doing next thing
  8. proc 1 doing last thing
  9. .
  10. .
  11. shutdown semaphores, etc
  12.  
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
Jun 11 '08 #1
Share this Question
Share on Google+
5 Replies


gpraghuram
Expert 100+
P: 1,275
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
Jun 12 '08 #2

P: 68
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
Jun 12 '08 #3

P: 16
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.
Jun 12 '08 #4

Expert 10K+
P: 11,448
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
Jun 12 '08 #5

P: 16
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
Jun 13 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.