����Ŀ��ΪLinuxϵͳ�����£�
1����д�ں�ģ�������
2�������ں�ģ��
3����дC��������ں�ģ��
����Ϊ���ں�ģ�������豸д���ַ������ٴ��ں�ģ�������豸�����ַ������ȡ�
1��word_count.c�ļ�
#include<linux/module.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/miscdevice.h>
#include<asm/uaccess.h>
//�豸�ļ���
#define DEVICE_NAME "word_count"
//�����д�ַ���
static unsigned char mem[1000];
//��д�ַ������ַ�����,ssize_t�з���int,size_t����int
static ssize_t char_count=0;
// ���豸�ļ���ȡ����ʱ���øú���
// file��ָ���豸�ļ���buf������ɶ�ȡ������ count���ɶ�ȡ���ֽ��� ppos����ȡ���ݵ�ƫ����
static ssize_t word_count_read(struct file *file,char __user *buf,size_t read_count,loff_t *ppos)
{
read_count = char_count;
//printk("read:debug:%d\n",(int)char_count);
// ���ں˿ռ�����ݸ��Ƶ��û��ռ䣬buf�е����ݾ��Ǵ��豸�ļ��ж���������
//if(copy_to_user(buf, (void*)mem,read_count)){
if(copy_to_user(buf, &char_count,read_count)){//ע����֮ǰ��һ������η����ַ�����
return -EINVAL;
}
printk("read:read_count:%d\n",(int)read_count);
char_count=0;
return read_count;
}
// ���豸�ļ�д������ʱ���øú���
// file��ָ���豸�ļ���buf������д������� count��д�����ݵ��ֽ��� ppos��д�����ݵ�ƫ����
static ssize_t word_count_write(struct file *file,const char __user *buf,size_t write_count,loff_t *ppos)
{
char_count=write_count;
// ���û��ռ�����ݸ��Ƶ��ں˿ռ䣬mem�е����ݾ������豸�ļ�д�������
if(copy_from_user((void*)mem,buf,write_count)){
return -EINVAL;
}
mem[write_count]='\0';
printk("write:write_count:%d\n",(int)char_count);
return char_count;
}
static struct file_operations dev_fops=
{.owner=THIS_MODULE, .read=word_count_read, .write=word_count_write};
static struct miscdevice misc=
{.minor=MISC_DYNAMIC_MINOR, .name=DEVICE_NAME, .fops=&dev_fops};
//Linux����װ�غ���
static int __init word_count_init(void)
{
int ret;
//ע���豸�ļ�
ret=misc_register(&misc);
printk("word_count init success!");
return ret;
}
//Linux����ж�غ���
static void __exit word_count_exit(void)
{
// ע�����Ƴ����豸�ļ�
misc_deregister(&misc);
printk("word_count exit success!");
}
2��Makefile�ļ�
TARGET=word_count
KDIR=/usr/src/kernels/3.10.0-327.el7.x86_64 # centos
PWD=$(shell pwd)
obj-m:=$(TARGET).o
default:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.o *.mod.o *.mod.c *.symvers *.order
3��word_count_test.c�ļ�
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char *argv[] )
{
/* ���豸�ļ���/dev/word_count���ľ�� */
int devret;
int word_count=0;
int write_count=0;
/* ���豸�ļ� */
devret = open( "/dev/word_count", O_RDWR );
/* ���open��������-1����ʾ���豸�ļ�ʧ�� */
if ( devret == -1 )
{
printf( "Cann't open file \n" );
return(0);
}
/*
* ���word_count_test������������в���������Ὣ��1������ֵ������ͳ�Ƶ��ַ���
* ���û�������в�������ֻ��ȡ�豸�ļ��е�ֵ
*/
if ( argc > 1 )
{
/* ���豸�ļ�д���ͳ�Ƶ��ַ��� */
write_count=write( devret, argv[1], strlen( argv[1] ) );
/* �����ͳ�Ƶ��ַ��� */
printf( "write string:%s,count:%d\n", argv[1],write_count );
}
// ��ȡ�豸�ļ��еĵ�����
read(devret, &word_count, sizeof(word_count));
// ���ͳ�Ƴ��ĵ�����
printf("word count:%d\n", word_count);
/* �ر��豸�ļ� */
close( devret );
return(0);
}
//ע��װ�غ���
module_init(word_count_init);
//ע��ж�غ���
module_exit(word_count_exit);
MODULE_AUTHOR("Leytton");
MODULE_DESCRIPTION("A simple Word Count Driver");
MODULE_ALIAS("a simplest module");
MODULE_LICENSE("MIT");
4�������ں�ģ��
[hadoop@localhost drivers01]$ make
make -C /usr/src/kernels/3.10.0-327.el7.x86_64 M=/mnt/workbench/workspace/drivers01 modules
make[1]: ����Ŀ¼“/usr/src/kernels/3.10.0-327.el7.x86_64”
CC [M] /mnt/workbench/workspace/drivers01/word_count.o
Building modules, stage 2.
MODPOST 1 modules
CC /mnt/workbench/workspace/drivers01/word_count.mod.o
LD [M] /mnt/workbench/workspace/drivers01/word_count.ko
make[1]: �뿪Ŀ¼“/usr/src/kernels/3.10.0-327.el7.x86_64”
[hadoop@localhost drivers01]$
5�������ں�ģ��
[hadoop@localhost drivers01]$ sudo insmod word_count.ko
[sudo] password for hadoop:
[hadoop@localhost drivers01]$
6������word_count_test.c�ļ�
gcc word_count_test.c -o word_count_test
7��ִ��word_count_test
[hadoop@localhost drivers01]$ sudo ./word_count_test hello12345
write string:hello12345,count:10
word count:10
ע��Ҫ��rootȨ��ִ�У�����᷵�����´���
[hadoop@localhost drivers01]$ ./word_count_test hello12345
Cann't open file
8���鿴�ں�ģ����־
[hadoop@localhost drivers01]$sudo dmesg -c
[15074.261371] write:write_count:10
[15074.261415] read:read_count:10
�������ø��µ�ַ��//m.ajphoenix.com/linux/24792.html