Solved

Urgent : Effeicient wait on multiple semaphores

Posted on 1998-12-02
4
761 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 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 179
ORA-01403: no data found 43 116
difference between String.subString() and String.subSequence() 6 290
matchUp  challenge 9 134
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Suggested Courses

751 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