timer.c 862 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#timers */
  2. #include <linux/jiffies.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/timer.h>
  6. static int i;
  7. /* We would normally mark this as static and give it a more generic name.
  8. * But let's do it like this this time for the sake of our GDB kernel module step debugging example. */
  9. void lkmc_timer_callback(struct timer_list *data);
  10. static unsigned long onesec;
  11. DEFINE_TIMER(mytimer, lkmc_timer_callback);
  12. void lkmc_timer_callback(struct timer_list *data)
  13. {
  14. pr_info("%d\n", i);
  15. i++;
  16. if (i == 10)
  17. i = 0;
  18. mod_timer(&mytimer, jiffies + onesec);
  19. }
  20. static int myinit(void)
  21. {
  22. onesec = msecs_to_jiffies(1000);
  23. mod_timer(&mytimer, jiffies + onesec);
  24. return 0;
  25. }
  26. static void myexit(void)
  27. {
  28. del_timer(&mytimer);
  29. }
  30. module_init(myinit)
  31. module_exit(myexit)
  32. MODULE_LICENSE("GPL");