EmbLogic's Blog

Kernel Timers

Timers are used to schedule execution of a function (a timer handler) at a particular time in the future. They thus work differently from task queues and in that you can specify when in the future your function will be called, whereas you can’t tell exactly when a queued task will be executed. On the other hand, kernel timers are similar to task queues in that a function registered in a kernel timer is executed only once — timers aren’t cyclic.

A timer is much easier to use. You register your function once, and the kernel calls it once when the timer expires. Such a functionality is used often within the kernel proper, but it is sometimes needed by the drivers as well, as in the example of the floppy motor. A timer is characterized by its time-out value (in jiffies) and the function to be called when the timer expires. The timer handler receives an argument, which is stored in the data structure, together with a pointer to the handler itself.

The data structure of a timer looks like the following, which is extracted from <linux/timer.h>):

 struct timer_list {
     unsigned long expires;            /* the timeout, in jiffies */
     unsigned long data;               /* argument to the handler */
     void (*function)(unsigned long);  /* handler of the timeout */
     };
These are the functions used to act on timers:
  • void init_timer(struct timer_list *timer);
  • void add_timer(struct timer_list *timer);
  • int del_timer(struct timer_list *timer);

The code for /proc/jitimer is as follows:

struct jit_data{

struct timer_list timer;

unsigned long prev-jiffies;

unsigned char*buff;

};

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>