Chinaunix首页 | 论坛 | 博客
  • 博客访问: 792413
  • 博文数量: 118
  • 博客积分: 2067
  • 博客等级: 大尉
  • 技术积分: 1751
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-17 14:27
文章存档

2016年(1)

2013年(1)

2012年(3)

2011年(26)

2010年(47)

2009年(40)

分类: LINUX

2011-04-22 11:28:27



  1. 5 #include <linux/module.h>
  2.   6 #include <linux/kernel.h>
  3.   7 #include <linux/delay.h>
  4.   8 #include <linux/jiffies.h>
  5.   9
  6.  10 struct timer_list timer;
  7.  11
  8.  12 static void timer_handle(unsigned long arg)
  9.  13 {
  10.  14 mod_timer(&timer, jiffies + HZ);
  11.  15 printk(" I am in timer\n");
  12.  16 }
  13.  17
  14.  18 static int __init test_init(void)
  15.  19 {
  16.  20
  17.  21 init_timer(&timer);
  18.  22 timer.function = &timer_handle;
  19.  23 timer.expires = jiffies + HZ;
  20.  24 add_timer(&timer);
  21.  25
  22.  26 printk("-------- test start ----------\n");
  23.  27 return 0;
  24.  28 }
  25.  29
  26.  30 static void __exit test_exit(void)
  27.  31 {
  28.  32 del_timer(&timer);
  29.  33 printk("-------- test over ----------\n");
  30.  34 return;
  31.  35 }
  32.  36
  33.  37 MODULE_LICENSE("GPL");
  34.  38
  35.  39 module_init(test_init);
  36.  40 module_exit(test_exit);
  37. ~
说明:
因为是以jiffies为计时单位,所以最低精度为1/HZ秒。如HZ=250,则精度为4ms.

Makefile内容:
  1
  2 name = test.o
  3 #name ?= jprobe.o
  4 obj-m := $(name)
  5
  6 KERNELDIR := /home/tangyt/tmp/linux-2.6-cloud/
  7
  8 ARCH = powerpc
  9 CROSS_COMPILE = /home/tangyt/toolchain/powerpc-linux-gnu-
 10 export ARCH CROSS_COMPILE
 11 #export CC
 12
 13 default:
 14     make -C $(KERNELDIR) M=$(shell pwd) modules
 15
 16
 17 clean:
 18     make -C $(KERNELDIR) M=$(shell pwd) clean

阅读(1206) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~