【问题标题】:Linux kernel add_timerLinux 内核 add_timer
【发布时间】:2013-03-13 21:02:58
【问题描述】:

我正在为内核编写一个模块。我需要等待一段时间(例如;20 秒)只是测试一些东西。该过程应在 20 秒后继续。在我的 module_init 函数中,我使用了这样的计时器:

init_timer(&timer);
timer.expires = jiffies + HZ*20;//timer expires in delay ticks
timer.data = 0;//zero is passed to the timer handler
timer.function = timer_handle;//function to run when timer expires  

"timer" 是这样定义的结构

static struct timer_list timer;

timer_handle 是我在计时器到期时运行的函数:

void timer_handle (unsigned long data){


 }

现在在我的功能中:

ssize_t write(struct file *filp, const char *buff, size_t count, loff_t *offp) { 
    unsigned long ret;


    printk(KERN_INFO "pid : .%d  \n", task->pid);
    down_write(&rwsem);
    if ( (filp->f_flags & O_ACCMODE) == O_RDONLY)
    {
        printk(KERN_INFO "The reader thread is not able to write. \n");
        return -EINVAL;
    }
    printk(KERN_INFO "Inside write 1 \n");
    add_timer(&timer);
    ret = copy_from_user(bufferr, buff, count);
    up_write(&rwsem);
    printk("Inside write 2 \n");
    return count;
}

"printk(KERN_INFO"里面写\n");"之后我想让进程等待 20 秒。消息“Inside write 2 \n”必须在 20 秒后写入消息“Inside write 1 \n”。我使用了 add_timer(&timer);他们之间,但它不起作用。我在终端中输入“dmesg”,消息立即连续写入。

我希望我清楚地解释了我的问题 :) 有人可以帮助我吗?

【问题讨论】:

    标签: kernel time.h


    【解决方案1】:

    两条消息被立即连续写入,因为它们是立即连续。您正在编写计时器而不是延迟,因此 20 秒后内核运行 timer_handle() 函数,在您的情况下该函数为空。

    如果您需要延迟某些代码执行,请阅读Linux Device Driver 3th Chapter 7 section 3

    【讨论】:

      【解决方案2】:

      你打印错了,因为它是用户触发读取时的写入回调。

      你想什么时候注册定时器?我不明白你为什么要写。

      该计时器应该定期计时还是根据某些事件触发来计时?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-27
        • 2018-11-14
        • 1970-01-01
        • 1970-01-01
        • 2017-04-08
        • 2013-10-20
        • 1970-01-01
        相关资源
        最近更新 更多