Solved

Urgent : Effeicient wait on multiple semaphores

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 28
sumNumber challenge 16 109
move a line in eclipse 3 80
Trying to run powershell  in a batch file. How do I do this? 8 63
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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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