fork download
  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/time.h>
  6.  
  7.  
  8. #define USECREQ 250
  9. #define LOOPS 1000
  10.  
  11. void event_handler (int signum)
  12. {
  13. static unsigned long cnt = 0;
  14. static struct timeval tsFirst;
  15. if (cnt == 0) {
  16. gettimeofday (&tsFirst, 0);
  17. }
  18. cnt ++;
  19. if (cnt >= LOOPS) {
  20. struct timeval tsNow;
  21. struct timeval diff;
  22. setitimer (ITIMER_REAL, NULL, NULL);
  23. gettimeofday (&tsNow, 0);
  24. timersub(&tsNow, &tsFirst, &diff);
  25. unsigned long long udiff = (diff.tv_sec * 1000000) + diff.tv_usec;
  26. double delta = (double)(udiff/cnt)/1000000;
  27. int hz = (unsigned)(1.0/delta);
  28. printf ("kernel timer interrupt frequency is approx. %d Hz", hz);
  29. if (hz >= (int) (1.0/((double)(USECREQ)/1000000))) {
  30. printf (" or higher");
  31. }
  32. printf ("\n");
  33. exit (0);
  34. }
  35. }
  36.  
  37. int main (int argc, char **argv)
  38. {
  39. struct sigaction sa;
  40. struct itimerval timer;
  41.  
  42. memset (&sa, 0, sizeof (sa));
  43. sa.sa_handler = &event_handler;
  44. sigaction (SIGALRM, &sa, NULL);
  45. timer.it_value.tv_sec = 0;
  46. timer.it_value.tv_usec = USECREQ;
  47. timer.it_interval.tv_sec = 0;
  48. timer.it_interval.tv_usec = USECREQ;
  49. setitimer (ITIMER_REAL, &timer, NULL);
  50. while (1);
  51. }
Success #stdin #stdout 1s 2248KB
stdin
Standard input is empty
stdout
kernel timer interrupt frequency is approx. 1002 Hz