Advertisement
kamiln60

Untitled

Apr 26th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. 4------------
  2.  
  3. #include<linux/module.h>
  4. #include<linux/workqueue.h>
  5.  
  6. static struct workqueue_struct *queue;
  7.  
  8. static void normal_work_handler(struct work_struct *work)
  9. {
  10. pr_info("Hi! I'm handler of normal work!\n");
  11. }
  12.  
  13. static void delayed_work_handler(struct work_struct *work)
  14. {
  15. pr_info("Hi! I'm handler of delayed work!\n");
  16. }
  17.  
  18. static DECLARE_WORK(normal_work, normal_work_handler);
  19. static DECLARE_DELAYED_WORK(delayed_work, delayed_work_handler);
  20.  
  21. static int __init workqueue_module_init(void)
  22. {
  23. queue = create_singlethread_workqueue("works");
  24. if(IS_ERR(queue)) {
  25. pr_alert("[workqueue_module] Error creating a workqueue: %ld\n",PTR_ERR(queue));
  26. return -ENOMEM;
  27. }
  28.  
  29. if(!queue_work(queue,&normal_work))
  30. pr_info("The normal work was already queued!\n");
  31. if(!queue_delayed_work(queue,&delayed_work,10*HZ))
  32. pr_info("The delayed work was already queued!\n");
  33.  
  34. return 0;
  35. }
  36.  
  37. static void __exit workqueue_module_exit(void)
  38. {
  39. if(!IS_ERR(queue)) {
  40. if(cancel_work_sync(&normal_work))
  41. pr_info("The normal work has not been done yet!\n");
  42. if(cancel_delayed_work_sync(&delayed_work))
  43. pr_info("The delayed work has not been done yet!\n");
  44. destroy_workqueue(queue);
  45. }
  46. }
  47.  
  48. module_init(workqueue_module_init);
  49. module_exit(workqueue_module_exit);
  50.  
  51. MODULE_AUTHOR("Kamil Niekurzak,Szota Przemysław, Krzysztof Skalik");
  52. MODULE_DESCRIPTION("A module demonstrating the use of work queues.");
  53. MODULE_VERSION("1.0");
  54.  
  55.  
  56.  
  57.  
  58. ------------------------------------
  59. 5------
  60.  
  61. #include<linux/module.h>
  62. #include<linux/workqueue.h>
  63.  
  64. static struct workqueue_struct *queue;
  65. static void delayed_work_handler(struct work_struct *work);
  66. static DECLARE_DELAYED_WORK(delayed_work, delayed_work_handler);
  67.  
  68. static void normal_work_handler(struct work_struct *work)
  69. {
  70. pr_info("Hi! I'm handler of normal work!\n");
  71. }
  72.  
  73. static void delayed_work_handler(struct work_struct *work)
  74. {
  75.  
  76. pr_info("Hi! I'm handler of delayed work!\n");
  77. pr_info("Initiating new delayed work\n");
  78.  
  79. if(!queue_delayed_work(queue,&delayed_work,10*HZ))
  80. pr_info("The delayed work was already queued!\n");
  81.  
  82. }
  83.  
  84.  
  85. static DECLARE_WORK(normal_work, normal_work_handler);
  86.  
  87.  
  88. static int __init workqueue_module_init(void)
  89. {
  90. queue = create_singlethread_workqueue("works");
  91. if(IS_ERR(queue)) {
  92. pr_alert("[workqueue_module] Error creating a workqueue: %ld\n",PTR_ERR(queue));
  93. return -ENOMEM;
  94. }
  95.  
  96. if(!queue_work(queue,&normal_work))
  97. pr_info("The normal work was already queued!\n");
  98. if(!queue_delayed_work(queue,&delayed_work,10*HZ))
  99. pr_info("The delayed work was already queued!\n");
  100.  
  101.  
  102. return 0;
  103. }
  104.  
  105. static void __exit workqueue_module_exit(void)
  106. {
  107. if(!IS_ERR(queue)) {
  108. if(cancel_work_sync(&normal_work))
  109. pr_info("The normal work has not been done yet!\n");
  110. if(cancel_delayed_work_sync(&delayed_work))
  111. pr_info("The delayed work has not been done yet!\n");
  112.  
  113.  
  114. destroy_workqueue(queue);
  115. }
  116. }
  117.  
  118. module_init(workqueue_module_init);
  119. module_exit(workqueue_module_exit);
  120. MODULE_LICENSE("GPL");
  121. MODULE_AUTHOR("Kamil Niekurzak,Szota Przemysław, Krzysztof Skalik");
  122. MODULE_DESCRIPTION("A module demonstrating the use of work queues.");
  123. MODULE_VERSION("1.0");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement