Linux内核模块开发实例学习
2017-09-14 09:09
302 查看
注:以下程序只是在我机器上测试通过,但代码不一定合理或高效,只是想了解一下内核模块的开发流程,以及工作流程
例子来源于网络,在此表示感谢
[cpp] view
plain copy
[root@localhost module]# cat /proc/version
Linux version 2.6.18-194.el5xen (mockbuild@builder16.centos.org) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)) #1 SMP Fri Apr 2 16:16:54 EDT 2010
1、编写最简单的helloworld版本的内核模块
[cpp] view
plain copy
[root@localhost module]# cat hello.c
#ifndef _KERNEL_
#define _KERNEL_
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int hello_init(void)
{
printk(KERN_ALERT "hello, I am Tuotuo\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "googbye Kernel mymodule n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile文件为
[cpp] view
plain copy
[root@localhost module]# cat Makefile
obj-m:=hello.o
VERSION_NUM:=$(shell uname -r)
KERNELBUILD := /lib/modules/`uname -r`/build
CURRENT_PATH:=$(shell pwd)
all:
make -C $(KERNELBUILD) M=$(CURRENT_PATH) modules
clean:
make -C $(KERNELBUILD) M=$(CURRENT_PATH) clean
编译
make
加载模块
insmod hello.ko
卸载模块
[cpp] view
plain copy
rmmod hello.ko
查看加载和卸载信息
dmesg
2、学习调用模块
学习http://blog.csdn.net/firststp/article/details/395009
内核模块代码
[cpp] view
plain copy
[root@localhost module]# cat TestModule.c
#ifndef _KERNEL_
#define _KERNEL_
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <linux/utsrelease.h>
#define DATA_LENGTH 10
MODULE_LICENSE("GPL");
MODULE_AUTHOR("TUO LI HENG");
char kernel_version[] = UTS_RELEASE;
unsigned int test_major = 0;
ssize_t read_test(struct file *file, char * buf, size_t count, loff_t *f_ops)
{
int left, i, *p;
int data[DATA_LENGTH];
p = data;
for(i=0; i<10; ++i)
data[i] = 61;
for(left=count; left>0; --left)
{
copy_to_user(buf, p, 1);
++buf;
++p;
}
return 0;
}
ssize_t write_test(struct file *file, char * buf, size_t count, loff_t *f_ops)
{
return 0;
}
static int open_test(struct inode* inode, struct file *file)
{
// MOD_INC_USE_COUNT;
return 0;
}
static int release_test(struct inode *inode, struct file *file)
{
// MOD_DEC_USE_COUNT;
return 0;
}
struct file_operations test_fops = {
read: read_test,
write: write_test,
open: open_test,
release: release_test
};
static int hello_init(void)
{
printk(KERN_ALERT "hello, I am Tuotuo\n");
int result = 0;
result = register_chrdev (0, "test", &test_fops);
if(result < 0)
{
printk(KERN_ALERT "test:can't get major number\n");
return result;
}
if(test_major == 0)
test_major = result;
printk(KERN_ALERT "test major:%d/r/n", result);
return 0;
}
static void hello_exit(void)
{
unregister_chrdev(test_major, "test");
printk(KERN_ALERT "googbye Kernel module tuotuo\n");
}
module_init(hello_init);
module_exit(hello_exit);
在以上的Makefile里第一行修改为:
[cpp] view
plain copy
obj-m:=TestModule.o
同样
[html] view
plain copy
make
insmod TestModule.ko
dmesg
测试前,先看看是否已经建立了设备文件。 检查 /dev下是否有test设备文件,如果没有,那我们就手工建立一个。用前面看到的主设备号,使用命令: “mknod test c 268 0” --引用博文
编写调用程序
[html] view
plain copy
[root@localhost module]# cat TestApp.c
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
char buf[20] = {0,};
int i = 0;
int p = open("/dev/test", O_RDWR);
if(p == -1)
{
printf("cannot open\n");
exit(0);
}
printf("buf addr %ui\r\n",buf);
read(p, buf, 10);
for(i = 0; i<10; i++)
{
printf("%s\r\n",buf+i);
}
close(p);
return 1;
}
编译
[html] view
plain copy
gcc TestApp.c -o TestApp
运行
[cpp] view
plain copy
[root@localhost module]# ./TestApp
buf addr 3218386872i
==========
=========
========
=======
======
=====
====
===
==
=
例子来源于网络,在此表示感谢
[cpp] view
plain copy
[root@localhost module]# cat /proc/version
Linux version 2.6.18-194.el5xen (mockbuild@builder16.centos.org) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)) #1 SMP Fri Apr 2 16:16:54 EDT 2010
1、编写最简单的helloworld版本的内核模块
[cpp] view
plain copy
[root@localhost module]# cat hello.c
#ifndef _KERNEL_
#define _KERNEL_
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int hello_init(void)
{
printk(KERN_ALERT "hello, I am Tuotuo\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "googbye Kernel mymodule n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile文件为
[cpp] view
plain copy
[root@localhost module]# cat Makefile
obj-m:=hello.o
VERSION_NUM:=$(shell uname -r)
KERNELBUILD := /lib/modules/`uname -r`/build
CURRENT_PATH:=$(shell pwd)
all:
make -C $(KERNELBUILD) M=$(CURRENT_PATH) modules
clean:
make -C $(KERNELBUILD) M=$(CURRENT_PATH) clean
编译
make
加载模块
insmod hello.ko
卸载模块
[cpp] view
plain copy
rmmod hello.ko
查看加载和卸载信息
dmesg
2、学习调用模块
学习http://blog.csdn.net/firststp/article/details/395009
内核模块代码
[cpp] view
plain copy
[root@localhost module]# cat TestModule.c
#ifndef _KERNEL_
#define _KERNEL_
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <linux/utsrelease.h>
#define DATA_LENGTH 10
MODULE_LICENSE("GPL");
MODULE_AUTHOR("TUO LI HENG");
char kernel_version[] = UTS_RELEASE;
unsigned int test_major = 0;
ssize_t read_test(struct file *file, char * buf, size_t count, loff_t *f_ops)
{
int left, i, *p;
int data[DATA_LENGTH];
p = data;
for(i=0; i<10; ++i)
data[i] = 61;
for(left=count; left>0; --left)
{
copy_to_user(buf, p, 1);
++buf;
++p;
}
return 0;
}
ssize_t write_test(struct file *file, char * buf, size_t count, loff_t *f_ops)
{
return 0;
}
static int open_test(struct inode* inode, struct file *file)
{
// MOD_INC_USE_COUNT;
return 0;
}
static int release_test(struct inode *inode, struct file *file)
{
// MOD_DEC_USE_COUNT;
return 0;
}
struct file_operations test_fops = {
read: read_test,
write: write_test,
open: open_test,
release: release_test
};
static int hello_init(void)
{
printk(KERN_ALERT "hello, I am Tuotuo\n");
int result = 0;
result = register_chrdev (0, "test", &test_fops);
if(result < 0)
{
printk(KERN_ALERT "test:can't get major number\n");
return result;
}
if(test_major == 0)
test_major = result;
printk(KERN_ALERT "test major:%d/r/n", result);
return 0;
}
static void hello_exit(void)
{
unregister_chrdev(test_major, "test");
printk(KERN_ALERT "googbye Kernel module tuotuo\n");
}
module_init(hello_init);
module_exit(hello_exit);
在以上的Makefile里第一行修改为:
[cpp] view
plain copy
obj-m:=TestModule.o
同样
[html] view
plain copy
make
insmod TestModule.ko
dmesg
测试前,先看看是否已经建立了设备文件。 检查 /dev下是否有test设备文件,如果没有,那我们就手工建立一个。用前面看到的主设备号,使用命令: “mknod test c 268 0” --引用博文
编写调用程序
[html] view
plain copy
[root@localhost module]# cat TestApp.c
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
char buf[20] = {0,};
int i = 0;
int p = open("/dev/test", O_RDWR);
if(p == -1)
{
printf("cannot open\n");
exit(0);
}
printf("buf addr %ui\r\n",buf);
read(p, buf, 10);
for(i = 0; i<10; i++)
{
printf("%s\r\n",buf+i);
}
close(p);
return 1;
}
编译
[html] view
plain copy
gcc TestApp.c -o TestApp
运行
[cpp] view
plain copy
[root@localhost module]# ./TestApp
buf addr 3218386872i
==========
=========
========
=======
======
=====
====
===
==
=
相关文章推荐
- Linux内核模块开发实例学习
- Apache—DBUtils框架开发学习实例
- Hasen的linux设备驱动开发学习之旅--简单字符驱动实例globalmem
- HBase开发实例学习
- 使用ionic开发移动端项目学习实例(1)
- 使用ionic开发移动端项目学习实例(2)
- socket编程的最简单实例 - linux系统编程及驱动开发 - 小超嵌入式工作室 嵌入式开发学习交流论坛 XC-STC XC2440技术支持 - Powered by Discuz!
- Web Service学习-CXF开发Web Service实例demo(一)
- 学习Spark的入门教程——《Spark大数据实例开发教程》
- cocos2d-x学习(4)---游戏实例开发:一个简单的射击游戏三
- Spring实例学习第一步,创建数据库(附Spring3.0.5完整压缩包下载地址以及Spring3.0开发所需要的jar包)
- ThinkPHP3.2 学习与实例开发 独立分组(一)
- iOS开发学习第十四课——实例变量可见度、方法、setter、getter
- Eclipse插件开发 学习笔记 PDF 第一篇到第四篇 免分下载 开发基础 核心技术 高级进阶 综合实例
- VC实例学习(4):BT拓展开发文档
- Delphi开发学习三:TdataSource 控件与 TDBGrid 控件使用实例
- Android开发学习之Toasting应用与实例
- cocos2d-x学习(2)---游戏实例开发:一个简单的射击游戏一
- ((ios开发学习笔记九)) Simple TableView 实现(附 实例源码)
- Spring boot入门实例 简易登录(精通Spring+4.x++企业应用开发实战 学习笔记二)