Linux timer Using Example

Source: Internet
Author: User

Code:

#include <linux/init.h>#include <linux/module.h>#include <linux/hardirq.h>MODULE_LICENSE("Dual BSD/GPL");static struct timer_list timer_data;static int counter = 0;static void test_timer(unsigned long temp){int ret = 0;printk("test_timer()++\n");ret = in_interrupt();printk("in_interrupt ret = %d\n",ret);ret = in_atomic();printk("in_atomic ret = %d\n",ret);printk("counter = %d\n", counter++);mod_timer(&timer_data, jiffies + HZ);printk("test_timer()--\n");}char *ptest_str = "Hello test_timer\n";static int mmap_init(void){printk(KERN_ALERT"mmap_init()++\n");init_timer(&timer_data);timer_data.expires = jiffies + HZ;timer_data.data = (unsigned long) ptest_str;timer_data.function = test_timer;add_timer(&timer_data);printk(KERN_ALERT"mmap_init()--\n");return 0;}static void mmap_exit(void){printk(KERN_ALERT"mmap_exit()++\n");del_timer(&timer_data);printk(KERN_ALERT"mmap_exit()--\n");}module_init(mmap_init);module_exit(mmap_exit);

Makefile:

# Obj-$ (config_mmp _test) + = MMAP. o

OBJ-M: = helloworld. o
Kerneldr: =/usr/src/linux-headers-2.6.32-38-generic/
PWD: = $ (shell PWD)
Modules:
$ (Make)-C $ (kerneldr) M = $ (PWD) Modules
Echo "make modules"
Moduels_install:
$ (Make)-C $ (kerneldr) M = $ (PWD) modules_install
Clean:
Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions

Test enviroment: x86

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.