kthreads.c 937 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. 2 kthreads!!! Will they interleave??? Yup.
  3. */
  4. #include <linux/delay.h> /* usleep_range */
  5. #include <linux/kernel.h>
  6. #include <linux/kthread.h>
  7. #include <linux/module.h>
  8. MODULE_LICENSE("GPL");
  9. static struct task_struct *kthread1, *kthread2;
  10. static int work_func1(void *data)
  11. {
  12. int i = 0;
  13. while (!kthread_should_stop()) {
  14. printk(KERN_INFO "1 %d\n", i);
  15. usleep_range(1000000, 1000001);
  16. i++;
  17. if (i == 10)
  18. i = 0;
  19. }
  20. return 0;
  21. }
  22. static int work_func2(void *data)
  23. {
  24. int i = 0;
  25. while (!kthread_should_stop()) {
  26. printk(KERN_INFO "2 %d\n", i);
  27. usleep_range(1000000, 1000001);
  28. i++;
  29. if (i == 10)
  30. i = 0;
  31. }
  32. return 0;
  33. }
  34. int init_module(void)
  35. {
  36. kthread1 = kthread_create(work_func1, NULL, "mykthread1");
  37. kthread2 = kthread_create(work_func2, NULL, "mykthread2");
  38. wake_up_process(kthread1);
  39. wake_up_process(kthread2);
  40. return 0;
  41. }
  42. void cleanup_module(void)
  43. {
  44. kthread_stop(kthread1);
  45. kthread_stop(kthread2);
  46. }