something about jiffies and delay!

 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));

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值