sem_open/sem_unlink errors
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > sem_open/sem_unlink errors




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    sem_open/sem_unlink errors  
psp


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-07 06:18 PM

I'm using named semaphores to share them acriss processes. Parent
process opens and initializes them while child processes simply open
them.
Once in a while the sem_open call in the parent fails because I've a
OCREAT&O_EXCL flag. However, if I remove the O_EXCL flag it works. I
know this error occurs because it means this semaphore already exists,
but when I do a ipcs from command line I don't see any semaphores.
When I try to remove them in my code before creating by doing a
sem_unlink that code fails too with no such file or directory error.

Below is my code:


if (_role == CREATOR) {
for(int i = 0; i < _numBuffs; i++) {
if( sem_unlink(_semNames[i]) < 0 ) {
perror("sem_unlink:");
exit(-1);
}


if ( (_semFilled[i] = sem_open(_semNames[i],
O_CREAT&O_EXCL, 0666, _initFilledVal)) == SEM_FAIL
ED) {
perror("sem_open:");
exit(-1);
}
}


}


Pleas help.






[ Post a follow-up to this message ]



    Re: sem_open/sem_unlink errors  
Jens Thoms Toerring


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-07 06:18 PM

psp <pradhan.pushkar@gmail.com> wrote:
> I'm using named semaphores to share them acriss processes. Parent
> process opens and initializes them while child processes simply open
> them.
> Once in a while the sem_open call in the parent fails because I've a
> OCREAT&O_EXCL flag. However, if I remove the O_EXCL flag it works. I
> know this error occurs because it means this semaphore already exists,
> but when I do a ipcs from command line I don't see any semaphores.
> When I try to remove them in my code before creating by doing a
> sem_unlink that code fails too with no such file or directory error.

> Below is my code:


>     if (_role == CREATOR) {
>         for(int i = 0; i < _numBuffs; i++) {
>             if( sem_unlink(_semNames[i]) < 0 ) {
>                perror("sem_unlink:");
>                 exit(-1);
>             }


>             if ( (_semFilled[i] = sem_open(_semNames[i],
> O_CREAT&O_EXCL, 0666, _initFilledVal)) == SEM_FAILED) {

I don't know if this will solve your problem (it might also help
figuring out what goes wrong if you would tell what perror() is
printing out as the error, there are quite a number of possibi-
lities), but using a bitwise AND between the flags will rather
likely guarantee that _none_ of them is set (and thus you only
open an already existing semaphore that didn't got deleted be-
cause another process has it still open) - to open a non-existing
semaphore (exclusively) you need to use a bitwise OR, i.e.
'O_CREAT | O_EXCL', to make it work.

>                 perror("sem_open:");
>                 exit(-1);
>             }
>         }
Regards, Jens
--
\   Jens Thoms Toerring  ___      jt@toerring.de
\__________________________      http://toerring.de





[ Post a follow-up to this message ]



    Re: sem_open/sem_unlink errors  
psp


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-07 06:18 PM


Jens Thoms Toerring wrote:[vbcol=seagreen]
> psp <pradhan.pushkar@gmail.com> wrote: 
> 
>
> 
>
> 
>
> I don't know if this will solve your problem (it might also help
> figuring out what goes wrong if you would tell what perror() is
> printing out as the error, there are quite a number of possibi-
> lities), but using a bitwise AND between the flags will rather
> likely guarantee that _none_ of them is set (and thus you only
> open an already existing semaphore that didn't got deleted be-
> cause another process has it still open) - to open a non-existing
> semaphore (exclusively) you need to use a bitwise OR, i.e.
> 'O_CREAT | O_EXCL', to make it work.
> 

Ok you are right about those flags they need to be OR'ed. The error I
get is:
sem_open:: File exists

So I think the semaphore names have persisted in the OS space, it works
when I use only O_CREAT flag, but I would like to make it work with
'O_CREAT | O_EXCL'.
Any suggestions. Thanks.






[ Post a follow-up to this message ]



    Re: sem_open/sem_unlink errors  
Jens Thoms Toerring


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-20-07 12:29 AM

psp <pradhan.pushkar@gmail.com> wrote:
> Ok you are right about those flags they need to be OR'ed. The error I
> get is:
> sem_open:: File exists

That you should only get when you try to open with 'O_CREAT | O_EXCL'
and the semaphore already exists. That could happen if another process
still has the semaphore open - the previous call of sem_unlink() in
that case didn't delete it yet (but without indicating an error,
deletion is just postponed until all processes have detached from
the semaphore). Are you sure that no other processes are still
around that may have it open?
Regards, Jens
--
\   Jens Thoms Toerring  ___      jt@toerring.de
\__________________________      http://toerring.de





[ Post a follow-up to this message ]



    Re: sem_open/sem_unlink errors  
psp


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-20-07 12:29 AM


Jens Thoms Toerring wrote:
> psp <pradhan.pushkar@gmail.com> wrote: 
>
> That you should only get when you try to open with 'O_CREAT | O_EXCL'
> and the semaphore already exists. That could happen if another process
> still has the semaphore open - the previous call of sem_unlink() in
> that case didn't delete it yet (but without indicating an error,
> deletion is just postponed until all processes have detached from
> the semaphore). Are you sure that no other processes are still
> around that may have it open?
I think that could be the case because when I try to recompile the
build fails (which is typically when file is in use). Strange though I
can't see it using ps -u.
Thanks.






[ Post a follow-up to this message ]



    Re: sem_open/sem_unlink errors  
Maxim Yegorushkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-20-07 12:23 PM


psp wrote:
> I'm using named semaphores to share them acriss processes. Parent
> process opens and initializes them while child processes simply open
> them.
> Once in a while the sem_open call in the parent fails because I've a
> OCREAT&O_EXCL flag. However, if I remove the O_EXCL flag it works. I
> know this error occurs because it means this semaphore already exists,
> but when I do a ipcs from command line I don't see any semaphores.

Are you sure that ipcs can show you POSIX semaphores?

man ipcs

ipcs - report XSI interprocess communication facilities status
...

POSIX ipc have names in the filesystem. XSI ipc do not, this seem to be
the reason why ipcs command exists.






[ Post a follow-up to this message ]



    Re: sem_open/sem_unlink errors  
Pierre L.


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-21-07 12:25 AM

On Jan 20, 10:40 am, "Maxim Yegorushkin" <maxim.yegorush...@gmail.com>
wrote:
> psp wrote: 
>
> man ipcs
>
>        ipcs - report XSI interprocess communication facilities status
> ...
>
> POSIX ipc have names in the filesystem. XSI ipc do not, this seem to be
> the reason why ipcs command exists.

FWIW, named semaphores appear in the devshm file system under linux 2.6
(ls -l /dev/shm - ymmv).

--
Pierre






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:26 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register