Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- zad1.c
- #include <linux/module.h>
- #include <linux/timer.h>
- static struct timer_list timer;
- static unsigned int count = 0;
- static unsigned int max = 5;
- static void timer_handler(unsigned long int data)
- {
- count++;
- pr_info("It is %d induction ", count);
- pr_info("Time nr %lu is active!\n", data);
- if(count < max)
- {
- timer.expires = jiffies + 2*HZ;
- add_timer(&timer);
- }
- }
- static int __init timer_module_init(void)
- {
- init_timer(&timer);
- timer.expires = jiffies + 2*HZ;
- timer.data = 0;
- timer.function = timer_handler;
- add_timer(&timer);
- return 0;
- }
- static void __exit timer_module_exit(void)
- {
- if(del_timer_sync(&timer))
- pr_notice("The timer was not active! \n");
- }
- module_init(timer_module_init);
- module_exit(timer_module_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Marek");
- MODULE_DESCRIPTION("Ad. 1. A module demonstrating the usage of timers");
- MODULE_VERSION("1.0");
- ---------------------------------------------------------------------------
- zad2.c
- #include <linux/module.h>
- #include <linux/timer.h>
- static struct timer_list timer;
- static unsigned int max = 7;
- static unsigned int delay = 1;
- static void timer_handler(unsigned long int data)
- {
- pr_info("Time nr %lu is active!\n", data);
- if(delay < max)
- {
- delay<<=1;
- timer.expires = jiffies + delay*HZ;
- add_timer(&timer);
- }
- }
- static int __init timer_module_init(void)
- {
- init_timer(&timer);
- timer.expires = jiffies + delay*HZ;
- timer.data = 0;
- timer.function = timer_handler;
- add_timer(&timer);
- return 0;
- }
- static void __exit timer_module_exit(void)
- {
- if(del_timer_sync(&timer))
- pr_notice("The timer was not active! \n");
- }
- module_init(timer_module_init);
- module_exit(timer_module_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("Marek");
- MODULE_DESCRIPTION("Ad. 2. A module demonstrating the usage of timers");
- MODULE_VERSION("1.0");
- ----------------------------------------------------------------------------
- zad4.c
- #include <linux/module.h>
- #include <linux/hrtimer.h>
- static struct hrtimer timer;
- static ktime_t delay;
- static enum hrtimer_restart hrtimer_function(struct hrtimer *hrtimer)
- {
- u64 overruns;
- pr_info("The timer is active!\n");
- overruns = hrtimer_forward_now(hrtimer,delay);
- pr_info("The overruns number since last activation: %llu.", overruns);
- return HRTIMER_NORESTART; // HRTIMER_NORESTART lub HRTIMER_RESTART
- }
- static int __init hrtimer_module_init(void) {
- delay = ktime_set(1,0);
- hrtimer_init(&timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
- timer.function = hrtimer_function;
- hrtimer_start(&timer,delay,HRTIMER_MODE_REL);
- return 0;
- }
- static void __exit hrtimer_module_exit(void) {
- if(!hrtimer_cancel(&timer))
- pr_alert("The timer was not active!\n");
- }
- module_init(hrtimer_module_init);
- module_exit(hrtimer_module_exit);
- MODULE_LICENSE("GPL");
- MODULE_VERSION("777");
- ------------------------------------------------------
- zad5.c
- #include<linux/module.h>
- #include<linux/hrtimer.h>
- static struct hrtimer timer1, timer2;
- static ktime_t delay1, delay2;
- static __u8 zmienna = 0;
- static enum hrtimer_restart hrtimer_function1(struct hrtimer *hrtimer)
- {
- u64 overruns;
- zmienna = zmienna + 2;
- pr_info("The timer 1 - value of variable is %d!\n", zmienna);
- overruns = hrtimer_forward_now(hrtimer,delay1);
- //pr_info("The overruns number since last activation: %llu.", overruns);
- return HRTIMER_RESTART;
- }
- static enum hrtimer_restart hrtimer_function2(struct hrtimer *hrtimer)
- {
- u64 overruns;
- zmienna = zmienna + 10;
- pr_info("The timer 2 - value of variable is %d!\n", zmienna);
- overruns = hrtimer_forward_now(hrtimer,delay2);
- //pr_info("The overruns number since last activation: %llu.", overruns);
- return HRTIMER_RESTART;
- }
- static int __init hrtimer_module_init(void)
- {
- delay1 = ktime_set(1,0);
- delay2 = ktime_set(3,0);
- hrtimer_init(&timer1,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
- hrtimer_init(&timer2,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
- timer1.function = hrtimer_function1;
- timer2.function = hrtimer_function2;
- hrtimer_start(&timer1,delay1,HRTIMER_MODE_REL);
- hrtimer_start(&timer2,delay2,HRTIMER_MODE_REL);
- return 0;
- }
- static void __exit hrtimer_module_exit(void)
- {
- if(!hrtimer_cancel(&timer1))
- pr_alert("The timer 1 was not active!\n");
- if(!hrtimer_cancel(&timer2))
- pr_alert("The timer 2 was not active!\n");
- }
- module_init(hrtimer_module_init);
- module_exit(hrtimer_module_exit);
- MODULE_LICENSE("GPL");
- MODULE_VERSION("777");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement