|
Home > Archive > Unix Programming > January 2007 > sem_open/sem_unlink errors
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
sem_open/sem_unlink errors
|
|
|
| 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.
| |
| Jens Thoms Toerring 2007-01-19, 1: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
| |
|
|
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.
| |
| Jens Thoms Toerring 2007-01-19, 7:29 pm |
| 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
| |
|
|
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.
| |
| Maxim Yegorushkin 2007-01-20, 7:23 am |
|
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.
| |
| Pierre L. 2007-01-20, 7:25 pm |
| 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
|
|
|
|
|