操作系统第四次实验


实验四的第3个就直接先上代码了,有空了再补上来

3)编写Linux驱动程序并编程应用程序测试。
◆提示1:参考任务1
◆提示2:至少实现xx_open,xx_write,xx_read等函数
◆提示3:功能:

  • 内核分配一定长度的缓冲区,比如64字节。
  • xx_write()写进去若干字符,注意维护写入位置。下次继续写的话 ,接着该位置往后写,直到缓冲区末尾。要返回实际写入字数。 
  • xx_read()读出若干字符串,注意维护读出位置。下次继续读的话 ,接着该位置往后读,直到缓冲区末尾。要返回实际读回字数。
  • 扩展:
     缓冲区设置为循环缓冲区?
     如何避免写覆盖,避免读重复?

Makefile

obj-m += test3.o  
 
#generate the path  
CURRENT_PATH:=$(shell pwd)
 
#the current kernel version number  
LINUX_KERNEL:=$(shell uname -r) 
 
#the absolute path--根据获取的内核版本拼装绝对路径
LINUX_KERNEL_PATH:=/usr/src/linux-$(LINUX_KERNEL)  

 
#complie object  
all:  
	make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
#clean  
clean:  
	make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) clean  

设备驱动的代码

#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <asm/io.h>
 
 
#define VIRTUALDISK_SIZE	255
#define MEM_CLEAR			0x1
#define PORT1_SET			0x2
#define PORT2_SET			0x3
#define VIRTUALDISK_MAJOR	210
#define BUFFER_SIZE 		64

static dev_t devno;//创建设备号
static int VirtualDisk_major = VIRTUALDISK_MAJOR;//主设备号
static struct cdev cdev;//构建字符设备结构
unsigned char *device_buffer;
static int W=0,R=0;
 
static int VirtualDisk_open(struct inode *inode, struct file *filp)
{
   
	printk("cdev open\n");
   	return 0;
}
 
static int VirtualDisk_release(struct inode *inode, struct file *filp)
{
   
	printk("cdev release\n");
    return 0;
}

static ssize_t VirtualDisk_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
   
	unsigned long p = *ppos;
	unsigned int count = size;
	unsigned char 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值