samir82show

worker model interrupt ordered

Sep 27th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. #include <linux/module.h>
  2. #include <linux/semaphore.h>
  3. #include <linux/delay.h>
  4. #include <linux/completion.h>
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/string.h>
  8. #include <linux/kthread.h>
  9. #include <linux/mutex.h>
  10. #include <linux/wait.h>
  11. MODULE_LICENSE("GPL");
  12. MODULE_AUTHOR("Sanfoundry-Student");
  13. MODULE_DESCRIPTION("Kernel Producer-Consumer Thread assignment");
  14. struct arg {
  15. char name[20];
  16. } arg[4] = {{"writer1"}, {"reader1"}, {"writer2"}, {"reader2"}};
  17.  
  18. static struct task_struct *threadtest0;
  19. static struct task_struct *threadtest1;
  20. static struct task_struct *threadtest2;
  21. static struct task_struct *threadtest3;
  22. unsigned i;
  23. unsigned char state;
  24. //static DEFINE_SPINLOCK (spinLK);
  25. static DECLARE_MUTEX (semLK);
  26. static int sanfd_producer (void *arg) {
  27. char *c = (char *)arg;
  28. while (!kthread_should_stop ()) {
  29. if ((strcmp (c, "writer1")) == 0) {
  30. if (down_interruptible (&semLK))
  31. printk (KERN_INFO "semaphore not acquired\n");
  32. if (state == 0) {
  33. i++;
  34. printk (KERN_INFO "%s seq #%d\n", c, i);
  35. state = 1;
  36. }
  37. up (&semLK);
  38. } else if ((strcmp (c, "writer2")) == 0) {
  39. if (down_interruptible (&semLK))
  40. printk (KERN_INFO "semaphore not acquired\n");
  41. if (state == 2) {
  42. i++;
  43. printk (KERN_INFO "%s seq #%d\n", c, i);
  44. state = 3;
  45. }
  46. up (&semLK);
  47. }
  48. schedule ();
  49. }
  50. return 0;
  51. }
  52.  
  53. static int sanfd_consumer (void *arg) {
  54. char *c = (char *)arg;
  55. while (!kthread_should_stop ()) {
  56. if ((strcmp (c, "reader1")) == 0) {
  57. if (down_interruptible (&semLK))
  58. printk (KERN_INFO "semaphore not acquired\n");
  59. if (state == 1) {
  60. printk (KERN_INFO "%s seq #%d\n", c, i);
  61. state = 2;
  62. }
  63. up (&semLK);
  64. } else if ((strcmp (c, "reader2")) == 0) {
  65. if (down_interruptible (&semLK))
  66. printk (KERN_INFO "semaphore not acquired\n");
  67. if (state == 3) {
  68. printk (KERN_INFO "%s seq #%d\n", c, i);
  69. state = 0;
  70. }
  71. up (&semLK);
  72. }
  73. schedule ();
  74. }
  75. return 0;
  76. }
  77.  
  78. static int __init sanfd_kthread_init(void)
  79. {
  80. printk(KERN_INFO "Loaded kthreadwork module\n");
  81. threadtest0 = kthread_run (sanfd_producer, &arg[0], "%s%d\n", arg[0].name, i);
  82. threadtest1 = kthread_run (sanfd_consumer, &arg[1], "%s%d\n", arg[1].name, i);
  83. threadtest2 = kthread_run (sanfd_producer, &arg[2], "%s%d\n", arg[2].name, i);
  84. threadtest3 = kthread_run (sanfd_consumer, &arg[3], "%s%d\n", arg[3].name, i);
  85. if (!threadtest0 || !threadtest1 || !threadtest2 || !threadtest3) {
  86. printk (KERN_ERR "unable to start kernel thread\n");
  87. return 1;
  88. }
  89. printk (KERN_INFO "threads started\n");
  90. return 0; // Non-zero return means that the module couldn't be loaded.
  91. }
  92.  
  93. static void __exit sanfd_kthread_cleanup(void)
  94. {
  95. printk(KERN_INFO "Cleaning up kthreadwork module.\n");
  96. kthread_stop (threadtest0);
  97. kthread_stop (threadtest1);
  98. kthread_stop (threadtest2);
  99. kthread_stop (threadtest3);
  100. }
  101.  
  102. module_init(sanfd_kthread_init);
  103. module_exit(sanfd_kthread_cleanup);
Advertisement
Add Comment
Please, Sign In to add comment