内核并发poll 加 lock
执行流程
用户空间进程调用write将数据写入设备:执行char_write,更新event_triggered并唤醒等待的进程。有进程因此等待:正在执行的char_read会检查event_triggered,如果为0,执行等待。数据可用时:事件被触发(event_triggered被设为1),之前因等待而被挂起的进程重启,继续执行char_read。
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/poll.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/uaccess.h> #define DEVICE_NAME "epoll_example"
#define CLASS_NAME "epoll_example_class"
static char kbuf[256];
static size_t data_size = 0;
static int major_number;
static struct class* char_class = NULL;
static struct device* char_device = NULL;
static struct cdev cdev;
static wait_queue_head_t read_wait;
static int event_triggered = 0;static DEFINE_SPINLOCK(event_lock);
static int char_open(struct inode *inode, struct file *file) {printk(KERN_INFO "Device opened: PID=%d, INODE=%lx\n", current->pid, (unsigned long)inode->i_ino); return 0;
}
static int char_release(struct inode *inode, struct file *file) {printk(KERN_INFO "Device closed: PID=%d, INODE=%lx\n", current->pid, (unsigned long)inode->i_ino); return 0;
}
static ssize_t char_write(struct file *file, const char __user *buf, size_t len, loff_t *offset) {if (len > sizeof(kbuf) - 1) {return -EINVAL; }if (copy_from_user(kbuf, buf, len)) {return -EFAULT; }kbuf[len] = '\0'; data_size = len; spin_lock(&event_lock); event_triggered = 1; spin_unlock(&event_lock); wake_up_interruptible(&read_wait); return len;
}
static ssize_t char_read(struct file *file, char __user *buf, size_t len, loff_t *offset) {if (!event_triggered) {wait_event_interruptible(read_wait, event_triggered != 0); }if (copy_to_user(buf, kbuf, data_size)) {return -EFAULT; }spin_lock(&event_lock); event_triggered = 0; spin_unlock(&event_lock); printk(KERN_INFO "读取数据完成\n");return data_size;
}
static unsigned int char_poll(struct file *file, poll_table *wait) {unsigned int mask = 0;poll_wait(file, &read_wait, wait);printk(KERN_INFO "记录当前进程加入到等待队列中\n"); if (event_triggered) {mask |= POLLIN | POLLRDNORM; printk(KERN_INFO "记录事件已触发: %u\n", mask); }return mask;
}
static struct file_operations fops = {.owner = THIS_MODULE, .open = char_open, .release = char_release, .write = char_write, .read = char_read, .poll = char_poll,
};
static int __init char_init(void) {major_number = register_chrdev(0, DEVICE_NAME, &fops);if (major_number < 0) {printk(KERN_ALERT "Failed to register a major number\n"); return major_number; }printk(KERN_INFO "记录成功注册的主设备号: %d\n", major_number); char_class = class_create( CLASS_NAME);if (IS_ERR(char_class)) {unregister_chrdev(major_number, DEVICE_NAME); printk(KERN_ALERT "Failed to register device class\n"); return PTR_ERR(char_class); }printk(KERN_INFO "设备类注册成功\n"); char_device = device_create(char_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);if (IS_ERR(char_device)) {class_destroy(char_class); unregister_chrdev(major_number, DEVICE_NAME); printk(KERN_ALERT "Failed to create the device\n"); return PTR_ERR(char_device); }printk(KERN_INFO "设备节点创建成功\n"); cdev_init(&cdev, &fops);cdev_add(&cdev, MKDEV(major_number, 0), 1); init_waitqueue_head(&read_wait); printk(KERN_INFO "驱动初始化成功\n"); return 0;
}
static void __exit char_exit(void) {cdev_del(&cdev); device_destroy(char_class, MKDEV(major_number, 0)); class_unregister(char_class); class_destroy(char_class); unregister_chrdev(major_number, DEVICE_NAME); printk(KERN_INFO "驱动卸载成功\n");
}
module_init(char_init);
module_exit(char_exit); MODULE_LICENSE("GPL");
MODULE_AUTHOR("gopher");
MODULE_DESCRIPTION("A simple character device driver with epoll support");
MODULE_VERSION("1.0");
测试
测试程序说明线程创建:
程序创建了多个线程(NUM_THREADS),每个线程负责向设备写入数据。写入操作:
每个线程会循环执行写入操作(NUM_WRITES次),每次写入一条消息到设备。延迟模拟:
在每次写入后,线程会休眠一小段时间(usleep(1000)),以模拟实际应用中的延迟。线程同步:
主线程会等待所有子线程完成后再结束程序。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>#define DEVICE_PATH "/dev/epoll_example"
#define NUM_THREADS 10
#define NUM_WRITES 100
void *write_to_device(void *threadid) {long tid = (long)threadid; int fd = open(DEVICE_PATH, O_WRONLY); if (fd == -1) {perror("打开设备失败"); pthread_exit(NULL); }char message[256]; for (int i = 0; i < NUM_WRITES; i++) {snprintf(message, sizeof(message), "线程 %ld: 写入 %d\n", tid, i);ssize_t bytes_written = write(fd, message, strlen(message)); if (bytes_written < 0) {perror("写入设备失败"); } else {printf("线程 %ld 写入: %s", tid, message); }usleep(1000); }close(fd); pthread_exit(NULL);
}int main() {pthread_t threads[NUM_THREADS]; int rc; long t; for (t = 0; t < NUM_THREADS; t++) {printf("正在创建线程 %ld\n", t); rc = pthread_create(&threads[t], NULL, write_to_device, (void *)t); if (rc) {printf("错误: pthread_create() 返回代码是 %d\n", rc); exit(-1); }}for (t = 0; t < NUM_THREADS; t++) {pthread_join(threads[t], NULL); }printf("所有线程已完成。\n"); return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>#define DEVICE_PATH "/dev/epoll_example"
#define NUM_THREADS 100
#define NUM_WRITES 1000 void *write_to_device(void *threadid) {long tid = (long)threadid;int fd = open(DEVICE_PATH, O_WRONLY);if (fd == -1) {perror("Failed to open device");pthread_exit(NULL);}char message[256];for (int i = 0; i < NUM_WRITES; i++) {snprintf(message, sizeof(message), "线程 %ld: 写入 %d\n", tid, i + 1);write(fd, message, strlen(message)); }close(fd);pthread_exit(NULL);
}int main() {pthread_t threads[NUM_THREADS]; int rc;struct timeval start, end;gettimeofday(&start, NULL); for (long t = 0; t < NUM_THREADS; t++) {rc = pthread_create(&threads[t], NULL, write_to_device, (void *)t);if (rc) {printf("Error: pthread_create() return code is %d\n", rc);exit(-1);}}for (int t = 0; t < NUM_THREADS; t++) {pthread_join(threads[t], NULL);}gettimeofday(&end, NULL); long seconds = end.tv_sec - start.tv_sec; long microseconds = end.tv_usec - start.tv_usec;long elapsed = seconds * 1000000 + microseconds; printf("所有线程已完成。总耗时: %ld 微秒\n", elapsed);return 0;
}