-
-
Save ajgappmark/0f3ab0a5043d92a130dbfaa9cbff9217 to your computer and use it in GitHub Desktop.
a simple timer example of Linux module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright (c) 2015 Song Yang @ ittraining | |
* | |
* All rights reserved. | |
* This program is free to use, but the ban on selling behavior. | |
* Modify the program must keep all the original text description. | |
* | |
* 保留所有權利。 | |
* 本程式可任意使用,但是禁止販售行為。 | |
* 修改程式時必須保留所有原有文字說明。 | |
* | |
* Email: [email protected] | |
* Blog : http://blog.ittraining.com.tw | |
*******************************************************************************/ | |
#include <linux/module.h> | |
#include <linux/init.h> | |
#include <linux/timer.h> | |
MODULE_LICENSE("GPL"); | |
MODULE_AUTHOR("ITtraining.com.tw"); | |
MODULE_DESCRIPTION("A timer example."); | |
struct timer_list my_timer; | |
/* | |
* TIMER FUNCTION | |
* */ | |
static void timer_function(unsigned long data){ | |
printk("Time up"); | |
// modify the timer for next time | |
mod_timer(&my_timer, jiffies + HZ / 2); | |
} | |
/* | |
* INIT MODULE | |
* */ | |
int init_module(void) | |
{ | |
printk("Hello My Timer\n"); | |
// -- initialize the timer | |
init_timer(&my_timer); | |
my_timer.expires = jiffies + HZ ; | |
my_timer.function = timer_function; | |
my_timer.data = NULL; | |
// -- TIMER START | |
add_timer(&my_timer); | |
printk("END: init_module() \n"); | |
return 0; | |
} | |
/* | |
* CLEANUP MODULE | |
* */ | |
void cleanup_module(void) | |
{ | |
del_timer(&my_timer); | |
printk("Goodbye\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment