sleep.c 860 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Usage:
  3. insmod /sleep.ko
  4. rmmod sleep
  5. dmesg prints an integer every second until rmmod.
  6. Since insmod returns, this also illustrates how the work queues are asynchronous.
  7. */
  8. #include <linux/delay.h> /* usleep_range */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/types.h> /* atomic_t */
  12. #include <linux/workqueue.h>
  13. MODULE_LICENSE("GPL");
  14. static struct workqueue_struct *queue;
  15. static atomic_t run = ATOMIC_INIT(1);
  16. static void work_func(struct work_struct *work)
  17. {
  18. int i = 0;
  19. while (atomic_read(&run)) {
  20. printk(KERN_INFO "%d\n", i);
  21. usleep_range(1000000, 1000001);
  22. i++;
  23. if (i == 10)
  24. i = 0;
  25. }
  26. }
  27. int init_module(void)
  28. {
  29. DECLARE_WORK(work, work_func);
  30. queue = create_workqueue("myworkqueue");
  31. queue_work(queue, &work);
  32. return 0;
  33. }
  34. void cleanup_module(void)
  35. {
  36. atomic_set(&run, 0);
  37. destroy_workqueue(queue);
  38. }