我正在爲內核編寫一個模塊。我需要等待一段時間(例如20秒)來測試一些東西。過程應該在20秒後繼續。在我的宏module_init功能我用定時器這樣的:Linux內核add_timer
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
「定時」是這樣的
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(&定時器);在他們之間,但它不起作用。我在終端輸入「dmesg」,消息立即連續寫入。
我希望我解釋清楚我的問題:)任何人都可以幫助我嗎?