Jiffies(我感觉它是个全局变量)
The global variable jiffies holds the number of ticks that have occurred since the system
booted. On boot, the kernel initializes the variable to zero, and it is incremented by one
during each timer interrupt.
一次时钟中断(timer interrupt)就产生一个ticks,jiffies 记录了从开机起的ticks数!
HZ代表这一秒内的jiffies数!!!(我的理解:这不就是对应着时钟频率吗?) 因此:
extern unsigned long volatile jiffies;
unsigned long time_stamp = jiffies; /* now */
unsigned long next_tick = jiffies +_1; /* one tick from now */
unsigned long later = jiffies + 5*HZ; /* five seconds from now */
The following code converts from seconds to a unit of jiffies:
(seconds * HZ)
Likewise, this code converts from jiffies to seconds:
(jiffies / HZ)
定义了一些“函数”
#define time_after(unknown, known) ((long)(known) - (long)(unknown) < 0)
#define time_before(unknown, known) ((long)(unknown) - (long)(known) < 0)
#define time_after_eq(unknown, known) ((long)(unknown) - (long)(known) >= 0)
#define time_before_eq(unknown, known) ((long)(known) - (long)(unknown) >= 0)
使用例子:
unsigned long timeout = jiffies + HZ/2; /* timeout in 0.5s */
/* ... */
if (time_before(jiffies, timeout)) {
/* we did not time out, good ... */
} else {
/* we timed out, error ... */
}
//用这个函数延时,属于长延时了!!
//短延时可以用 :
defined in <linux/delay.h>, which do not use jiffies:
void udelay(unsigned long usecs)//微妙
void mdelay(unsigned long msecs)//毫秒
//还可以用睡着延时函数!因为进程会被重新调度,所以此函数不能用在中断上下文或之前持有锁的地
方!
signed long schedule_timeout(signed long timeout);
Usage is easy:
/* set task's state to interruptible sleep */
set_current_state(TASK_INTERRUPTIBLE);
/* take a nap and wake up in "s" seconds */
schedule_timeout(s * HZ);
//延时S秒
prototype is:
signed long schedule_timeout(signed long timeout)
{
timer_t timer;
unsigned long expire;
switch (timeout)
{
case MAX_SCHEDULE_TIMEOUT:
schedule();
goto out;
default:
if (timeout < 0)
{
printk(KERN_ERR "schedule_timeout: wrong timeout "
"value %lx from %p/n", timeout,
__builtin_return_address(0));
current->state = TASK_RUNNING;
goto out;
}
}
expire = timeout + jiffies;
init_timer(&timer);
timer.expires = expire;
timer.data = (unsigned long) current;
timer.function = process_timeout;
add_timer(&timer);
schedule();
del_timer_sync(&timer);
timeout = expire - jiffies;
out:
return timeout < 0 ? 0 : timeout;
}
其中调用的函数为:
void process_timeout(unsigned long data)
{
wake_up_process((task_t *) data);//其中timer.data = (unsigned long) current;
//即唤醒当前进程
}
还可以这样用哦:
unsigned long start;
unsigned long total_time;
start = jiffies;
/* do some work ... */
total_time = jiffies - start;
printk("That took %lu ticks/n", jiffies_to_clock_t(total_time));