Reputation: 309
I am writing a module for kernel. I need to wait a process for a while (for example; 20 seconds) just test something. Process should go on after 20 seconds. In my module_init function I used timer like that :
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" is a struct that defined like that
static struct timer_list timer;
timer_handle is my function to run when timer expires:
void timer_handle (unsigned long data){
}
Now in my function :
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;
}
After "printk(KERN_INFO "Inside write \n");" I want to keep process 20 seconds in waiting. The message "Inside write 2 \n" must written after 20 second the message "Inside write 1 \n" written. I used add_timer(&timer); between them but it does not work. I enter "dmesg" in terminal and the messages are written consecutively immediately.
I hope I explained my problem clearly :) Could anyone help me?
Upvotes: 1
Views: 2239
Reputation: 449
you are printing it wrong, as since its a write callback when user triggered a read.
when do you want to register the timer? I couldn't get it why you do in the write.
is that timer should timer periodically or on basis of some event trigger?
Upvotes: 1
Reputation: 3892
Two messages are written consecutively immediately beacuse they are consecutively immediately. You are programming a timer not a delay, so after 20 seconds kernel run the timer_handle() function, which is empty in your case.
If you need to delay some code execution read Linux Device Driver 3th Chapter 7 section 3
Upvotes: 2