Realization and application of real time timer in C language under Linux

Source: Internet
Author: User
Tags exit linux

Linux also has a high-precision timer, that is posix_timer. I remember looking at the code when the Clock_realtime timer seems to use the RDTSC instructions, but now not sure, first put aside. In principle, you can use the RDTSC instruction in the frequency conversion, because the frequency of the CPU is also known to us, the kernel is also known when the frequency conversion.

Here is my timer_create example, compile with RT library, this is the Linux Realtime Library:

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;
}

Output results:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.