Solved

Urgent : Effeicient wait on multiple semaphores

Posted on 1998-12-02
4
755 Views
Last Modified: 2013-12-26
Is there any way to do effecient wait for a group of
 semaphores to become zero.
If I have three semaphores s1,s2,s3
I want to wait until all of them are signalled(become zero).
If I am using Busy Wait loop
e.g
in algorithmic form
for(;;)
{
check s1 nowait;
check s2 nowait;
check s3 nowait;
if ((s1 || s2 || s3)=0) exit;
}
then it will lot of CPU time.Instead I want to get notified only
when (s1 || s2 || s3) become true & just wait effeciently all other times.
If any body has some innovative idea to achieve this.

Regards
0
Comment
Question by:plateau
4 Comments
 
LVL 3

Expert Comment

by:dhm
ID: 1294547
If all of your semaphores are part of the same semaphore group (same sem_id), then this can be done with a simple semop().  If they're in separate semaphore groups, then you've got a tougher problem (as you note).

Your description is a little confusing, though: a semaphore is typically used in the "wait until value is at least N" mode (where N>=1).  It's unusual to want to wait until the value is zero, unless the value actually represents some kind of resource and you need to know when to make more of them.

Also, you say you "want to wait until all of them are signalled (become zero)", but I can't tell from your pseudocode whether you really want to wait for *all* to become zero, or for any one of the three.

If you want to wait for all, then you can improve on your loop by picking a semaphore and waiting for it to become zero in "wait" mode, then checking the other two.  If one or the other is non-zero, then repeat the loop, but choose one of the non-zero sems to wait for in "wait" mode.

This will, however, leave you open to race conditions: what if one of the semaphores gets incremented after you check it?

If you actually want to wait for any one of the three to become non-zero, then you're out of luck -- even if all three semaphores are part of the same group.

I can give you a better answer if you'll elaborate on your application.
0
 

Expert Comment

by:graywatson
ID: 1294548
What is decrementing your semaphores?  Maybe the other side can check them when they all when one of thenm goes to 0 and then inform the waiting process.
0
 
LVL 2

Accepted Solution

by:
bedot earned 200 total points
ID: 1294549
if i have understood, you want wait S1 or S2 or S3:
you must create 3 children that will wait the first S1, the second S2, and the third S3;in t(he concept,  the first child that return from the semop() must kill the others and continue;
BUT: a call to the kernel such as semop is uninterruptible; so a kill will not operate during the two others process are sleeping during awaiting the requested operation.

in reality,  the first child that return from the semop() must (after a while necessary to avoid jam) operate an increment  on the two others semaphores to provocate the return from the calling system, and immediately kill the two other chids.

REMARK: the semaphores are synchronization tools that provocates a blocking onto the calling-system; is not very appropriated to your problem.

You can try better instead an other IPC synchro-communication: the files of messages: several can write in, and get messages.

For other cases of exclusive synchronization, remember that is possible to apply an array of operation on an array of counters in a semaphore (that contain 10 counters)

0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6823603
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.

This is the Community Support link, if help is needed, along with the link to All Topics since many new ones were recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thanks,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 78
Tab names are off by one 5 49
sum67 challenge 35 89
firstChar challenge 13 83
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now