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:}