1. 首页 > 科技

如何在linux系统中自己新建一个内核定时器,用struct timer_list这样子?

如何在linux系统中自己新建一个内核定时器,用struct timer_list这样子?

有谁会用linux里面的定时器timer_list

大家来学习学习,“jiffies + HZ”这个时间是怎么计算,还不是非常理解。我的整体理解是,驱动被加载一秒内,运行myfunc函数,myfunc函数打印一个“Hello,world!"后,每两秒再打印一个"Hello,world!"。驱动函数不需要你自己写main函数,你配置成y会自动被拉起,配置成m,需要手动通过工具拉起。

如何在Linux下实现定时器

可以使用 crontab 命令

求linux下用c语言编写的定时器程序

定时器就相当于系统每隔一段时间给进程发一个定时信号。在linux里面有一种进程中信息传递的方法,那就是信号,定时器程序就是需要定义一个信号处理函数。利用定时器产生的一个信号SIGALRM信号,在收到该信号同时会调用do_alarm函数。这也就类似单片机的定时中断,当定时到了,就跳到中断服务函数里去执行相应的程序。 Linux操作系统为每一个进程提供了3个内部间隔计时器. ITIMER_REAL:减少实际时间.到时的时候发出SIGALRM信号. ITIMER_VIRTUAL:减少有效时间(进程执行的时间).产生SIGVTALRM信号. ITIMER_PROF:减少进程的有效时间和系统时间(为进程调度用的时间).这个经常和上面一个使用用来计算系统内核时间和用户时间.产生SIGPROF信号. 代码如下:

#include   

#include

#include

#include

#include

static int count = 0;

static struct itimerval oldtv;

void set_timer()

{

struct itimerval itv;

itv.it_interval.tv_sec = 1;

itv.it_interval.tv_usec = 0;

itv.it_value.tv_sec = 1;

itv.it_value.tv_usec = 0;

setitimer(ITIMER_REAL, itv, oldtv);

}

void signal_handler(int m)

{

count ++;

printf("%d\n", count);

}

int main()

{

signal(SIGALRM, signal_handler);

set_timer();

while(count < 10000);

exit(0);

return 1;

}

在linux环境中,如何实现多线程中使用多个定时器,POSIX定时器可以吗,如何用?

个人解决了,以下是一个实现:

#include

#include

#include

#include

#include

#include

#include

#if 1

pthread_attr_t attr;

timer_t hard_timer, software_timer;

struct sigevent hard_evp, software_evp;

static void watchdog_hard_timeout(union sigval v)

{

time_t t;

char p[32];

timer_t *q;

struct itimerspec ts;

int ret;

time(&t);

strftime(p, sizeof(p), "%T", localtime(&t));

printf("watchdog hard timeout!\n");

printf("%s thread %d, val = %u, signal captured.\n", p, (unsigned int)pthread_self(), v.sival_int);

q = (timer_t *)(v.sival_ptr);

printf("hard timer_t:%d add:%p, q:%p!\n", (int)hard_timer, &hard_timer, q);

ts.it_interval.tv_sec = 0;

ts.it_interval.tv_nsec = 0;

ts.it_value.tv_sec = 6;

ts.it_value.tv_nsec = 0;

ret = timer_settime(*q, CLOCK_REALTIME, &ts, NULL);

if (ret != 0) {

printf("settime err(%d)!\n", ret);

}

}

static void watchdog_software_timeout(union sigval v)

{

time_t t;

char p[32];

timer_t *q;

struct itimerspec ts;

int ret;

time(&t);

strftime(p, sizeof(p), "%T", localtime(&t));

printf("watchdog software timeout!\n");

printf("%s thread %d, val = %u, signal captured.\n", p, (unsigned int)pthread_self(), v.sival_int);

q = (timer_t *)(v.sival_ptr);

printf("hard timer_t:%d add:%p, q:%p!\n", (int)hard_timer, &hard_timer, q);

ts.it_interval.tv_sec = 0;

ts.it_interval.tv_nsec = 0;

ts.it_value.tv_sec = 10;

ts.it_value.tv_nsec = 0;

ret = timer_settime(*q, CLOCK_REALTIME, &ts, NULL);

if (ret != 0) {

printf("settime err(%d)!\n", ret);

}

}

static void dcmi_sol_pthread_attr_destroy(pthread_attr_t *attr)

{

pthread_attr_destroy(attr);

}

static int dcmi_sol_pthread_attr_init(pthread_attr_t *attr)

{

int ret;

if ((ret = pthread_attr_init(attr) != 0)) {

goto err;

}

if ((ret = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED)) != 0) {

dcmi_sol_pthread_attr_destroy(attr);

goto err;

}

/* 设置线程的栈大小,失败则用系统默认值 */

pthread_attr_setstacksize(attr, 128 * 1024);

return 0;

err:

printf("set ptread attr failed(ret:%d)!\n", ret);

return -1;

}

int main(void)

{

struct itimerspec ts;

int ret;

ret = dcmi_sol_pthread_attr_init(&attr);

if (ret != 0) {

printf("init pthread attributes fail(%d)!\n", ret);

exit(-1);

}

memset(&hard_evp, 0, sizeof(struct sigevent));

hard_evp.sigev_value.sival_ptr = &hard_timer;

hard_evp.sigev_notify = SIGEV_THREAD;

hard_evp.sigev_notify_function = watchdog_hard_timeout;

hard_evp.sigev_notify_attributes = NULL;//&attr;

memset(&software_evp, 0, sizeof(struct sigevent));

software_evp.sigev_value.sival_ptr = &software_timer;

software_evp.sigev_notify = SIGEV_THREAD;

software_evp.sigev_notify_function = watchdog_software_timeout;

software_evp.sigev_notify_attributes = NULL;//&attr;

ret = timer_create(CLOCK_REALTIME, &hard_evp, &hard_timer);

if(ret != 0) {

perror("hard timer_create fail!");

exit(-1);

}

ret = timer_create(CLOCK_REALTIME, &software_evp, &software_timer);

if (ret != 0) {

timer_delete(hard_timer);

perror("software timer_create fail!");

exit(-1);

}

ts.it_interval.tv_sec = 0;

ts.it_interval.tv_nsec = 0;

ts.it_value.tv_sec = 6;

ts.it_value.tv_nsec = 0;

ret = timer_settime(hard_timer, CLOCK_REALTIME, &ts, NULL);

if(ret != 0) {

perror("hard timer_settime fail!");

timer_delete(hard_timer);

timer_delete(software_timer);

exit(-1);

}

ts.it_value.tv_sec = 10;

ret = timer_settime(software_timer, CLOCK_REALTIME, &ts, NULL);

if(ret != 0) {

perror("hard timer_settime fail!");

timer_delete(hard_timer);

timer_delete(software_timer);

exit(-1);

}

while(1) {

printf("main ready sleep!\n");

sleep(15);

printf("main sleep finish!\n");

}

return 0;

}

#endif