473,497 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

semaphores

16 New Member
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
5 2688
gpraghuram
1,275 Recognized Expert Top Contributor
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
pootle
68 New Member
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
davidcollins001
16 New Member
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
JosAH
11,448 Recognized Expert MVP
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
davidcollins001
16 New Member
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

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

Similar topics

1
1867
by: Nish | last post by:
Hi, I have some code, to which I added usage of posix semaphores. I am working on a solaris platform. After doing that, my process becomes unkillable, that is it will not terminate if sent a...
1
2154
by: Mehmet Turker | last post by:
Hi; In a research project, I want to use shared memory with semaphores. First I have to create an environment for interprocess messaging. I'm thinking of having a main process which creates a big...
2
2274
by: schneider jp | last post by:
Hi everybody For a project of system programming, we need to use semaphores, but i got some pbs... when I compile, i get perror with sem_open("machin", O_CREAT, 0666, 0); and perror says...
11
2983
by: FiLH | last post by:
Hello, I would like to know if posix semaphores are inter processes or just semaphores for threads inside the same process. I have not seen it defined in the posix specification I have found,...
1
556
by: jinksk | last post by:
I have a C# application and a VB6 application that need to share a dialup connection to the internet. Don't ask why, this is one of those corporate "do or die" things. The only way I can see to...
1
4681
by: Edwin New | last post by:
I have a requirement to run two separate postmasters on the one machine. It looks like they compete for resources (semaphores) so won't run concurrently. I compiled twice, specifying different...
5
2599
by: Steven Spencer | last post by:
Hi, I'm doing an IT degree at university, and my lecturer's do not teach m$ technologies. I was wondering if anyone could point me to the implementation of mutex's and semaphores in .net? Or the...
0
1126
arne
by: arne | last post by:
Hi, I am using Perl ithreads. The threads I create shall access a shared hash. In order to control access to the hash, I would like to use a semaphore. (BTW, does the hash need the protection at...
0
1851
by: The Advocate | last post by:
Hello, how can we implement semaphores using monitors. I got how we can do it the way round. I did alot of search but couldnt find any implementation of semaphores using monitors,. Can anyone help me...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.