当前位置: 技术问答>linux和unix
linux如何实现精确定时器?
来源: 互联网 发布时间:2015-12-02
本文导语: 应用:按固定的速率发送数据。 我想要精确到ms,用select来实现的,效果好像不行。2.4内核。 | 试一下timer_create,例子如下: #include #include #include void handle (sigval_t v) { time_t t; char p[32]; ...
应用:按固定的速率发送数据。
我想要精确到ms,用select来实现的,效果好像不行。2.4内核。
我想要精确到ms,用select来实现的,效果好像不行。2.4内核。
|
试一下timer_create,例子如下:
#include
#include
#include
void
handle (sigval_t v)
{
time_t t;
char p[32];
time (&t);
strftime (p, sizeof (p), "%T", localtime (&t));
printf ("%s thread %ld, val = %d, signal captured.n", p, pthread_self (),
v.sival_int);
return;
}
int
create (int seconds, int id)
{
timer_t tid;
struct sigevent se;
struct itimerspec ts, ots;
memset (&se, 0, sizeof (se));
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_function = handle;
se.sigev_value.sival_int = id;
if (timer_create (CLOCK_REALTIME, &se, &tid)
#include
#include
#include
void
handle (sigval_t v)
{
time_t t;
char p[32];
time (&t);
strftime (p, sizeof (p), "%T", localtime (&t));
printf ("%s thread %ld, val = %d, signal captured.n", p, pthread_self (),
v.sival_int);
return;
}
int
create (int seconds, int id)
{
timer_t tid;
struct sigevent se;
struct itimerspec ts, ots;
memset (&se, 0, sizeof (se));
se.sigev_notify = SIGEV_THREAD;
se.sigev_notify_function = handle;
se.sigev_value.sival_int = id;
if (timer_create (CLOCK_REALTIME, &se, &tid)