Back to index

glibc  2.9
tst-timer.c
Go to the documentation of this file.
00001 /* Tests for POSIX timer implementation.
00002    Copyright (C) 2000, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public License as
00008    published by the Free Software Foundation; either version 2.1 of the
00009    License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00018    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.  */
00020 
00021 #include <errno.h>
00022 #include <signal.h>
00023 #include <stdio.h>
00024 #include <time.h>
00025 #include <unistd.h>
00026 
00027 
00028 static void
00029 notify_func (union sigval sigval)
00030 {
00031   puts ("notify_func");
00032 }
00033 
00034 
00035 static void
00036 signal_func (int sig)
00037 {
00038   static const char text[] = "signal_func\n";
00039   signal (sig, signal_func);
00040   write (STDOUT_FILENO, text, sizeof text - 1);
00041 }
00042 
00043 static void
00044 intr_sleep (int sec)
00045 {
00046   struct timespec ts;
00047 
00048   ts.tv_sec = sec;
00049   ts.tv_nsec = 0;
00050 
00051   while (nanosleep (&ts, &ts) == -1 && errno == EINTR)
00052     ;
00053 }
00054 
00055 #define ZSIGALRM 14
00056 
00057 
00058 int
00059 main (void)
00060 {
00061   struct timespec ts;
00062   timer_t timer_sig, timer_thr1, timer_thr2;
00063   int retval;
00064   struct sigevent sigev1 =
00065   {
00066     .sigev_notify = SIGEV_SIGNAL,
00067     .sigev_signo = ZSIGALRM
00068   };
00069   struct sigevent sigev2;
00070   struct itimerspec itimer1 = { { 0, 200000000 }, { 0, 200000000 } };
00071   struct itimerspec itimer2 = { { 0, 100000000 }, { 0, 500000000 } };
00072   struct itimerspec itimer3 = { { 0, 150000000 }, { 0, 300000000 } };
00073   struct itimerspec old;
00074 
00075   retval = clock_gettime (CLOCK_REALTIME, &ts);
00076 
00077   sigev2.sigev_notify = SIGEV_THREAD;
00078   sigev2.sigev_notify_function = notify_func;
00079   sigev2.sigev_notify_attributes = NULL;
00080 
00081   setvbuf (stdout, 0, _IOLBF, 0);
00082 
00083   printf ("clock_gettime returned %d, timespec = { %ld, %ld }\n",
00084          retval, ts.tv_sec, ts.tv_nsec);
00085 
00086   retval = clock_getres (CLOCK_REALTIME, &ts);
00087 
00088   printf ("clock_getres returned %d, timespec = { %ld, %ld }\n",
00089          retval, ts.tv_sec, ts.tv_nsec);
00090 
00091   timer_create (CLOCK_REALTIME, &sigev1, &timer_sig);
00092   timer_create (CLOCK_REALTIME, &sigev2, &timer_thr1);
00093   timer_create (CLOCK_REALTIME, &sigev2, &timer_thr2);
00094 
00095   timer_settime (timer_thr1, 0, &itimer2, &old);
00096   timer_settime (timer_thr2, 0, &itimer3, &old);
00097 
00098   signal (ZSIGALRM, signal_func);
00099 
00100   timer_settime (timer_sig, 0, &itimer1, &old);
00101 
00102   timer_delete (-1);
00103 
00104   intr_sleep (3);
00105 
00106   timer_delete (timer_sig);
00107   timer_delete (timer_thr1);
00108 
00109   intr_sleep (3);
00110 
00111   timer_delete (timer_thr2);
00112 
00113   return 0;
00114 }