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, 2003 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_func1 (union sigval sigval)
00030 {
00031   puts ("notify_func1");
00032 }
00033 
00034 
00035 static void
00036 notify_func2 (union sigval sigval)
00037 {
00038   puts ("notify_func2");
00039 }
00040 
00041 
00042 static void
00043 signal_func (int sig)
00044 {
00045   static const char text[] = "signal_func\n";
00046   signal (sig, signal_func);
00047   write (STDOUT_FILENO, text, sizeof text - 1);
00048 }
00049 
00050 static void
00051 intr_sleep (int sec)
00052 {
00053   struct timespec ts;
00054 
00055   ts.tv_sec = sec;
00056   ts.tv_nsec = 0;
00057 
00058   while (nanosleep (&ts, &ts) == -1 && errno == EINTR)
00059     ;
00060 }
00061 
00062 #define ZSIGALRM 14
00063 
00064 
00065 int
00066 main (void)
00067 {
00068   struct timespec ts;
00069   timer_t timer_sig, timer_thr1, timer_thr2;
00070   int retval;
00071   struct sigevent sigev1 =
00072   {
00073     .sigev_notify = SIGEV_SIGNAL,
00074     .sigev_signo = ZSIGALRM
00075   };
00076   struct sigevent sigev2;
00077   struct itimerspec itimer1 = { { 0, 200000000 }, { 0, 200000000 } };
00078   struct itimerspec itimer2 = { { 0, 100000000 }, { 0, 500000000 } };
00079   struct itimerspec itimer3 = { { 0, 150000000 }, { 0, 300000000 } };
00080   struct itimerspec old;
00081 
00082   retval = clock_gettime (CLOCK_REALTIME, &ts);
00083 
00084   sigev2.sigev_notify = SIGEV_THREAD;
00085   sigev2.sigev_notify_function = notify_func1;
00086   sigev2.sigev_notify_attributes = NULL;
00087   /* It is unnecessary to do the following but to set a good example
00088      we do it anyhow.  */
00089   sigev2.sigev_value.sival_ptr = NULL;
00090 
00091   setvbuf (stdout, 0, _IOLBF, 0);
00092 
00093   printf ("clock_gettime returned %d, timespec = { %ld, %ld }\n",
00094          retval, ts.tv_sec, ts.tv_nsec);
00095 
00096   retval = clock_getres (CLOCK_REALTIME, &ts);
00097 
00098   printf ("clock_getres returned %d, timespec = { %ld, %ld }\n",
00099          retval, ts.tv_sec, ts.tv_nsec);
00100 
00101   if (timer_create (CLOCK_REALTIME, &sigev1, &timer_sig) != 0)
00102     {
00103       printf ("timer_create for timer_sig failed: %m\n");
00104       exit (1);
00105     }
00106   if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr1) != 0)
00107     {
00108       printf ("timer_create for timer_thr1 failed: %m\n");
00109       exit (1);
00110     }
00111   sigev2.sigev_notify_function = notify_func2;
00112   if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr2) != 0)
00113     {
00114       printf ("timer_create for timer_thr2 failed: %m\n");
00115       exit (1);
00116     }
00117 
00118   if (timer_settime (timer_thr1, 0, &itimer2, &old) != 0)
00119     {
00120       printf ("timer_settime for timer_thr1 failed: %m\n");
00121       exit (1);
00122     }
00123   if (timer_settime (timer_thr2, 0, &itimer3, &old) != 0)
00124     {
00125       printf ("timer_settime for timer_thr2 failed: %m\n");
00126       exit (1);
00127     }
00128 
00129   signal (ZSIGALRM, signal_func);
00130 
00131   if (timer_settime (timer_sig, 0, &itimer1, &old) != 0)
00132     {
00133       printf ("timer_settime for timer_sig failed: %m\n");
00134       exit (1);
00135     }
00136 
00137   intr_sleep (3);
00138 
00139   if (timer_delete (timer_sig) != 0)
00140     {
00141       printf ("timer_delete for timer_sig failed: %m\n");
00142       exit (1);
00143     }
00144   if (timer_delete (timer_thr1) != 0)
00145     {
00146       printf ("timer_delete for timer_thr1 failed: %m\n");
00147       exit (1);
00148     }
00149 
00150   intr_sleep (3);
00151 
00152   if (timer_delete (timer_thr2) != 0)
00153     {
00154       printf ("timer_delete for timer_thr2 failed: %m\n");
00155       exit (1);
00156     }
00157 
00158   return 0;
00159 }