Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Urgent : Effeicient wait on multiple semaphores

Posted on 1998-12-02
4
Medium Priority
?
766 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 800 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

722 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