#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/fcntl.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
#define DEVICE_NAME "custom_dev"
#define BUFFER_SIZE 4096
static int major_num;
static char *buffer;
static int head = 0;
static int tail = 0;
static struct mutex lock;
static wait_queue_head_t read_wait, write_wait;
static int __init char_dev_init(void) {
dev_t dev_num;
int ret;
// 注册字符设备
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);
return -ENOMEM;
}
memset(buffer, 0, BUFFER_SIZE);
// 初始化互斥锁和等待队列
mutex_init(&lock);
init_waitqueue_head(&read_wait);
init_waitqueue_head(&write_wait);
return 0;
}
static void __exit char_dev_exit(void) {
// 释放缓冲区和注销字符设备
kfree(buffer);
unregister_chrdev(major_num, DEVICE_NAME);
printk(KERN_INFO "Unregistered correctly\n");
}
static ssize_t char_dev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) {
int ret;
int bytes_to_read;
printk(KERN_INFO "Read operation\n");
wait_event_interruptible(read_wait, (head != tail));
mutex_lock(&lock);
bytes_to_read = min((size_t)(tail - head), count);
ret = copy_to_user(buf, &buffer[head], bytes_to_read);
if (ret == 0) {
head += bytes_to_read;
if (head == BUFFER_SIZE) {
head = 0;
}
} else {
ret = -EFAULT;
}
mutex_unlock(&lock);
return ret ? ret : bytes_to_read;
}
static ssize_t char_dev_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos) {
int ret;
int bytes_to_write;
printk(KERN_INFO "Write operation\n");
wait_event_interruptible(write_wait, (head != tail));
mutex_lock(&lock);
bytes_to_write = min((size_t)(BUFFER_SIZE - (tail - head)), count);
ret = copy_from_user(&buffer[tail], buf, bytes_to_write);
if (ret == 0) {
tail += bytes_to_write;
if (tail == BUFFER_SIZE) {
tail = 0;
}
} else {
ret = -EFAULT;
}
mutex_unlock(&lock);
if (!ret) {
wake_up_interruptible(&read_wait); // 唤醒读进程
}
return ret ? ret : bytes_to_write;
}
static int char_dev_open(struct inode *inode, struct file *filp) {
printk(KERN_INFO "Device opened\n");
return 0;
}
static int char_dev_release(struct inode *inode, struct file *filp) {
printk(KERN_INFO "Device released\n");
return 0;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.read = char_dev_read,
.write = char_dev_write,
.open = char_dev_open,
.release = char_dev_release,
};
module_init(char_dev_init);
module_exit(char_dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple character device driver with circular buffer and mutex");
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define DEVICE_NAME "/dev/custom_dev"
int main() {
int fd;
char buffer[1024];
ssize_t bytes;
fd = open(DEVICE_NAME, O_RDONLY);
if (fd < 0) {
perror("Error opening device");
exit(EXIT_FAILURE);
}
while (1) {
bytes = read(fd, buffer, sizeof(buffer));
if (bytes < 0) {
perror("Error reading from device");
break;
} else if (bytes == 0) {
printf("Reached end of file, no more data to read\n");
break;
}
// 打印读取的数据
printf("Read %zd bytes: %.*s\n", bytes, (int)bytes, buffer);
}
close(fd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define DEVICE_NAME "/dev/custom_dev"
int main() {
int fd;
const char *message = "Hello, this is a test message!\n";
ssize_t bytes;
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("Written %zd bytes to device\n", bytes);
// 休眠一段时间后再写入,模拟生产者节奏
usleep(100000); // 休眠100ms
}
close(fd);
return 0;
}
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/mutex.h>
#define DEVICE_NAME "custom_char_dev"
#define BUFFER_SIZE 32
static char *buffer;
static int pRead = 0;
static int pWrite = 0;
static int bytes_in_buffer = 0;
static DECLARE_WAIT_QUEUE_HEAD(read_queue);
static DECLARE_WAIT_QUEUE_HEAD(write_queue);
static DEFINE_MUTEX(mutex);
static ssize_t dev_read(struct file *file, char __user *user_buffer, size_t count, loff_t *offset) {
ssize_t bytes_read = 0;
if (wait_event_interruptible(read_queue, bytes_in_buffer > 0)) {
return -ERESTARTSYS;
}
mutex_lock(&mutex);
bytes_read = min_t(size_t, count, bytes_in_buffer);
if (copy_to_user(user_buffer, buffer + pRead, bytes_read)) {
mutex_unlock(&mutex);
return -EFAULT;
}
pRead = (pRead + bytes_read) % BUFFER_SIZE;
bytes_in_buffer -= bytes_read;
mutex_unlock(&mutex);
wake_up(&write_queue);
return bytes_read;
}
static ssize_t dev_write(struct file *file, const char __user *user_buffer, size_t count, loff_t *offset) {
ssize_t bytes_written = 0;
if (wait_event_interruptible(write_queue, bytes_in_buffer < BUFFER_SIZE)) {
return -ERESTARTSYS;
}
mutex_lock(&mutex);
bytes_written = min_t(size_t, count, BUFFER_SIZE - bytes_in_buffer);
if (copy_from_user(buffer + pWrite, user_buffer, bytes_written)) {
mutex_unlock(&mutex);
return -EFAULT;
}
pWrite = (pWrite + bytes_written) % BUFFER_SIZE;
bytes_in_buffer += bytes_written;
mutex_unlock(&mutex);
wake_up(&read_queue);
return bytes_written;
}
static int dev_open(struct inode *inode, struct file *file) {
printk(KERN_INFO "Device %s opened\n", DEVICE_NAME);
return 0;
}
static int dev_release(struct inode *inode, struct file *file) {
printk(KERN_INFO "Device %s closed\n", DEVICE_NAME);
return 0;
}
static struct file_operations fops = {
.open = dev_open,
.release = dev_release,
.read = dev_read,
.write = dev_write,
};
static struct miscdevice custom_char_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &fops,
};
static int __init custom_char_dev_init(void) {
int ret;
buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
if (!buffer) {
printk(KERN_ERR "Failed to allocate buffer\n");
return -ENOMEM;
}
ret = misc_register(&custom_char_dev);
if (ret) {
printk(KERN_ERR "Failed to register misc device\n");
kfree(buffer);
return ret;
}
printk(KERN_INFO "Custom char device registered\n");
return 0;
}
static void __exit custom_char_dev_exit(void) {
misc_deregister(&custom_char_dev);
kfree(buffer);
printk(KERN_INFO "Custom char device unregistered\n");
}
module_init(custom_char_dev_init);
module_exit(custom_char_dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Custom Character Device Driver");
read
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define DEVICE_NAME "/dev/custom_dev"
#define BUFFER_SIZE 1024
int main() {
int fd;
char buffer[BUFFER_SIZE];
ssize_t bytes;
fd = open(DEVICE_NAME, O_RDONLY);
if (fd < 0) {
perror("Error opening device");
exit(EXIT_FAILURE);
}
while (1) {
bytes = read(fd, buffer, sizeof(buffer) - 1);
if (bytes < 0) {
perror("Error reading from device");
break;
}
buffer[bytes] = '\0'; // Null terminate the string
printf("Read: %s\n", buffer);
sleep(1); // 每秒读取一次
}
close(fd);
return 0;
}
write:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define DEVICE_NAME "/dev/custom_dev"
#define MESSAGE "Hello, this is a test message!\n"
int main() {
int fd;
ssize_t bytes;
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("Written %zd bytes to device\n", bytes);
sleep(1); // 每秒写入一次
}
close(fd);
return 0;
}
xie
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define DEVICE_NAME "/dev/custom_char_dev"
#define MESSAGE "hello device\n"
int main() {
int fd;
ssize_t bytes;
int write_count = 0;
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("Wrote: %s %d\n", MESSAGE, write_count++);
sleep(1); // 每秒写入一次
}
close(fd);
return 0;
}
读
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define DEVICE_NAME "/dev/custom_dev"
#define BUFFER_SIZE 1024
int main() {
int fd;
char buffer[BUFFER_SIZE];
ssize_t bytes;
fd = open(DEVICE_NAME, O_RDONLY);
if (fd < 0) {
perror("Error opening device");
exit(EXIT_FAILURE);
}
while (1) {
bytes = read(fd, buffer, sizeof(buffer) - 1);
if (bytes < 0) {
perror("Error reading from device");
break;
} else if (bytes == 0) {
printf("No more data to read\n");
break;
}
buffer[bytes] = '\0'; // Null terminate the string
printf("Read: %s\n", buffer);
sleep(1); // 每秒读取一次
}
close(fd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h> // 添加错误处理头文件
#define DEVICE_NAME "/dev/custom_dev"
#define BUFFER_SIZE 1024
int main() {
int fd;
char buffer[BUFFER_SIZE];
ssize_t bytes;
size_t total_bytes_read = 0; // 用于跟踪读取的总字节数
fd = open(DEVICE_NAME, O_RDONLY);
if (fd < 0) {
perror("Error opening device");
exit(EXIT_FAILURE);
}
while (1) {
bytes = read(fd, buffer + total_bytes_read, sizeof(buffer) - total_bytes_read - 1);
if (bytes < 0) {
perror("Error reading from device");
break;
} else if (bytes == 0) {
printf("No more data to read\n");
break;
}
total_bytes_read += bytes;
// 检查是否包含完整的一行
char *newline_pos = strchr(buffer, '\n');
if (newline_pos != NULL) {
*newline_pos = '\0'; // 将换行符替换为空字符
printf("Read: %s\n", buffer);
memmove(buffer, newline_pos + 1, total_bytes_read - (newline_pos - buffer) - 1);
total_bytes_read -= (newline_pos - buffer) + 1;
}
sleep(1); // 每秒读取一次
}
close(fd);
return 0;
}
//read
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#define DEVICE_NAME "/dev/custom_char_dev"
#define BUFFER_SIZE 1024
int main() {
int fd;
char buffer[BUFFER_SIZE];
ssize_t bytes;
size_t total_bytes_read = 0;
int read_count = 1; // 从1开始编号
fd = open(DEVICE_NAME, O_RDONLY);
if (fd < 0) {
perror("Error opening device");
exit(EXIT_FAILURE);
}
while (1) {
bytes = read(fd, buffer + total_bytes_read, sizeof(buffer) - total_bytes_read - 1);
if (bytes < 0) {
perror("Error reading from device");
break;
} else if (bytes == 0) {
printf("No more data to read\n");
break;
}
total_bytes_read += bytes;
char *newline_pos = strchr(buffer, '\n');
if (newline_pos != NULL) {
*newline_pos = '\0';
printf("Read: %s [Count: %d]\n", buffer, read_count++); // 输出编号
memmove(buffer, newline_pos + 1, total_bytes_read - (newline_pos - buffer) - 1);
total_bytes_read -= (newline_pos - buffer) + 1;
}
sleep(1); // 每秒读取一次
}
close(fd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define DEVICE_NAME "/dev/custom_char_dev"
#define MESSAGE "hello device\n"
int main() {
int fd;
ssize_t bytes;
int write_count = 1; // 从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("Wrote: %s [Count: %d]\n", MESSAGE, write_count++); // 输出编号
sleep(1); // 每秒写入一次
}
close(fd);
return 0;
}