timer.c 975 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. Print the jiffies every second.
  3. Timers are callbacks that run when an interrupt happens, from the interrupt context itself.
  4. Therefore they produce more accurate timing than thread scheduling, which is more complex,
  5. but you can't do too much work inside of them.
  6. See also:
  7. - http://stackoverflow.com/questions/10812858/timers-in-linux-device-drivers
  8. - https://gist.github.com/yagihiro/310149
  9. */
  10. #include <linux/jiffies.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/timer.h>
  14. static void callback(struct timer_list *data);
  15. static unsigned long onesec;
  16. DEFINE_TIMER(mytimer, callback);
  17. static void callback(struct timer_list *data)
  18. {
  19. pr_info("%u\n", (unsigned)jiffies);
  20. mod_timer(&mytimer, jiffies + onesec);
  21. }
  22. static int myinit(void)
  23. {
  24. onesec = msecs_to_jiffies(1000);
  25. mod_timer(&mytimer, jiffies + onesec);
  26. return 0;
  27. }
  28. static void myexit(void)
  29. {
  30. del_timer(&mytimer);
  31. }
  32. module_init(myinit)
  33. module_exit(myexit)
  34. MODULE_LICENSE("GPL");