Operating System - HP-UX
1757749 Members
1927 Online
108863 Solutions
New Discussion

Error handling for sem_open() API

 
meghana9490
Occasional Advisor

Error handling for sem_open() API

Hi ,

I have written the wrapper function for sem_open() based on my requirent , but my team asking to add more error contions to that, like error handling 

I am attching the sample code, I had written,Can you please help on this code how to handling error or return values 

sem_t *sem;
sem = sem_open(sem_path, flag, mode, value);
if (sem == (void *)-1) {
FYI(FYI_FATAL,"sem_open() failed %s\n",__FUNCTION__);
}
return sem;
}

meghanagujjeti
3 REPLIES 3
Steven Schweda
Honored Contributor

Re: Error handling for sem_open() API

> [...] my team asking to add more error contions to that, like error
> handling

   With my weak psychic powers, I don't know what sort of error handling
your team might like.

   According to the documentation, sem_open() sets errno when it fails.
You might do something to display or return that value.

> [...] (void *)-1 [...]

   "(sem_t *)(-1)" might be a little classier.  In some environments,
the macro SEM_FAILED is defined, so you wouldn't need to worry so much
about the implementation.

> FYI(FYI_FATAL,"sem_open() failed %s\n",__FUNCTION__);

   Also, if I see a message like "sem_open() failed", I'd like to know
_what_, exactly, it was trying to open. In this case, sem_path.

meghana9490
Occasional Advisor

Re: Error handling for sem_open() API

   EACCES The semaphore exists, but the caller does not have
              permission to open it.

       EEXIST Both O_CREAT and O_EXCL were specified in oflag, but a
              semaphore with this name already exists.

       EINVAL value was greater than SEM_VALUE_MAX.

       EINVAL name consists of just "/", followed by no other
              characters.

       EMFILE The per-process limit on the number of open file
              descriptors has been reached.

       ENAMETOOLONG
              name was too long.

       ENFILE The system-wide limit on the total number of open files
              has been reached.

       ENOENT The O_CREAT flag was not specified in oflag and no
              semaphore with this name exists; or, O_CREAT was
              specified, but name wasn't well formed.

       ENOMEM Insufficient memory.


These are the error condifion , So i need implement those error contions
meghanagujjeti
Steven Schweda
Honored Contributor

Re: Error handling for sem_open() API

> These are the error condifion , [...]

   Thanks, but I had already seen the sem_open "man" page.

> [...] So i need implement those error contions

   I don't know what that means to you,

>    With my weak psychic powers, I don't know what sort of error handling
> your team might like.

   Still true.