Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-08-2005, 04:17 AM   #1
LQ Newbie
Registered: Nov 2003
Posts: 7

Rep: Reputation: 0
How to test for remaining semaphore?

I have created 4 sets of semaphore set.
At any one time, is there a way for me to test whether there are any remaining semaphores for me to use?

Should I use semctl() ?
How would the function be coded ?
Old 11-25-2005, 08:03 AM   #2
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
I don't really understand what you're asking. You can use any semaphore you like, whenever you like.

Do you mean is there a way to find out if there is one which won't block if you attempt to do an operation to it? If this is your question then the answer is yes.

All you do is loop through each semaphore and try and do your operation with semop() but pass the IPC_NOWAIT flag. This means if the operation can't be performed immediately then semop() returns -1 and sets errno to EAGAIN. I would imagine something like the below code should work(not tested it though)

struct sembuf s;
s.sem_op= //the operation you want to do
for(int i=0;i<4;i++) {
    if(semop(SEM_ID, &s, 1) == 0) break;
if(i==4) {
    if(errno==EAGAIN) {
        //No semaphores available
    } else {
        //Another error occurred
} else {
    //Semaphore operation was successful, and was
    //done to semaphore i.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
remaining partition invisible?? wrangler Linux - Newbie 2 03-01-2005 07:26 AM
Some remaining things... amazingdominik Linux - Newbie 2 12-13-2003 09:31 AM
Remaining space on harddisk WiWa Linux - General 4 10-19-2003 10:20 AM
Last remaining ports to close Poetics Linux - Security 6 06-22-2003 05:18 AM
samba / xp / remaining space bobio Linux - Networking 0 02-03-2002 10:37 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration