#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/semaphore.h>
#define DEVICE_NAME "os_dev"
#define BUFFER_SIZE 32
static int major_num;
static char *buffer;
static int pRead = 0;
static int pWrite = 0;
static int bytes_in_buffer = 0;
static struct semaphore sem;
static int dev_open(struct inode *, struct file *);
static int dev_release(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);
static struct file_operations fops = {
.open = dev_open,
.release = dev_release,
.read = dev_read,
.write = dev_write,
};
static int __init os_dev_init(void) {
major_num = register_chrdev(0, DEVICE_NAME, &fops);
if (major_num < 0) {
printk(KERN_ALERT "Failed to register a major number\n");
return major_num;
}
printk(KERN_INFO "Registered correctly with major number %d\n", major_num);
buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
if (!buffer) {
unregister_chrdev(major_num, DEVICE_NAME);
printk(KERN_ALERT "Failed to allocate memory\n");
return -ENOMEM;
}
sema_init(&sem, 1); // Initialize semaphore
return 0;
}
static void __exit os_dev_exit(void) {
kfree(buffer);
unregister_chrdev(major_num, DEVICE_NAME);
printk(KERN_INFO "Unregistered correctly\n");
}
static int dev_open(struct inode *inodep, struct file *filep) {
printk(KERN_INFO "Device opened\n");
return 0;
}
static ssize_t dev_read(struct file *filep, char *buffer, size_t len, loff_t *offset) {
int bytes_to_read;
if (down_interruptible(&sem)) // Acquire semaphore with interruptible sleep
return -ERESTARTSYS;
if (bytes_in_buffer == 0) {
printk(KERN_INFO "Buffer is empty\n");
up(&sem); // Release semaphore
return 0; // Return 0 to indicate end of file (EOF)
}
bytes_to_read = min(len, bytes_in_buffer);
if (pRead + bytes_to_read > BUFFER_SIZE) {
// Wrap around the buffer
int first_part = BUFFER_SIZE - pRead;
if (copy_to_user(buffer, &buffer[pRead], first_part) != 0) {
up(&sem); // Release semaphore
printk(KERN_ALERT "Failed to read from device\n");
return -EFAULT;
}
if (copy_to_user(buffer + first_part, buffer, bytes_to_read - first_part) != 0) {
up(&sem); // Release semaphore
printk(KERN_ALERT "Failed to read from device\n");
return -EFAULT;
}
} else {
if (copy_to_user(buffer, &buffer[pRead], bytes_to_read) != 0) {
up(&sem); // Release semaphore
printk(KERN_ALERT "Failed to read from device\n");
return -EFAULT;
}
}
pRead = (pRead + bytes_to_read) % BUFFER_SIZE;
bytes_in_buffer -= bytes_to_read;
printk(KERN_INFO "Read %d bytes from the device\n", bytes_to_read);
up(&sem); // Release semaphore
return bytes_to_read;
}
static ssize_t dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset) {
int space_available;
int bytes_to_write = len;
if (down_interruptible(&sem)) // Acquire semaphore with interruptible sleep
return -ERESTARTSYS;
space_available = BUFFER_SIZE - bytes_in_buffer;
if (bytes_to_write > space_available) {
up(&sem); // Release semaphore
printk(KERN_ALERT "Buffer is full, cannot write\n");
return -ENOMEM;
}
if (pWrite + bytes_to_write > BUFFER_SIZE) {
// Wrap around the buffer
int first_part = BUFFER_SIZE - pWrite;
if (copy_from_user(&buffer[pWrite], buffer, first_part) != 0) {
up(&sem); // Release semaphore
printk(KERN_ALERT "Failed to write to device\n");
return -EFAULT;
}
if (copy_from_user(buffer, buffer + first_part, bytes_to_write - first_part) != 0) {
up(&sem); // Release semaphore
printk(KERN_ALERT "Failed to write to device\n");
return -EFAULT;
}
} else {
if (copy_from_user(&buffer[pWrite], buffer, bytes_to_write) != 0) {
up(&sem); // Release semaphore
printk(KERN_ALERT "Failed to write to device\n");
return -EFAULT;
}
}
pWrite = (pWrite + bytes_to_write) % BUFFER_SIZE;
bytes_in_buffer += bytes_to_write;
printk(KERN_INFO "Wrote %d bytes to the device\n", bytes_to_write);
up(&sem); // Release semaphore
return bytes_to_write;
}
static int dev_release(struct inode *inodep, struct file *filep) {
printk(KERN_INFO "Device successfully closed\n");
return 0;
}
module_init(os_dev_init);
module_exit(os_dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple character device driver with adjustable size circular buffer");
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define DEVICE_NAME "/dev/os_dev"
#define MESSAGE "hello device\n"
int main() {
int fd;
ssize_t bytes;
int write_count = 1;
fd = open(DEVICE_NAME, O_WRONLY);
if (fd < 0) {
perror("Error opening device");
exit(EXIT_FAILURE);
}
while (1) {
bytes = write(fd, MESSAGE, strlen(MESSAGE));
if (bytes < 0) {
perror("Error writing to device");
break;
}
printf("Write %d: %s", write_count++, MESSAGE);
sleep(1); // 每秒写入一次
}
close(fd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define DEVICE_NAME "/dev/os_dev"
#define BUFFER_SIZE 32
int main() {
int fd;
char buffer[BUFFER_SIZE];
ssize_t bytes;
int read_count = 1;
fd = open(DEVICE_NAME, O_RDONLY);
if (fd < 0) {
perror("Error opening device");
exit(EXIT_FAILURE);
}
while (1) {
bytes = read(fd, buffer, BUFFER_SIZE);
if (bytes < 0) {
perror("Error reading from device");
break;
}
printf("Read %d: %s", read_count++, buffer);
}
close(fd);
return 0;
}