timer.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. /* We would normally mark this as static and give it a more generic name.
  15. * But let's do it like this this time for the sake of our GDB kernel module step debugging example. */
  16. void lkmc_timer_callback(struct timer_list *data);
  17. static unsigned long onesec;
  18. DEFINE_TIMER(mytimer, lkmc_timer_callback);
  19. void lkmc_timer_callback(struct timer_list *data)
  20. {
  21. pr_info("%u\n", (unsigned)jiffies);
  22. mod_timer(&mytimer, jiffies + onesec);
  23. }
  24. static int myinit(void)
  25. {
  26. onesec = msecs_to_jiffies(1000);
  27. mod_timer(&mytimer, jiffies + onesec);
  28. return 0;
  29. }
  30. static void myexit(void)
  31. {
  32. del_timer(&mytimer);
  33. }
  34. module_init(myinit)
  35. module_exit(myexit)
  36. MODULE_LICENSE("GPL");