您的位置:首页 > 运维架构 > Linux

Linux内核时间管理相关的接口

2016-03-28 10:59 411 查看
在内核经常会碰到一些要定时调度的任务,可以用timer处理,下面来看看都有哪些接口,

/*******************************************************************/

linux/param.h

HZ;//宏,每秒定时器中断的次数,也即是系统每秒节拍或称滴答数

linux/sched.h;包含了linux/jiffies.h

jiffies_64;//自系统启动的滴答数

jiffies;//上面的低有效位...............它们明显是只读的

linux/jiffies.h;...............包含一些宏用来比较缓存的值和当前值

int time_after(unsignde long a,unsigned long b);

int time_before(unsignde long a,unsigned long b);

int time_after_eq(unsignde long a,unsigned long b);

int time_before_eq(unsignde long a,unsigned long b);

linux/time.h;..................jiffies到struct timeval,struct timespec转换

unsigned long timespec_to_jiffies(struct timespec*value);

void jiffies_to_timespec(unsigned long jiffies,struct timespec* value);

unsigned long timeval_to_jiffies(struct timeval* value);

void jiffies_to_timeval(unsigned long jiffies,struct timeval* value);

linux/jiffies.h

u64 get_jiffies_64(void);//读取jiffies_64,而且完成了加锁

linux/time.h

void do_gettimeofday(struct timeval *tv);  /*获取墙上

struct timespec current_kernel_time(void); /*时间

unsigned long mktime(unsigned int year,unsigned int mon,unsigned int day,

unsigned int hour,unsigned int min,unsigned int sec);/*转换墙上时间到一个

           jiffies值*/

while(time_before(jiffies,jl))
cpu_relax();    //一种延时方式,忙等待

while(time_before(jiffies,jl))
schedule();   //另一种延时方式,让出cpu

linux/sched.h

signed long schedule_timeout(signed long num_jiffies)/*睡眠一定时间*/

set_current_state(TASK_INTERRUPTIBLE/TASK_UNINTERRUPTIBLE)/*一般先设置状态

linux/delay.h  //短的延时,忙等待的实现,实参有上限

void ndelay(unsigned long nsecs);

void udelay(unsigned long usecs);

void mdelay(unsigned long msecs);

void msleep(unsigned int msecs);//不可打断的睡眠

unsigned long msleep_interruptbile(unsigned int msecs);//可打断的睡眠

void ssleep(unsigned int secs);//不可打断的睡眠

linux/timer.h //内核定时器,异步运行,处于“软件中断”环境,确保原子性,一

     //个任务可以注册它本身在一定时间后运行,共享资源的竞争性,同一CPU运行

struct timer_list {
/*....*/
unsigned long expires;//jiffies值衡量,多长时间后运行
void (*function)(unsigned long);
unsigned long data;//传递给function的参数

};

void init_timer(struct timer_list *timer);

struct timer_list TIMER_INITIALIZER(_function,_expires,_data);

void add_timer(struct timer_list *timer);

int del_timer(struct timer_list *timer);

int mod_timer(struct timer_list *timer,unsigned long expires);//更新一个定

                                             //时器,无论add_timer之前之后

int del_timer_sync(struct timer_list *timer);//返回时确保定时器函数不会在 

            //任一个CPU上运行,非原子上下文可能会睡眠,其他的会忙等待

int timer_pending(const struct timer_list *timer);//返回真假指示定时器任务

                                                  //当前是否在调度运行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: