Solved

Urgent : Effeicient wait on multiple semaphores

Posted on 1998-12-02
4
764 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

626 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