计算机操作系统课程设计博客,操作系统课程设计

程序实现方法和思路:

本实验的驱动程序是比较简单的,没有涉及到申请文件系统,仅简单地做了个模仿字符设备的就在内存缓冲区(buffer)写数据跟读数据分别调用了中的copy_to_user

和copy_from_user来实现的,其中主要还是填充内核文件操作集这个结构体包括read

,write,open,release等等.

驱动模块加载的时候是要调用init_module(),模块卸载时是要调用exit_module(),

在init_module()函数体中注册设备register_chrdev(),在exit_module()中要unregister_chardev.

驱动代码:

#include

#include

#include

#if defined (CONFIG_MODVERSIONS)

#define MODVERSIONS

#include

#endif

#include//size_t

#include//COPY_TO_USER

//#include

//#include

//#include

#define MY_MAJOR 60

#define

MAXBUFFERSIZE 1000

#define DEV_NAME "rwbuff"

#define CLEAR 0x909090

static int opened=0;

static char buff[MAXBUFFERSIZE];

static int rwlenth=0;

static int rwbuff_open(struct inode*node,struct file*fd)

{

if(opened == 1)

{

printk("you can just open this for one time !\n");

return -1;

}

opened

=1;

MOD_INC_USE_COUNT;

printk("Open this module ok!\n");

return 0;

}

static int rwbuff_release(struct inode*node,struct file*fd)

{

if(opened ==1)opened =0;

MOD_DEC_USE_COUNT;

printk("release this module ok!\n");

}

static ssize_t rwbuff_read(struct file*fd,char*buffer,size_t

count,loff_t*ppos)

{

if(rwlenth==0)

{

return 0;

}

if(count>MAXBUFFERSIZE) count = MAXBUFFERSIZE;

copy_to_user(buffer,buff,count);

return count;

}

static ssize_t rwbuff_write(struct file*fd,const char*buffer,size_t

count,loff_t*ppos)

{

if(count >MAXBUFFERSIZE) count = MAXBUFFERSIZE;

copy_from_user(buff,buffer,count);

rwlenth = count;

return count;

}

static int rwbuff_ioctl(struct inode*node,struct file*fd,unsigned

int cmd,unsigned long arg)

{

if(cmd == CLEAR)

{

rwlenth = 0;

printk("clear the buffer success!\n");

}

return 0;

}

static struct file_operations rwbuff_fops={

open:rwbuff_open,

release:rwbuff_release,

write:rwbuff_write,

read:rwbuff_read,

ioctl:rwbuff_ioctl,

};

static int init_module(void)

{

if(register_chrdev(MY_MAJOR,DEV_NAME,&rwbuff_fops))

{

printk("Register failure!\n");

return -1;

}

printk("Register success!\n");

return 0;

}

static void cleanup_module(void)

{

unregister_chrdev(MY_MAJOR,DEV_NAME);

printk("Unregister device success!\n");

}

测试代码是:

#include

#include

#define CLEAR 0x909090

char message[100]="hello world!";

int main(int argc,char*argv[])

{

char*buffer;

int fd = open("/dev/rwbuff",O_RDWR);

if(fd==-1)

{

printf("open failure!\n");

return 1;

}

printf("Testting for write: \n");

write(fd,message,sizeof(message)+1);

printf("Testting for read :");

read(fd,buffer,sizeof(message));

printf("\tRead from the device data : %s

\n",buffer);

printf("Testting for ioctl:");

ioctl(fd,CLEAR,0);

buffer=" ";

if((read(fd,buffer,sizeof(message)))==0)

{

printf("Read date lenth is 0!\n");

printf("Buffer: %s\n",buffer);

}

return 0;

}

测试结果:

运行创建挂载点和挂载驱动的命令后:

运行测试程序后:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值