Linux下即時定時器在C語言中的實現和應用

來源:互聯網
上載者:User

linux下還有一種高精度的定時器,那就是posix_timer.我記得以前看代碼的時候CLOCK_REALTIME的定時器似乎用的就是rdtsc指令,不過現在不確定了,先放到一邊。原理上來說,可以在變頻的時候也使用rdtsc指令,因為CPU的頻率我們也是知道的,變頻的時候核心也是知道的。

下面是我的timer_create的例子,編譯的時候要加上rt庫,這是linux的realtime庫:

gcc -o test test.c

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>

#define rdtsc(low,high) __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))

timer_t tt;

void handler (int sig, siginfo_t * extra, void *cruft)
{
  static last_i=0;
  unsigned int i, j;
  rdtsc(i,j);
  printf ("time:%u, %u, [%u] %uHZ ", j, i, i-last_i, (i-last_i)*10/1000000);
  last_i = i;
}

int main ()
{
  int i=0;
  sigset_t sigset;

  sigfillset (&sigset);
  sigdelset (&sigset, SIGRTMIN);
  sigprocmask (SIG_SETMASK, &sigset, NULL);

  struct sigaction sa;
  sigfillset (&sa.sa_mask);
  sa.sa_flags = SA_SIGINFO;
  sa.sa_sigaction = handler;

  if (sigaction (SIGRTMIN, &sa, NULL) < 0)
  {
    perror ("sigaction failed ");
    exit (-1);
  }

  struct sigevent timer_event;
  struct itimerspec timer;

  timer.it_interval.tv_sec = 0;
  timer.it_interval.tv_nsec = 100 * 1000 * 1000;
  timer.it_value = timer.it_interval;

  timer_event.sigev_notify = SIGEV_SIGNAL;
  timer_event.sigev_signo = SIGRTMIN;
  timer_event.sigev_value.sival_ptr = (void *) &tt;

  if (timer_create (CLOCK_REALTIME, &timer_event, &tt) < 0)
  {
    perror ("timer_create failed");
    exit (-1);
  }

  if (timer_settime (tt, 0, &timer, NULL) < 0)
  {
    perror ("timer_settime failed");
    exit (-1);
  }

  while (i++ < 10)
  {
    pause ();
  }

  return 0;
}

輸出結果:

time:166081, 1934350847, [1934350847] 2163HZ

time:166081, 2120528291, [186177444] 1861HZ

time:166081, 2306679576, [186151285] 1861HZ

time:166081, 2494695630, [188016054] 1880HZ

time:166081, 2680865389, [186169759] 1861HZ

time:166081, 2867018473, [186153084] 1861HZ

time:166081, 3053152230, [186133757] 1861HZ

time:166081, 3239309935, [186157705] 1861HZ

time:166081, 3425467261, [186157326] 1861HZ

time:166081, 3611639266, [186172005] 1861HZ

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.