workqueue_cheat.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. Usage:
  3. insmod /workqueue_cheat.ko
  4. # dmesg => worker
  5. rmmod workqueue_cheat
  6. Creates a separate thread. So init_module can return, but some work will still get done.
  7. Can't call this just workqueue.c because there is already a built-in with that name:
  8. https://unix.stackexchange.com/questions/364956/how-can-insmod-fail-with-kernel-module-is-already-loaded-even-is-lsmod-does-not
  9. Workqueues are a convenience frontend for kthreads.
  10. Bibliography:
  11. - https://www.ibm.com/developerworks/library/l-tasklets/
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/workqueue.h>
  16. static struct workqueue_struct *queue;
  17. static void work_func(struct work_struct *work)
  18. {
  19. pr_info("worker\n");
  20. }
  21. DECLARE_WORK(work, work_func);
  22. static int myinit(void)
  23. {
  24. queue = create_singlethread_workqueue("myworkqueue");
  25. queue_work(queue, &work);
  26. return 0;
  27. }
  28. static void myexit(void)
  29. {
  30. /* Waits for jobs to finish. */
  31. destroy_workqueue(queue);
  32. }
  33. module_init(myinit)
  34. module_exit(myexit)
  35. MODULE_LICENSE("GPL");