#include <semaphore.h>
Link against:
libpthread.lib
Interface status: |
externallyDefinedApi |
|
SEM_FAILED -1
Description
This value is returned on the failure of sem_open()
Interface status: |
externallyDefinedApi |
|
typedef struct _sem_t * sem_t;
Description
used in performing semaphore operations
Interface status: |
externallyDefinedApi |
|
IMPORT_C int sem_destroy(sem_t *sem);
Description
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;
}
Parameters
Return value
int |
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.
|
|
Interface status: |
externallyDefinedApi |
|
IMPORT_C int sem_trywait(sem_t *sem);
Description
Parameters
Return value
int |
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.
|
|
See also:
Interface status: |
externallyDefinedApi |
|
IMPORT_C int sem_wait(sem_t *sem);
Description
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");
Parameters
Return value
int |
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
|
|
See also:
sem_timedwait(sem_t *,const struct timespec *)
Interface status: |
externallyDefinedApi |
|
IMPORT_C int sem_timedwait(sem_t *sem, const struct timespec *abstime);
Description
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");
Parameters
Return value
int |
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.
|
|
See also:
Interface status: |
externallyDefinedApi |
|
IMPORT_C int sem_post(sem_t *sem);
Description
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;
}
Parameters
Return value
int |
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)
|
|
See also: