The Linux kernel provides the API used by the timer and makes a simple memo. 1. Included header file: Linux/timer.h 2. Data type: struct timer_list;
Main members included:
A. Data: Parameters passed to the timeout processing function, mainly when multiple timers are used simultaneously, the difference is which timer timed out.
B. Expires: Timer timeout time, measured in terms of Linux jiffies.
c. void (*function) (unsigned long): Timer timeout handler function.
3. Main related API functions:
A. Init_timer (struct timer_list*): Timer initialization function;
b. Add_timer (struct timer_list*): Adding timers to the system;
Mod_timer (struct Timer_list *, unsigned long jiffier_timerout): The time to modify the timer timeout is jiffies_timerout;
D. timer_pending (struct timer_list *): Timer state Query, if in the system's timer list return 1, otherwise return 0
E. Del_timer (struct timer_list*): Delete timer.
4. Time and Jiffies conversion function:
The jiffies in a Linux system is similar to the TickCount in Windows, which is defined as a global variable within the kernel, except that its units are not seconds or milliseconds. Typically 250 jiffies for one second, the kernel can be defined directly using the macro: HZ. Here are a couple of time and jiffies reciprocal conversion functions:
unsigned int jiffies_to_msecs (unsigned long);
unsigned int jiffies_to_usecs (unsigned long);
unsigned long msecs_to_jiffies (unsigned int);
unsigned long usecs_to_jiffies (unsigned int);
5. Use simple example:1,
Step: Init_timer->[timer.expires= & timer.function=?] ->add_timer->[mod_timer]->del_timer.
#include <linux/init.h> #include <linux/module.h> #include <linux/timer.h> struct
Timer_list timer;
void Timer_handler (unsigned Long data) {PRINTK (kern_info "Timer pending:%d\n", timer_pending (&timer));
Mod_timer (&timer, jiffies+msecs_to_jiffies (1000));
PRINTK (kern_info "jiffies:%ld, data:%ld\n", jiffies, data);
int Timer_init (void) {PRINTK (kern_info "%s jiffies:%ld\n", __func__, jiffies);
PRINTK (kern_info "ji:%d,hz:%d\n", Jiffies_to_msecs (), HZ);
Init_timer (&timer);
Timer.data = 45;
Timer.function = Timer_handler;
Timer.expires = jiffies + HZ;
Add_timer (&timer);
PRINTK (kern_info "Timer pending:%d\n", timer_pending (&timer));
return 0;
} void Timer_exit (void) {PRINTK (kern_info "%s jiffies:%ld\n", __func__, jiffies);
Del_timer (&timer);
} module_init (Timer_init);
Module_exit (Timer_exit); Module_license ("GPL");
</pre><pre code_snippet_id= "1847837" snippet_file_name= "blog_20160824_1_713948" name= "code" class= "CPP" ><span style= "font-family:arial, Helvetica, Sans-serif;" >
</span>
<span style= "font-family:arial, Helvetica, Sans-serif;" >2,</span>
<span style= "font-family:arial, Helvetica, Sans-serif;" > #include <linux/nfs_fs_sb.h></span>
#include <linux/nfs_mount.h>
#include "do_mounts.h"
extern void Kernel_restart (char *cmd);
static void Nfs_mount_timeout ()
{
Kernel_restart ("NFS Mount Faiure");
}
* * Init and start NFS Mount timer
/Init_timer (&nfs_mount_timer);
Nfs_mount_timer.function = &nfs_mount_timeout;
Nfs_mount_timer.expires = jiffies + msecs_to_jiffies (nfsroot_mount_seconds * 1000);
Add_timer (&nfs_mount_timer);
for (;;) {
err = Do_mount_root (Root_dev, "NFS", Root_mountflags, root_data);
if (err = = 0) {
del_timer (&nfs_mount_timer);
return 1; /* NFS Mount Success
/} PRINTK (kern_warning "NFS Mount Faiure before timeout, err=%d\n", err)
; /* Wait 1 second, in case the server refused us immediately * *
ssleep (1);
}