Chinaunix首页 | 论坛 | 博客
  • 博客访问: 346366
  • 博文数量: 168
  • 博客积分: 6895
  • 博客等级: 准将
  • 技术积分: 1726
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-12 23:01
文章分类

全部博文(168)

文章存档

2011年(6)

2010年(162)

我的朋友

分类: LINUX

2010-10-06 11:04:07

   LKM is made of Loadable Kernel Module, which let kernel be little and easy.
   Today let us learn how to program LKM together.

First, you should do your program like following.
hello.c
//kernel module
#include
#include

static int hello_init( void )
{
    printk( KERN_ALERT"Hello Modules!\n" );

    return( 0 );
}

static    void hello_exit( void )
{
    printk( KERN_ALERT"Good bye!\n" );
}

MODULE_LICENSE( "GPL" );
MODULE_AUTHOR( "HarryWei" );
module_init( hello_init );
module_exit( hello_exit );

Second you should do your Makefile like following.
Makefile
# Makefile for hello module.
# Programmer's name: Harry Wei( ).
# Time and place: 2010, 10, 5. @ linux laboratory in xiyou.

obj-m    +=    hello.o
KERNEL_PATH     =    /usr/src/linux-headers-$(shell uname -r)

all:
    make    -C    $(KERNEL_PATH)    M=$(PWD) modules

clean:
    make    -C    $(KERNE
L_PATH)    M=$(PWD) clean

Okay, all the work we have done.
Let's make.

Then you should do the command sudo insmod hello.ko and do command dmesg.
You will see like this,

[ 1532.840682] Hello Modules!

Do command lsmod then you will see like following,
Module                  Size  Used by
hello                   9344  0



 Do  command sudo rmmod hello and then do command dmesg, you will see.

[ 4119.244391] Good bye!


okay  If  you have other problem, please give me a mail( harryxiyou@gmail.com ).

阅读(362) | 评论(0) | 转发(0) |
0

上一篇:nslookup

下一篇:begin syscall

给主人留下些什么吧!~~