kthreads.c 960 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. static struct task_struct *kthread1, *kthread2;
  9. static int work_func1(void *data)
  10. {
  11. int i = 0;
  12. while (!kthread_should_stop()) {
  13. pr_info("1 %d\n", i);
  14. usleep_range(1000000, 1000001);
  15. i++;
  16. if (i == 10)
  17. i = 0;
  18. }
  19. return 0;
  20. }
  21. static int work_func2(void *data)
  22. {
  23. int i = 0;
  24. while (!kthread_should_stop()) {
  25. pr_info("2 %d\n", i);
  26. usleep_range(1000000, 1000001);
  27. i++;
  28. if (i == 10)
  29. i = 0;
  30. }
  31. return 0;
  32. }
  33. static int myinit(void)
  34. {
  35. kthread1 = kthread_create(work_func1, NULL, "mykthread1");
  36. kthread2 = kthread_create(work_func2, NULL, "mykthread2");
  37. wake_up_process(kthread1);
  38. wake_up_process(kthread2);
  39. return 0;
  40. }
  41. static void myexit(void)
  42. {
  43. kthread_stop(kthread1);
  44. kthread_stop(kthread2);
  45. }
  46. module_init(myinit)
  47. module_exit(myexit)
  48. MODULE_LICENSE("GPL");