SEM_TIMEDWAIT(3)           POSIX Programmer's Manual          SEM_TIMEDWAIT(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       sem_timedwait -- lock a semaphore

SSYYNNOOPPSSIISS
       #include <semaphore.h>
       #include <time.h>

       int sem_timedwait(sem_t *restrict _s_e_m,
           const struct timespec *restrict _a_b_s_t_i_m_e);

DDEESSCCRRIIPPTTIIOONN
       The _s_e_m___t_i_m_e_d_w_a_i_t() function shall lock the semaphore referenced by _s_e_m
       as in the _s_e_m___w_a_i_t() function. However, if the semaphore cannot be
       locked without waiting for another process or thread to unlock the
       semaphore by performing a _s_e_m___p_o_s_t() function, this wait shall be
       terminated when the specified timeout expires.

       The timeout shall expire when the absolute time specified by _a_b_s_t_i_m_e
       passes, as measured by the clock on which timeouts are based (that is,
       when the value of that clock equals or exceeds _a_b_s_t_i_m_e), or if the
       absolute time specified by _a_b_s_t_i_m_e has already been passed at the time
       of the call.

       The timeout shall be based on the CLOCK_REALTIME clock.  The resolution
       of the timeout shall be the resolution of the clock on which it is
       based. The ttiimmeessppeecc data type is defined as a structure in the _<_t_i_m_e_._h_>
       header.

       Under no circumstance shall the function fail with a timeout if the
       semaphore can be locked immediately. The validity of the _a_b_s_t_i_m_e need
       not be checked if the semaphore can be locked immediately.

RREETTUURRNN VVAALLUUEE
       The _s_e_m___t_i_m_e_d_w_a_i_t() function shall return zero if the calling process
       successfully performed the semaphore lock operation on the semaphore
       designated by _s_e_m.  If the call was unsuccessful, the state of the
       semaphore shall be unchanged, and the function shall return a value of
       -1 and set _e_r_r_n_o to indicate the error.

EERRRROORRSS
       The _s_e_m___t_i_m_e_d_w_a_i_t() function shall fail if:

       EEIINNVVAALL The process or thread would have blocked, and the _a_b_s_t_i_m_e
              parameter specified a nanoseconds field value less than zero or
              greater than or equal to 1000 million.

       EETTIIMMEEDDOOUUTT
              The semaphore could not be locked before the specified timeout
              expired.

       The _s_e_m___t_i_m_e_d_w_a_i_t() function may fail if:

       EEDDEEAADDLLKK
              A deadlock condition was detected.

       EEIINNTTRR  A signal interrupted this function.

       EEIINNVVAALL The _s_e_m argument does not refer to a valid semaphore.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       The program shown below operates on an unnamed semaphore. 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 _s_e_m___p_o_s_t(3) to increment the semaphore
       that is being waited on in _m_a_i_n() using _s_e_m___t_i_m_e_d_w_a_i_t().  The second
       command-line argument specifies the length of the timeout, in seconds,
       for _s_e_m___t_i_m_e_d_w_a_i_t().

           ##iinncclluuddee <<uunniissttdd..hh>>
           ##iinncclluuddee <<ssttddiioo..hh>>
           ##iinncclluuddee <<ssttddlliibb..hh>>
           ##iinncclluuddee <<sseemmaapphhoorree..hh>>
           ##iinncclluuddee <<ttiimmee..hh>>
           ##iinncclluuddee <<aasssseerrtt..hh>>
           ##iinncclluuddee <<eerrrrnnoo..hh>>
           ##iinncclluuddee <<ssiiggnnaall..hh>>

           sem_t sem;

           static void
           handler(int sig)
           {
               int sav_errno = errno;
               static const char info_msg[] = "sem_post() from handler\n";
               write(STDOUT_FILENO, info_msg, sizeof info_msg - 1);
               if (sem_post(&sem) == -1) {
                   static const char err_msg[] = "sem_post() failed\n";
                   write(STDERR_FILENO, err_msg, sizeof err_msg - 1);
                   _exit(EXIT_FAILURE);
               }
               errno = sav_errno;
           }

           int
           main(int argc, char *argv[])
           {
               struct sigaction sa;
               struct timespec ts;
               int s;

               if (argc != 3) {
                   fprintf(stderr, "Usage: %s <alarm-secs> <wait-secs>\n",
                       argv[0]);
                   exit(EXIT_FAILURE);
               }

               if (sem_init(&sem, 0, 0) == -1) {
                   perror("sem_init");
                   exit(EXIT_FAILURE);
               }

               /* Establish SIGALRM handler; set alarm timer using argv[1] */

               sa.sa_handler = handler;
               sigemptyset(&sa.sa_mask);
               sa.sa_flags = 0;
               if (sigaction(SIGALRM, &sa, NULL) == -1) {
                   perror("sigaction");
                   exit(EXIT_FAILURE);
               }

               alarm(atoi(argv[1]));

               /* Calculate relative interval as current time plus
                  number of seconds given argv[2] */

               if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
                   perror("clock_gettime");
                   exit(EXIT_FAILURE);
               }
               ts.tv_sec += atoi(argv[2]);

               printf("main() about to call sem_timedwait()\n");
               while ((s = sem_timedwait(&sem, &ts)) == -1 && errno == EINTR)
                   continue;       /* Restart if interrupted by handler */

               /* Check what happened */

               if (s == -1) {
                   if (errno == ETIMEDOUT)
                       printf("sem_timedwait() timed out\n");
                   else
                       perror("sem_timedwait");
               } else
                   printf("sem_timedwait() succeeded\n");

               exit((s == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
           }

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       Applications using these functions may be subject to priority
       inversion, as discussed in the Base Definitions volume of POSIX.1-2008,
       _S_e_c_t_i_o_n _3_._2_8_7, _P_r_i_o_r_i_t_y _I_n_v_e_r_s_i_o_n.

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _s_e_m___p_o_s_t(), _s_e_m___t_r_y_w_a_i_t(), _s_e_m_c_t_l(), _s_e_m_g_e_t(), _s_e_m_o_p(), _t_i_m_e()

       The Base Definitions volume of POSIX.1-2008, _S_e_c_t_i_o_n _3_._2_8_7, _P_r_i_o_r_i_t_y
       _I_n_v_e_r_s_i_o_n, <<sseemmaapphhoorree..hh>>, <<ttiimmee..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                     SEM_TIMEDWAIT(3)
