schedule.c 745 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* https://cirosantilli.com/linux-kernel-module-cheat#schedule */
  2. #include <linux/kernel.h>
  3. #include <linux/kthread.h>
  4. #include <linux/module.h>
  5. #include <uapi/linux/stat.h> /* S_IRUSR | S_IWUSR */
  6. static int do_schedule = 1;
  7. module_param_named(schedule, do_schedule, int, S_IRUSR | S_IWUSR);
  8. static struct task_struct *kthread;
  9. static int work_func(void *data)
  10. {
  11. unsigned int i = 0;
  12. while (!kthread_should_stop()) {
  13. pr_info("%u\n", i);
  14. i++;
  15. if (do_schedule)
  16. schedule();
  17. }
  18. return 0;
  19. }
  20. static int myinit(void)
  21. {
  22. kthread = kthread_create(work_func, NULL, "mykthread");
  23. wake_up_process(kthread);
  24. return 0;
  25. }
  26. static void myexit(void)
  27. {
  28. kthread_stop(kthread);
  29. }
  30. module_init(myinit)
  31. module_exit(myexit)
  32. MODULE_LICENSE("GPL");