Question on SysV Non-blocking semaphore

Question on SysV Non-blocking semaphore

Post by Damo » Sat, 15 Jul 2000 04:00:00



Is there anyone know how to write a non-blocking semaphores function?
I read the book written by Steven R (Unix Network Programming 2) that
there exist a non-blocking semaphores.  I found the code of that
try_wait function.
But i can't use it, it only give error.
Does anybody know why?

#include "semaphore.h"

int sem_trywait(sem_t *sem)
{
 struct sembuf op;

 if (sem->sem_magic != SEM_MAGIC) {
  errno = EINVAL;
  return(-1);
 }

 op.sem_num = 0;
 op.sem_op = -1;
 op.sem_flg = IPC_NOWAIT;
 if (semop(sem->sem_semid, &op, 1) < 0)
  return(-1);
 return(0);

Quote:}

 
 
 

1. mandatory to use SEM_UNDO for non-blocking Semaphores ?

Hi,

  Tried the man pages but found the man pages for
semaphores pretty "loaded" for me to grasp, hence
this posting...

 I needed a non-blocking semaphores so used "semop"
with "IPC_NOWAIT".

--snip--
  struct sembuf v = { 0, -1, IPC_NOWAIT};
  if((ret = semop(sem_id, &v, 1)) < 0) {
   // error handling...
   }
--snip--

  This impl caused some of my processes to stall since the
process which called semop first didn't really release the
semaphore even though I released it explicitly using the
following code fragment in a release funtion.

--snip--
  struct sembuf p = { 0, 1, SEM_UNDO };
  if((ret = semop(sem_id, &p, 1)) < 0) {
     // error handling...
  }
--snip--

  if while opening the semaphore I use "IPC_NOWAIT | SEM_UNDO"
things seem to work fine.
   Is it mandatory to use SEM_UNDO with IPC_NOWAIT for non-blocking
semaphores?
   Another confusing this is that for simple examples (2 simple
processes just taking/releasing semaphores) this seesm to work
nicely without SEM_UNDO :(

 Would appreciate any help/pointers :)

thanks,
Deven

2. >>> named won't return rootservers

3. How to write a non-locking semaphores on sysV linux?

4. Strange self-reboot

5. Blocking connect() fails, non-blocking succeeds?

6. Future of UNIX Security

7. Blocking and Non-Blocking socket

8. Kids and Bath-tubs

9. [2.2] pipe_write can block even with non-blocking fd

10. how to get blocking i/o after non-blocking???

11. [2.5] Non-blocking write can block

12. Blocking & Non-Blocking

13. Non-blocking socket reads block! (Bug?)