Advertisement
Guest User

Untitled

a guest
May 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. zad1.c
  2. #include <linux/module.h>
  3. #include <linux/timer.h>
  4.  
  5. static struct timer_list timer;
  6. static unsigned int count = 0;
  7. static unsigned int max = 5;
  8.  
  9. static void timer_handler(unsigned long int data)
  10. {
  11.         count++;
  12.         pr_info("It is %d induction ", count);
  13.         pr_info("Time nr %lu is active!\n", data);
  14.         if(count < max)
  15.         {
  16.                 timer.expires = jiffies + 2*HZ;
  17.                 add_timer(&timer);
  18.         }
  19. }
  20.  
  21. static int __init timer_module_init(void)
  22. {
  23.         init_timer(&timer);
  24.         timer.expires = jiffies + 2*HZ;
  25.         timer.data = 0;
  26.         timer.function = timer_handler;
  27.         add_timer(&timer);
  28.         return 0;
  29. }
  30.  
  31. static void __exit timer_module_exit(void)
  32. {
  33.         if(del_timer_sync(&timer))
  34.                 pr_notice("The timer was not active! \n");
  35. }
  36.  
  37. module_init(timer_module_init);
  38. module_exit(timer_module_exit);
  39. MODULE_LICENSE("GPL");
  40. MODULE_AUTHOR("Marek");
  41. MODULE_DESCRIPTION("Ad. 1. A module demonstrating the usage of timers");
  42. MODULE_VERSION("1.0");
  43. ---------------------------------------------------------------------------
  44. zad2.c
  45. #include <linux/module.h>
  46. #include <linux/timer.h>
  47.  
  48. static struct timer_list timer;
  49. static unsigned int max = 7;
  50. static unsigned int delay = 1;
  51.  
  52. static void timer_handler(unsigned long int data)
  53. {
  54.         pr_info("Time nr %lu is active!\n", data);
  55.         if(delay < max)
  56.         {
  57.                 delay<<=1;
  58.                 timer.expires = jiffies + delay*HZ;
  59.                 add_timer(&timer);
  60.         }
  61. }
  62.  
  63. static int __init timer_module_init(void)
  64. {
  65.         init_timer(&timer);
  66.         timer.expires = jiffies + delay*HZ;
  67.         timer.data = 0;
  68.         timer.function = timer_handler;
  69.         add_timer(&timer);
  70.         return 0;
  71. }
  72.  
  73. static void __exit timer_module_exit(void)
  74. {
  75.         if(del_timer_sync(&timer))
  76.                 pr_notice("The timer was not active! \n");
  77. }
  78.  
  79. module_init(timer_module_init);
  80. module_exit(timer_module_exit);
  81. MODULE_LICENSE("GPL");
  82. MODULE_AUTHOR("Marek");
  83. MODULE_DESCRIPTION("Ad. 2. A module demonstrating the usage of timers");
  84. MODULE_VERSION("1.0");
  85.  
  86. ----------------------------------------------------------------------------
  87. zad4.c
  88. #include <linux/module.h>
  89. #include <linux/hrtimer.h>
  90.  
  91. static struct hrtimer timer;
  92. static ktime_t delay;
  93.  
  94. static enum hrtimer_restart hrtimer_function(struct hrtimer *hrtimer)
  95. {
  96.     u64 overruns;
  97.     pr_info("The timer is active!\n");
  98.     overruns = hrtimer_forward_now(hrtimer,delay);
  99.     pr_info("The overruns number since last activation: %llu.", overruns);
  100.     return HRTIMER_NORESTART; // HRTIMER_NORESTART lub HRTIMER_RESTART
  101. }
  102.  
  103. static int __init hrtimer_module_init(void) {
  104.  
  105.     delay = ktime_set(1,0);
  106.     hrtimer_init(&timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
  107.     timer.function = hrtimer_function;
  108.     hrtimer_start(&timer,delay,HRTIMER_MODE_REL);
  109.     return 0;
  110. }
  111.  
  112. static void __exit hrtimer_module_exit(void) {
  113.  
  114.     if(!hrtimer_cancel(&timer))
  115.     pr_alert("The timer was not active!\n");
  116. }
  117.  
  118. module_init(hrtimer_module_init);
  119. module_exit(hrtimer_module_exit);
  120. MODULE_LICENSE("GPL");
  121. MODULE_VERSION("777");
  122. ------------------------------------------------------
  123. zad5.c
  124. #include<linux/module.h>
  125. #include<linux/hrtimer.h>
  126.  
  127. static struct hrtimer timer1, timer2;
  128. static ktime_t delay1, delay2;
  129. static __u8 zmienna = 0;
  130.  
  131. static enum hrtimer_restart hrtimer_function1(struct hrtimer *hrtimer)
  132. {
  133.     u64 overruns;
  134.     zmienna = zmienna + 2;
  135.     pr_info("The timer 1 - value of variable is %d!\n", zmienna);
  136.     overruns = hrtimer_forward_now(hrtimer,delay1);
  137.     //pr_info("The overruns number since last activation: %llu.", overruns);
  138.     return HRTIMER_RESTART;
  139. }
  140.  
  141. static enum hrtimer_restart hrtimer_function2(struct hrtimer *hrtimer)
  142. {
  143.     u64 overruns;
  144.     zmienna = zmienna + 10;
  145.     pr_info("The timer 2 - value of variable is %d!\n", zmienna);
  146.     overruns = hrtimer_forward_now(hrtimer,delay2);
  147.     //pr_info("The overruns number since last activation: %llu.", overruns);
  148.     return HRTIMER_RESTART;
  149. }
  150.  
  151. static int __init hrtimer_module_init(void)
  152. {
  153.     delay1 = ktime_set(1,0);
  154.     delay2 = ktime_set(3,0);
  155.  
  156.     hrtimer_init(&timer1,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
  157.     hrtimer_init(&timer2,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
  158.  
  159.     timer1.function = hrtimer_function1;
  160.     timer2.function = hrtimer_function2;
  161.  
  162.     hrtimer_start(&timer1,delay1,HRTIMER_MODE_REL);
  163.     hrtimer_start(&timer2,delay2,HRTIMER_MODE_REL);
  164.     return 0;
  165. }
  166.  
  167. static void __exit hrtimer_module_exit(void)
  168. {
  169.     if(!hrtimer_cancel(&timer1))
  170.         pr_alert("The timer 1 was not active!\n");
  171.     if(!hrtimer_cancel(&timer2))
  172.         pr_alert("The timer 2 was not active!\n");
  173. }
  174.  
  175. module_init(hrtimer_module_init);
  176. module_exit(hrtimer_module_exit);
  177.  
  178. MODULE_LICENSE("GPL");
  179. MODULE_VERSION("777");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement