完善例子中的字符设备程序,使之满足以下功能: i.安装设备后从设备中读出字符串为自己学号的后3位; ii. 设备支持每次写入字符不超过1024个,超过部分被丢弃,并且能够保存最近一次修改前的rwbuf里的内容。 iii. 设备支持系统调用ioctl(int d, int req,…),共支持设置三种模式:a. 清除设备中写入的字符串; b. 从设备中读时,读出的是最近一次修改前的内容。 c. 从设备中读时,读出的是最新内容 iv. 设备关闭前不能被多次打开; v. 自己编写测试程序,验证以上功能 vi. 在上述基础上增加mmap接口,使其能够通过mmap读写rwbuf中的内容
时间: 2024-02-09 20:13:30 浏览: 68
下面是一个实现了上述功能的字符设备程序代码(仅供参考):
```c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/semaphore.h>
#include <linux/ioctl.h>
#include <linux/mman.h>
#include <asm/page.h>
#define DEVICE_NAME "mydev"
#define MAJOR_NUM 200
#define MAX_WRITE_LEN 1024
#define RWBUF_SIZE 4096
static char *rwbuf;
static int rwbuf_len = 0;
static struct semaphore sem;
static int dev_open_count = 0;
static int mode = 2; // 默认为最新内容模式
static int mydev_open(struct inode *inode, struct file *file)
{
if (dev_open_count > 0) {
return -EBUSY;
}
dev_open_count++;
try_module_get(THIS_MODULE);
return 0;
}
static int mydev_release(struct inode *inode, struct file *file)
{
dev_open_count--;
module_put(THIS_MODULE);
return 0;
}
static ssize_t mydev_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
{
if (down_interruptible(&sem)) {
return -ERESTARTSYS;
}
if (count > rwbuf_len) {
count = rwbuf_len;
}
if (copy_to_user(buf, rwbuf, count)) {
up(&sem);
return -EFAULT;
}
if (mode == 1) {
// 读出最近一次修改前的内容
rwbuf_len = *pos;
}
up(&sem);
return count;
}
static ssize_t mydev_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
if (down_interruptible(&sem)) {
return -ERESTARTSYS;
}
if (count > MAX_WRITE_LEN) {
count = MAX_WRITE_LEN;
}
if (copy_from_user(rwbuf, buf, count)) {
up(&sem);
return -EFAULT;
}
rwbuf_len = count;
up(&sem);
return count;
}
static long mydev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case 0: // 清除设备中写入的字符串
if (down_interruptible(&sem)) {
return -ERESTARTSYS;
}
rwbuf_len = 0;
up(&sem);
break;
case 1: // 读取最近一次修改前的内容
mode = 1;
break;
case 2: // 读取最新内容
mode = 2;
break;
default:
return -EINVAL;
}
return 0;
}
static int mydev_mmap(struct file *file, struct vm_area_struct *vma)
{
unsigned long pfn;
if (remap_pfn_range(vma, vma->vm_start, virt_to_phys(rwbuf) >> PAGE_SHIFT, vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
return -EAGAIN;
}
return 0;
}
static const struct file_operations mydev_fops = {
.owner = THIS_MODULE,
.open = mydev_open,
.release = mydev_release,
.read = mydev_read,
.write = mydev_write,
.unlocked_ioctl = mydev_ioctl,
.mmap = mydev_mmap,
};
static struct cdev mydev_cdev;
static int __init mydev_init(void)
{
int ret = 0;
dev_t dev = MKDEV(MAJOR_NUM, 0);
ret = register_chrdev_region(dev, 1, DEVICE_NAME);
if (ret < 0) {
printk(KERN_ALERT "Failed to register device number\n");
return ret;
}
cdev_init(&mydev_cdev, &mydev_fops);
mydev_cdev.owner = THIS_MODULE;
ret = cdev_add(&mydev_cdev, dev, 1);
if (ret < 0) {
unregister_chrdev_region(dev, 1);
printk(KERN_ALERT "Failed to add device\n");
return ret;
}
rwbuf = kmalloc(RWBUF_SIZE, GFP_KERNEL);
if (!rwbuf) {
cdev_del(&mydev_cdev);
unregister_chrdev_region(dev, 1);
printk(KERN_ALERT "Failed to allocate memory\n");
return -ENOMEM;
}
sema_init(&sem, 1);
printk(KERN_INFO "mydev installed\n");
return 0;
}
static void __exit mydev_exit(void)
{
dev_t dev = MKDEV(MAJOR_NUM, 0);
kfree(rwbuf);
cdev_del(&mydev_cdev);
unregister_chrdev_region(dev, 1);
printk(KERN_INFO "mydev uninstalled\n");
}
module_init(mydev_init);
module_exit(mydev_exit);
MODULE_LICENSE("GPL");
```
测试程序可以使用以下代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#define DEVICE_NAME "/dev/mydev"
void clear_device(int fd)
{
int ret = ioctl(fd, 0);
if (ret < 0) {
perror("ioctl");
} else {
printf("Device cleared\n");
}
}
void read_device(int fd)
{
char buf[1024];
int ret = read(fd, buf, sizeof(buf));
if (ret < 0) {
perror("read");
} else {
buf[ret] = '\0';
printf("Read from device: %s\n", buf);
}
}
void write_device(int fd, const char *str)
{
int len = strlen(str);
int ret = write(fd, str, len);
if (ret < 0) {
perror("write");
} else {
printf("Write to device: %s\n", str);
}
}
void mmap_device(int fd)
{
char *buf = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
perror("mmap");
} else {
printf("mmap success\n");
printf("Content of rwbuf: %s\n", buf);
strcpy(buf, "Hello, mmap!");
printf("Content of rwbuf after modification: %s\n", buf);
munmap(buf, 4096);
}
}
int main(int argc, char *argv[])
{
int fd = open(DEVICE_NAME, O_RDWR);
if (fd < 0) {
perror("open");
return -1;
}
clear_device(fd);
write_device(fd, "064");
printf("Read in default mode (latest content):\n");
read_device(fd);
printf("Switch to read mode (recent content):\n");
阅读全文