sleep.c 892 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. static struct workqueue_struct *queue;
  14. static atomic_t run = ATOMIC_INIT(1);
  15. static void work_func(struct work_struct *work)
  16. {
  17. int i = 0;
  18. while (atomic_read(&run)) {
  19. pr_info("%d\n", i);
  20. usleep_range(1000000, 1000001);
  21. i++;
  22. if (i == 10)
  23. i = 0;
  24. }
  25. }
  26. static int myinit(void)
  27. {
  28. DECLARE_WORK(work, work_func);
  29. queue = create_workqueue("myworkqueue");
  30. queue_work(queue, &work);
  31. return 0;
  32. }
  33. static void myexit(void)
  34. {
  35. atomic_set(&run, 0);
  36. destroy_workqueue(queue);
  37. }
  38. module_init(myinit)
  39. module_exit(myexit)
  40. MODULE_LICENSE("GPL");