tst-timer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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
  15. not, see <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. static void
  22. notify_func (union sigval sigval)
  23. {
  24. puts ("notify_func");
  25. }
  26. static void
  27. signal_func (int sig)
  28. {
  29. static const char text[] = "signal_func\n";
  30. signal (sig, signal_func);
  31. write (STDOUT_FILENO, text, sizeof text - 1);
  32. }
  33. static void
  34. intr_sleep (int sec)
  35. {
  36. struct timespec ts;
  37. ts.tv_sec = sec;
  38. ts.tv_nsec = 0;
  39. while (nanosleep (&ts, &ts) == -1 && errno == EINTR)
  40. ;
  41. }
  42. #define ZSIGALRM 14
  43. int
  44. main (void)
  45. {
  46. struct timespec ts;
  47. timer_t timer_sig, timer_thr1, timer_thr2;
  48. int retval;
  49. struct sigevent sigev1 =
  50. {
  51. .sigev_notify = SIGEV_SIGNAL,
  52. .sigev_signo = ZSIGALRM
  53. };
  54. struct sigevent sigev2;
  55. struct itimerspec itimer1 = { { 0, 200000000 }, { 0, 200000000 } };
  56. struct itimerspec itimer2 = { { 0, 100000000 }, { 0, 500000000 } };
  57. struct itimerspec itimer3 = { { 0, 150000000 }, { 0, 300000000 } };
  58. struct itimerspec old;
  59. retval = clock_gettime (CLOCK_REALTIME, &ts);
  60. sigev2.sigev_notify = SIGEV_THREAD;
  61. sigev2.sigev_notify_function = notify_func;
  62. sigev2.sigev_notify_attributes = NULL;
  63. setvbuf (stdout, 0, _IOLBF, 0);
  64. printf ("clock_gettime returned %d, timespec = { %ld, %ld }\n",
  65. retval, ts.tv_sec, ts.tv_nsec);
  66. retval = clock_getres (CLOCK_REALTIME, &ts);
  67. printf ("clock_getres returned %d, timespec = { %ld, %ld }\n",
  68. retval, ts.tv_sec, ts.tv_nsec);
  69. timer_create (CLOCK_REALTIME, &sigev1, &timer_sig);
  70. timer_create (CLOCK_REALTIME, &sigev2, &timer_thr1);
  71. timer_create (CLOCK_REALTIME, &sigev2, &timer_thr2);
  72. timer_settime (timer_thr1, 0, &itimer2, &old);
  73. timer_settime (timer_thr2, 0, &itimer3, &old);
  74. signal (ZSIGALRM, signal_func);
  75. timer_settime (timer_sig, 0, &itimer1, &old);
  76. timer_delete (-1);
  77. intr_sleep (3);
  78. timer_delete (timer_sig);
  79. timer_delete (timer_thr1);
  80. intr_sleep (3);
  81. timer_delete (timer_thr2);
  82. return 0;
  83. }