semaphore.h File Reference

SEM_FAILED

Typedef sem_t

typedef struct _sem_t *sem_t

used in performing semaphore operations

sem_init ( sem_t *, int, unsigned int )

IMPORT_C intsem_init(sem_t *sem,
intpshared,
unsigned intvalue
)

sem_destroy ( sem_t * )

IMPORT_C intsem_destroy(sem_t *sem)

The sem_destroy function destroys the unnamed semaphore pointed to by sem . After a successful call to sem_destroy , sem is unusable until re-initialized by another call to sem_init .

Examples:
sem_t psem;   
if (sem_init(&psem;, 0, 1) < 0) {
        perror("sem_init");
        return -1;
}
if (sem_destroy(&psem;) == 0)
        return -1;
}

See also: sem_init()

Parameters
sempointer to sem_t object that needs to be destroyed
Return Value
Upon successful completion, the function returns zero; otherwise, it returns -1 and set errno to indicate the error.0 if semaphore successfully destroyed, -1 on error, errno set to specific error EINVAL - The sem pointer is NULL or sem points to an invalid object EAGAIN - There are threads waiting on the semaphore.

sem_trywait ( sem_t * )

IMPORT_C intsem_trywait(sem_t *sem)
Parameters
sempointer to sem_t object on which to wait
Return Value
0 if calling thread was able to acquire the semaphore immediately, -1 on error or if acquiring the semaphore would block the calling thread and errno is set to EINVAL - The sem pointer is NULL or sem points to an invalid object EAGAIN - Acquiring the semaphore would block the calling thread.

sem_wait ( sem_t * )

IMPORT_C intsem_wait(sem_t *sem)

The sem_wait function decrements (locks) the semaphore pointed to by sem , but blocks if the value of sem is zero, until the value is non-zero and the value can be decremented.

The sem_trywait function decrements (locks) the semaphore pointed to by sem only if the value is non-zero. Otherwise, the semaphore is not decremented and an error is returned.

Examples:
sem_t psem;   
if (sem_init(&psem;, 0, 1) < 0) {
        perror("sem_init");
        return -1;
   }
 /* Lock Semaphore */
if( sem_wait(psem) == -1 ) {
  perror("sem_wait failed");
  return -1;
}
else
    printf ("Locked successfully
");
sem_t psem;   
if (sem_init(&psem;, 0, 1) < 0) {
        perror("sem_init");
        return -1;
   }
 /* Lock Semaphore */
if ( sem_trywait(psem) == 0)
{   switch(errno)
     {
     case 0:
        printf ("Locked successfully");
        break;
    case EAGAIN:
        printf ("could not lock, try later.....");
        break;
    }
}
else
    printf ("Sem_trywait failed");

See also: sem_getvalue() sem_post()

Parameters
sempointer to sem_t object on which to wait
Return Value
Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error.0 if calling thread was able to acquire the semaphore, -1 on error and errno set to EINVAL - The sem pointer is NULL or sem points to an invalid object

sem_timedwait ( sem_t *, const struct timespec * )

IMPORT_C intsem_timedwait(sem_t *sem,
const struct timespec *abstime
)

The sem_timedwait function decrements (locks) the semaphore pointed to by sem , but blocks if the value of sem is zero, until the value is non-zero or timeout occurs.

Examples:
sem_t mysemp;
struct timespec ts;
int val, sts;
#define TIMEOUT 3
if ( sem_init (&mysemp;, 0, 1) == -1 ) {
      perror( "sem_init failed");
      return -1;
}
       
struct timeval now;
gettimeofday(&now;,NULL);
ts.tv_sec=now.tv_sec + TIMEOUT;
ts.tv_nsec=0;
/* Lock Semaphore */
if (sem_timedwait(&mysemp;, &ts;) == 0)
{  
    switch(errno)
    {
     case 0:
        printf ("Locked successfully");
        break;
     case ETIMEDOUT:
        printf ("could not lock, try later.....");
        break;
    }
}
else
    printf ("Sem_timedwait failed 
")
fprintf(stderr,"Thread wakened up");

See also: sem_getvalue() sem_post()

Parameters
sempointer to sem_t object on which to wait
abstimepointer to timespec struct specifying the absolute time till which to wait
Return Value
Upon successful completion, the value 0 is returned; otherwise the value -1 is returned and the global variable errno is set to indicate the error.0 if wait was successfull, -1 on error and errno set to: EINVAL - Either the sem or abstime pointer is NULL or points to an invalid object. ETIMEDOUT - The absolute time specified by abstime parameter occurred before the semaphore was signaled.

sem_post ( sem_t * )

IMPORT_C intsem_post(sem_t *sem)

The sem_post function increments (unlocks) the semaphore pointed to by sem . If there are threads blocked on the semaphore when sem_post is called, then the highest priority thread that has been blocked the longest on the semaphore will be allowed to return from sem_wait .

Examples:
sem_t psem;   
if (sem_init(&psem;, 0, 1) < 0) {
        perror("sem_init");
        return -1;
   }
if( sem_post(psem) == -1 ) {
  perror( "sem_post failed");
  return -1;
 }

See also: sem_getvalue() sem_trywait() sem_wait()

Parameters
sempointer to sem_t object which should be signalled
Return Value
If successful, the function returns zero; otherwise, it returns -1 and set errno to indicate the error.0 if post was successfull, -1 on error and errno set to: EINVAL - The sem pointer is NULL or sem points to an invalid object ERANGE - the semaphore count would exceed SEM_VALUE_MAX if signaled by this function(the semaphore count is left unchanged in this case)

sem_getvalue ( sem_t *, int * )

IMPORT_C intsem_getvalue(sem_t *sem,
int *sval
)