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 ]
|