linux 定时器

linux 定时器,第1张

linux 定时器 linux 定时器 1.定时器的概念

内核中通常需要周期性的执行一段函数,我们就需要定时器进行计时,溢出的时候开始执行

节拍率(HZ)

系统定时器以某种频率自行触发时钟中断,连续两个中断的间隔为一个节拍(tick),为节拍率分之一秒.

通常系统定时器节拍率(HZ)是通过静态自定义定义的

2.jiffies

全局变脸jiffies用来记录自系统启动以来产生的节拍总数,内核会给jiffies赋予一个初值,引起变量不断溢出

头文件

#include 
extern unsigned long volatile __cacheline_aligned_in_smp __jiffy_arch_data jiffies;
extern u64 __cacheline_aligned_in_smp jiffies_64;
//溢出通常使用相对时间 当前jiffies + 延时时间
unsigned long time1 = jiffies + 1;			//从现在的第一个节拍
unsigned long time2 = jiffies + HZ;			//从现在的第一秒
unsigned long time3 = jiffies + HZ/10;		//从现在的0.1s


unsigned long msecs_to_jiffies(const unsigned int m);
    
unsigned long usecs_to_jiffies(const unsigned int u);
       
unsigned int jiffies_to_msecs(const unsigned long j);
   
unsigned int jiffies_to_usecs(const unsigned long j);
u64 jiffies_to_nsecs(const unsigned long j)
3.定时器使用

头文件

#include 
struct timer_list {
	
	struct hlist_node	entry;			//定时器链表
	unsigned long		expires;		//以jiffies为单位的定时值
	void			(*function)(struct timer_list *);	//定时器处理函数
	u32			flags;					//

#ifdef CONFIG_LOCKDEP
	struct lockdep_map	lockdep_map;
#endif
};
1.初始化定时器
#define timer_setup(timer, callback, flags)			
	__init_timer((timer), (callback), (flags))
2.激活定时器
void add_timer(struct timer_list *timer)
{
	BUG_ON(timer_pending(timer));
	mod_timer(timer, timer->expires);
}
3.更改定时器时间
int mod_timer(struct timer_list *timer, unsigned long expires)
{
	return __mod_timer(timer, expires, 0);
}
4.停止定时器
int del_timer(struct timer_list *timer)
{
	struct timer_base *base;
	unsigned long flags;
	int ret = 0;

	debug_assert_init(timer);

	if (timer_pending(timer)) {
		base = lock_timer_base(timer, &flags);
		ret = detach_if_pending(timer, base, true);
		raw_spin_unlock_irqrestore(&base->lock, flags);
	}

	return ret;
}


int try_to_del_timer_sync(struct timer_list *timer)
{
	struct timer_base *base;
	unsigned long flags;
	int ret = -1;

	debug_assert_init(timer);

	base = lock_timer_base(timer, &flags);

	if (base->running_timer != timer)
		ret = detach_if_pending(timer, base, true);

	raw_spin_unlock_irqrestore(&base->lock, flags);

	return ret;
}
eg:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


struct timer_list timer_demo;

void timer_demo_fuc(struct timer_list *timer)
{

    printk("timer demo printkn");
    mod_timer(&timer_demo, jiffies + HZ);	//相对时间,通过对当前节拍值+溢出时间 设置新的时间

}
static int __init timer_demo_init(void)
{
    printk("timer demo initn");

    timer_setup(&timer_demo, timer_demo_fuc, 0);
    timer_demo.expires = jiffies + HZ;
    add_timer(&timer_demo);

    return 0;
}

static void __exit timer_demo_exit(void)
{
	printk("timer demo exitn");
}

module_init(timer_demo_init);
module_exit(timer_demo_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("L");
MODULE_ALIAS("hello");
4.延迟执行

短延时

头文件

#include 

void ndelay(unsigned long x)
void msleep(unsigned int msecs);
void ssleep(unsigned int seconds)
void usleep_range(unsigned long min, unsigned long max);

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5699047.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存