首先需要安装库文件 yum install kernel-devel kernel,支持内核编程。
-------------------------------------------------------------------------------------------------
然后编写内核模块程序
#include
#include
#include
#include
#include
#include
MODULE_AUTHOR("Me");
MODULE_LICENSE("GPL");
#define MYCDEV_MAJOR 231 /*给定的主设备号*/
#define MYCDEV_SIZE 100
static int mycdev_open(struct inode *inode, struct file *fp)
{
return 0;
}
static int mycdev_release(struct inode *inode, struct file *fp)
{
return 0;
}
static ssize_t mycdev_read(struct file *fp, char __user *buf, size_t size, loff_t *pos)
{
unsigned long p = *pos;
unsigned int count = size;
//char kernel_buf[MYCDEV_SIZE]="This is mycdev!";
char kernel_buf[MYCDEV_SIZE];
int i;
memset(kernel_buf,0,MYCDEV_SIZE);
memcpy(kernel_buf, "This is mycdev!", MYCDEV_SIZE);
if(p >= MYCDEV_SIZE)
return -1;
if(count > MYCDEV_SIZE)
count = MYCDEV_SIZE - p;
if (copy_to_user(buf, kernel_buf, count) != 0) {
printk("read error!\n");
return -1;
}
/*
for (i = 0; i < count; i++) {
__put_user(i, buf);//write 'i' from kernel space to user space's buf;
buf++;
}
*/
printk("reader: %d bytes was read...\n", count);
return count;
}
static ssize_t mycdev_write(struct file *fp, const char __user *buf, size_t size, loff_t *pos)
{
return size;
}
/* 填充 mycdev的 file operation 结构*/
static const struct file_operations mycdev_fops =
{
.owner = THIS_MODULE,
.read = mycdev_read,
.write = mycdev_write,
.open = mycdev_open,
.release = mycdev_release,
};
/*模块初始化函数*/
static int __init mycdev_init(void)
{
int ret;
printk("mycdev module is staring..\n");
ret=register_chrdev(MYCDEV_MAJOR,"my_cdev",&mycdev_fops); /*注册驱动程序*/
if(ret<0){
printk("register failed..\n");
return 0;
}else{
printk("register success..\n");
}
return 0;
}
/*模块卸载函数*/
static void __exit mycdev_exit(void)
{
printk("mycdev module is leaving..\n");
unregister_chrdev(MYCDEV_MAJOR,"my_cdev"); /*注销驱动程序*/
}
module_init(mycdev_init);
module_exit(mycdev_exit);
以上保存为mydev.c
-----------------------------------------------------------------------------------------------
编写测试程序
#include
#include
#include
#include
#include
int main()
{
int testdev;
int i, ret;
char buf[15];
testdev = open("/dev/mycdev", O_RDWR);
if (-1 == testdev) {
printf("cannot open file.\n");
exit(1);
}
if (ret = read(testdev, buf, 15) <15) {
printf("read error!\n");
exit(1);
}
printf("%s\n", buf);
close(testdev);
return 0;
}
以上保存为mydev_test.c
--------------------------------------------------------------------------------------------
编写Makefile文件
obj-m:=mydev.o
PWD:=$(shell pwd)
CUR_PATH:=$(shell uname -r)
KERNEL_PATH:=/usr/src/kernels/$(CUR_PATH)
all:
make -C $(KERNEL_PATH) M=$(PWD) modules
clean:
make -C $(KERNEL_PATH) M=$(PWD) clean
-------------------------------------------------------------------------------------------
命令 #make
结果 :
make -C /usr/src/kernels/2.6.32-279.9.1.el6.i686 M=/root/桌面/homework modules
make[1]: Entering directory `/usr/src/kernels/2.6.32-279.9.1.el6.i686'
CC [M] /root/桌面/homework/mydev.o
/root/桌面/homework/mydev.c: In function ‘mycdev_read’:
/root/桌面/homework/mydev.c:30: warning: unused variable ‘i’
Building modules, stage 2.
MODPOST 1 modules
CC /root/桌面/homework/mydev.mod.o
LD [M] /root/桌面/homework/mydev.ko.unsigned
NO SIGN [M] /root/桌面/homework/mydev.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.32-279.9.1.el6.i686'
[root@bogon 桌面]#
insmod mydev.ko[root@bogon 桌面]#
cat /proc/devices[root@bogon 桌面]#
mknod /dev/mydev c 231 0[root@bogon 桌面]#
chmod 777 /dev/mydev[root@bogon 桌面]#
gcc mycdev_test.c -o mydev_test[root@bogon 桌面]#
./mydev_test [root@bogon 桌面]#
dmesg
阅读(366) | 评论(0) | 转发(0) |