tst-timer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Tests for POSIX timer implementation.
  2. Copyright (C) 2000, 2002 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License as
  7. published by the Free Software Foundation; either version 2.1 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #include <errno.h>
  18. #include <signal.h>
  19. #include <stdio.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. static void
  23. notify_func (union sigval sigval)
  24. {
  25. puts ("notify_func");
  26. }
  27. static void
  28. signal_func (int sig)
  29. {
  30. static const char text[] = "signal_func\n";
  31. signal (sig, signal_func);
  32. write (STDOUT_FILENO, text, sizeof text - 1);
  33. }
  34. static void
  35. intr_sleep (int sec)
  36. {
  37. struct timespec ts;
  38. ts.tv_sec = sec;
  39. ts.tv_nsec = 0;
  40. while (nanosleep (&ts, &ts) == -1 && errno == EINTR)
  41. ;
  42. }
  43. #define ZSIGALRM 14
  44. int
  45. main (void)
  46. {
  47. struct timespec ts;
  48. timer_t timer_sig, timer_thr1, timer_thr2;
  49. int retval;
  50. struct sigevent sigev1 =
  51. {
  52. .sigev_notify = SIGEV_SIGNAL,
  53. .sigev_signo = ZSIGALRM
  54. };
  55. struct sigevent sigev2;
  56. struct itimerspec itimer1 = { { 0, 200000000 }, { 0, 200000000 } };
  57. struct itimerspec itimer2 = { { 0, 100000000 }, { 0, 500000000 } };
  58. struct itimerspec itimer3 = { { 0, 150000000 }, { 0, 300000000 } };
  59. struct itimerspec old;
  60. retval = clock_gettime (CLOCK_REALTIME, &ts);
  61. sigev2.sigev_notify = SIGEV_THREAD;
  62. sigev2.sigev_notify_function = notify_func;
  63. sigev2.sigev_notify_attributes = NULL;
  64. setvbuf (stdout, 0, _IOLBF, 0);
  65. printf ("clock_gettime returned %d, timespec = { %ld, %ld }\n",
  66. retval, ts.tv_sec, ts.tv_nsec);
  67. retval = clock_getres (CLOCK_REALTIME, &ts);
  68. printf ("clock_getres returned %d, timespec = { %ld, %ld }\n",
  69. retval, ts.tv_sec, ts.tv_nsec);
  70. timer_create (CLOCK_REALTIME, &sigev1, &timer_sig);
  71. timer_create (CLOCK_REALTIME, &sigev2, &timer_thr1);
  72. timer_create (CLOCK_REALTIME, &sigev2, &timer_thr2);
  73. timer_settime (timer_thr1, 0, &itimer2, &old);
  74. timer_settime (timer_thr2, 0, &itimer3, &old);
  75. signal (ZSIGALRM, signal_func);
  76. timer_settime (timer_sig, 0, &itimer1, &old);
  77. timer_delete (-1);
  78. intr_sleep (3);
  79. timer_delete (timer_sig);
  80. timer_delete (timer_thr1);
  81. intr_sleep (3);
  82. timer_delete (timer_thr2);
  83. return 0;
  84. }