Waiting In Kernel Space Getting a time value: #include jiffies - the number of clock ticks that have elapsed since system boot HZ - the number on jiffies per second on the current architecture Short Delays (maximum 1 millisecond): #include void udelay(unsigned long microseconds) -busy/waiting loop. Eats CPU while in delay. -eight bit resolution, bad drift on long (>100 microseconds) delays Obvious but Braindead Implementation of Longer Delays: unsigned long j = jiffies + delay_in_seconds * HZ; while(jiffies < j); -Can lock machine if interrups are diabled. -Still busy waiting loop, and eats CPU while in delay. Wait Queues: -A kernel structure that is used to put a process to sleep to be woken up at a leter time. #include void interruptible_sleep_on(struct wait_queue **q) void wake_up_interruptible(struct wait_queue **q) Above delay more gracefully handled by wait queue, CPU is put to work elsewhere until needed (timeout). struct wait_queue *wait=NULL; current->timeout=jiffies + delay_in_seconds * HZ; interruptible_sleep_on(&wait); -good but only works in the context of a process (current is a struct for current process) Kernel Timers: -ultimate time resource in kernel. -not bound to run in context of a process. #include void init_timer(struct timer_list *timer) void add_timer(struct timer_list *timer) int del_timer(struct timer_list *timer) Usage: struct timer_list our_timer; struct wait_queue *our_wq; void function_run_when_timeout(int data) { /* perform some action on timeout */ wake_up_interruptible(&our_wq); } int i_bus_write( .... ) { init_timer(&our_timer); our_timer.function = function_run_when_timeout; our_timer.data = some_int_data; our_timer.expires = jiffies + delay_in_seconds * HZ; add_timer(&our_timer); interruptible_sleep_on(&our_wq); return; }