Manual Pages for Linux CentOS command on man sem_wait
MyWebUniversity

Manual Pages for Linux CentOS command on man sem_wait

SEMWAIT(3) Linux Programmer's Manual SEMWAIT(3)

NAME

semwait, semtimedwait, semtrywait - lock a semaphore SYNOPSIS

#include int semwait(semt *sem); int semtrywait(semt *sem); int semtimedwait(semt *sem, const struct timespec *abstimeout);

Link with -pthread. Feature Test Macro Requirements for glibc (see featuretestmacros(7)): semtimedwait(): POSIXCSOURCE >= 200112L || XOPENSOURCE >= 600 DESCRIPTION semwait() decrements (locks) the semaphore pointed to by sem. If the semaphore's value is greater than zero, then the decrement proceeds, and the function returns, immediately. If the semaphore currently has the value zero, then the call blocks until either it becomes possible to perform the decrement (i.e., the semaphore value rises above zero), or a signal handler interrupts the call. semtrywait() is the same as semwait(), except that if the decrement cannot be immediately performed, then call returns an error (errno set to EAGAIN) instead of blocking. semtimedwait() is the same as semwait(), except that abstimeout specifies a limit on the amount of time that the call should block if the decrement cannot be immediately performed. The abstimeout argu‐ ment points to a structure that specifies an absolute timeout in sec‐

onds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). This structure is defined as follows: struct timespec { timet tvsec; /* Seconds */ long tvnsec; /* Nanoseconds [0 .. 999999999] */ }; If the timeout has already expired by the time of the call, and the semaphore could not be locked immediately, then semtimedwait() fails with a timeout error (errno set to ETIMEDOUT). If the operation can be performed immediately, then semtimedwait() never fails with a timeout error, regardless of the value of abstime‐ out. Furthermore, the validity of abstimeout is not checked in this case. RETURN VALUE All of these functions return 0 on success; on error, the value of the

semaphore is left unchanged, -1 is returned, and errno is set to indi‐ cate the error. ERRORS EINTR The call was interrupted by a signal handler; see signal(7). EINVAL sem is not a valid semaphore. The following additional error can occur for semtrywait(): EAGAIN The operation could not be performed without blocking (i.e., the semaphore currently has the value zero). The following additional errors can occur for semtimedwait(): EINVAL The value of abstimeout.tvnsecs is less than 0, or greater than or equal to 1000 million. ETIMEDOUT The call timed out before the semaphore could be locked. ATTRIBUTES For an explanation of the terms used in this section, see attributes(7). ┌───────────────────────────┬───────────────┬─────────┐ │Interface │ Attribute │ Value │ ├───────────────────────────┼───────────────┼─────────┤

│semwait(), semtrywait(), │ Thread safety │ MT-Safe │ │semtimedwait() │ │ │ └───────────────────────────┴───────────────┴─────────┘ CONFORMING TO

POSIX.1-2001. NOTES A signal handler always interrupts a blocked call to one of these func‐ tions, regardless of the use of the sigaction(2) SARESTART flag. EXAMPLE The (somewhat trivial) program shown below operates on an unnamed sema‐

phore. The program expects two command-line arguments. The first argument specifies a seconds value that is used to set an alarm timer to generate a SIGALRM signal. This handler performs a sempost(3) to increment the semaphore that is being waited on in main() using

semtimedwait(). The second command-line argument specifies the length of the timeout, in seconds, for semtimedwait(). The following shows what happens on two different runs of the program:

$ ./a.out 2 3 About to call semtimedwait() sempost() from handler semtimedwait() succeeded

$ ./a.out 2 1 About to call semtimedwait() semtimedwait() timed out Program source

#include

#include

#include

#include

#include

#include

#include

#include semt sem;

#define handleerror(msg) \ do { perror(msg); exit(EXITFAILURE); } while (0) static void handler(int sig) { write(STDOUTFILENO, "sempost() from handler\n", 24);

if (sempost(&sem) == -1) { write(STDERRFILENO, "sempost() failed\n", 18); exit(EXITFAILURE); } } int main(int argc, char *argv[]) { struct sigaction sa; struct timespec ts; int s; if (argc != 3) {

fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXITFAILURE); }

if (seminit(&sem, 0, 0) == -1) handleerror("seminit"); /* Establish SIGALRM handler; set alarm timer using argv[1] */ sa.sahandler = handler; sigemptyset(&sa.samask); sa.saflags = 0;

if (sigaction(SIGALRM, &sa, NULL) == -1) handleerror("sigaction"); alarm(atoi(argv[1])); /* Calculate relative interval as current time plus number of seconds given argv[2] */

if (clockgettime(CLOCKREALTIME, &ts) == -1) handleerror("clockgettime"); ts.tvsec += atoi(argv[2]); printf("main() about to call semtimedwait()\n");

while ((s = semtimedwait(&sem, &ts)) == -1 && errno == EINTR) continue; /* Restart if interrupted by handler */ /* Check what happened */

if (s == -1) { if (errno == ETIMEDOUT) printf("semtimedwait() timed out\n"); else perror("semtimedwait"); } else printf("semtimedwait() succeeded\n"); exit((s == 0) ? EXITSUCCESS : EXITFAILURE); } SEE ALSO clockgettime(2), semgetvalue(3), sempost(3), semoverview(7), time(7) COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

Linux 2012-05-13 SEMWAIT(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™