Linux内核超时检测

printk("HZ is %d\n",HZ);
    
    timeout = jiffies + 5*HZ/100;
    printk(KERN_INFO"time_after test\r\n");
    while(1)
    {
        if(time_after(jiffies,timeout))
        {
            printk("timeout\n");
            break;
        }
        else
        {
            printk("not timeout %ld\n",jiffies);
        }
    }

    printk(KERN_INFO"time_before test\r\n");
    timeout = jiffies + 5*HZ/100;
    while(1)
    {
        if(time_before(jiffies,timeout))
        {
            printk("not timeout %ld\n",jiffies);
            
        }
        else
        {
            printk("timeout\n");
            break;
        }
    }

    printk(KERN_INFO"time_before_eq test\r\n");
    timeout = jiffies + 5*HZ/100;
    while(1)
    {
        if(time_before_eq(jiffies,timeout))
        {
            printk("not equal: timeout %ld,jiffies %ld\n",timeout,jiffies);
            
        }
        else
        {
            printk("equal\n");
            break;
        }
    }
    
    printk(KERN_INFO"time_after_eq test\r\n");
    timeout = jiffies + 5*HZ/100;
    while(1)
    {
        if(time_after_eq(jiffies,timeout))
        {
            printk("equal\n");
            break;
        }
        else
        {
            printk("not equal: timeout %ld,jiffies %ld\n",timeout,jiffies);
        }
    }
View Code

 

time_after(a,b)    a>b:返回true

time_before(a,b)    a<b:返回true

time_after_eq(a,b)   a>=b:返回true

time_before_eq(a,b)  a<=b :返回true

可以解决回绕,不要自己去瞎搞了。。。。。

posted @ 2019-03-19 11:12  一枚小苦工  阅读(663)  评论(0编辑  收藏  举报