今天我们的系统分为3部分: 1 是驱动模块部分,2是应用部分的读设备文件,打开驱动模块挂载的文件进行poll调用,如果没有数据可以读,则阻塞,等待数据可读。3 是对设备文件写入, 就是每隔一段时间往这个驱动模块挂载的文件里面写内容,然后看poll部分的反应。
下面是我们的驱动模块部分,主要是2个地方,1 是写入部分,只要文件有写入,我们就唤醒等待队列。2是poll部分,调用poll_wait()方法将我们驱动模块本地的等待队列提供给内核,内核就会监听我们这个等待队列,一旦我们有数据写入,我们就会唤醒这个等待队列,那么系统的poll就被唤醒,然后poll就会开始循环,然后来调用我们的poll接口,我们的poll接口会发现有数据可读,然后上层就可以读数据了。
驱动部分的代码如下:
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/bug.h> /* For BUG_ON. */
#include <linux/cpu.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <linux/delay.h>
#include <linux/smp.h>
#include <linux/kernel_stat.h>
#include <linux/sched.h>
#include <linux/percpu-defs.h>
#include <linux/wait.h>
#include <linux/gpio/driver.h>
#include <linux/atomic.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/kfifo.h>
#define MY_MAX_MINORS 1
#define DEVICE_NAME "device name 500"
DEFINE_KFIFO(myfifo, char, 1024);
struct my_device_data {
struct cdev cdev;
};
DECLARE_WAIT_QUEUE_HEAD(wq);
static struct my_device_data devs[MY_MAX_MINORS];
static int my_open(struct inode *inode, struct file *file) {
pr_info("a3 my_open \n");
return 0;
}
static ssize_t my_write(struct file *filp, const char __user *user_buffer,
size_t size, loff_t *offset) {
int ret;
unsigned int len = 0;
pr_info("write");
ret = kfifo_from_user(&myfifo, user_buffer, size, &len);
if (ret != 0) {
pr_err("kfifo_from_user error");
return 0;
}
if (len <= 0)
return 0;
*offset += len;
wake_up(&wq);
return len;
}
static ssize_t my_read(struct file *filp, char __user *user_buffer,
size_t count, loff_t *offset) {
int ret;
unsigned int len = 0;
pr_info("read");
ret = kfifo_to_user(&myfifo, user_buffer, count, &len);
if (len <= 0)
return 0;
*offset += len;
return len;
}
unsigned int my_poll(struct file *flip, struct poll_table_struct *table) {
int mask = 0;
pr_info("my_poll \n");
poll_wait(flip,&wq,table);
if(kfifo_is_empty(&myfifo)){
} else {
mask |= POLLIN | POLLRDNORM;
}
return mask;
}
static const struct file_operations my_fops = { .owner = THIS_MODULE, .open =
my_open, .read = my_read, .write = my_write,
.poll = my_poll,
};
static dev_t mydev;
static __init int my_init(void) {
int i;
int err;
pr_info("a3 init_module\n");err = alloc_chrdev_region(&mydev, 0, MY_MAX_MINORS, DEVICE_NAME);
if (err != 0) {
return err;
}
for (i = 0; i < MY_MAX_MINORS; i++) {
/* initialize devs[i] fields */
cdev_init(&devs[i].cdev, &my_fops);
cdev_add(&devs[i].cdev, MKDEV(MAJOR(mydev), i)
, 1);
}
return 0;
}
static void __exit my_exit(void) {
int i;
pr_info("a3 cleanup_module\n");for (i = 0; i < MY_MAX_MINORS; i++) {
/* release devs[i] fields */
cdev_del(&devs[i].cdev);
}
unregister_chrdev_region(mydev, MY_MAX_MINORS);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
59行有数据写入的时候,唤醒等待队列。
78行的poll调用的使用,将我们的等待队列提供给内核,这样文件有写入的时候就会通知内核。
下面是应用部分
1 是通过poll来读:
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <poll.h>
static void* calls(void *ptr) {
int ret;
char *buf = (char*) calloc(1024, sizeof(char));
int fd = open("/dev/t01", O_RDWR, O_NONBLOCK);
if (fd < 0) {
perror("open");
printf("error open\n");
exit(EXIT_FAILURE);
}
struct pollfd fds[1];
fds[0].fd = fd;
fds[0].events = POLLIN;
fds[0].revents = 0;
while (1) {
printf("start poll\n");
ret = poll(fds, 1, -1);
if (ret == -1) {
perror("poll test");
return NULL;
}
if (fds[0].revents & POLLIN) {
ret = read(fds[0].fd, buf, 1024);
if (ret < 0) {
perror("read test");
return NULL;
}
buf[ret] = '\0';
printf("read: %s\n", buf);
}
sleep(1);
}
close(fd);
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, calls, NULL);
pthread_join(thread, NULL);
printf("main exit\n");
return EXIT_SUCCESS;
}
这个就是标准的应用使用poll的流程 。 如果我们的驱动文件没有内容可读,那么调用poll之后当前线程会被挂起。
下面就是模拟有数据写入。
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <poll.h>
static void* write01(void *ptr) {
int fd = open("/dev/t01", O_RDWR, O_NONBLOCK);
while(1){
char *buf = "kfifo_from_user - puts some data from user space into the fifo;";
write(fd, buf, strlen(buf));
sleep(1);
}
close(fd);
return NULL;
}
int main() {
pthread_t thread2;
pthread_create(&thread2, NULL, write01, NULL);
pthread_join(thread2, NULL);
printf("main exit\n");
return EXIT_SUCCESS;
}
我们是每隔1秒往这个文件写入字符串 。那么poll调用就被通知有数据可读,然后读完之后又被阻塞,1秒钟后然后又被通知有数据可读,然后一直循环。
所以结合驱动模块里面poll的实现,我们就可以理解系统调用poll的机制 。
欢迎评论。