大多数时候阻塞型和非阻塞型操作的组合以及select方法可以有效地查询设备,但在某些时候效率却不高。通过使用异步通知,应用程序可以在数据可用时收到一个信号,而不需要不断地轮询来关注数据。异步通知通常用于数据可读。
异步通知步骤:
第一步:指定一个进程作为文件的属主,进程使用fcntl系统调用执行F_SETOWN设置属主,让内核知道通知哪个进程。
第二步:用户程序在设备中设置FASYNC标志,通过使用fcntl的F_SETFL设置。
第三步:所有注册为异步通知的进程会被发送一个SIGIO信号。
如果进程中有多于一个文件可以异步通知,则应用程序需要使用poll或select来确定输入来源。
头文件:<linux/fs.h>
接口:
int fasync_helper(int fd, struct file *filp, int mode, struct fasync_struct **fa);
从相关进程的异步通知列表中增加或删除文件。
void kill_fasync(struct fasync_struct **fa, int sig, int band);
数据到达时通知所有的相关进程,sig通常为POLL_IN;如果是针对写入异步通知,sig为POLL_OUT。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/poll.h>
#define MAX_BUF_LEN 256
static int major = 277;
static int minor = 0;
static dev_t helloNum;
struct class *helloClass = NULL;
struct device *helloDev = NULL;
static char helloBuf[MAX_BUF_LEN] = {0};
static struct fasync_struct *helloFasyncQueue = NULL;
static int hello_fasync(int fd, struct file *filp, int mode)
{
return fasync_helper(fd, filp, mode, &helloFasyncQueue);
}
int hello_open(struct inode *pinode, struct file *pfile)
{
printk("hello_open, minor:%d, major:%d\n", iminor(pinode), imajor(pinode));
return 0;
}
int hello_release(struct inode *pinode, struct file *pfile)
{
hello_fasync(-1, pfile, 0);
printk("hello_release, minor:%d, major:%d\n", iminor(pinode), imajor(pinode));
return 0;
}
ssize_t hello_read(struct file *filep, char __user *buf, size_t size, loff_t *pos)
{
int copySize;
if (size > MAX_BUF_LEN)
{
copySize = MAX_BUF_LEN;
}
else
{
copySize = size;
}
if(copy_to_user(buf, helloBuf, copySize))
{
return -EFAULT;
}
printk("read:%s\n",helloBuf);
return copySize;
}
ssize_t hello_write(struct file *filep, const char __user *buf, size_t size, loff_t *pos)
{
int copySize;
if(size > MAX_BUF_LEN)
{
copySize = MAX_BUF_LEN;
}
else
{
copySize = size;
}
memset(helloBuf, 0, MAX_BUF_LEN);
if(copy_from_user(helloBuf, buf, copySize))
{
return -EFAULT;
}
printk("write:%s\n",helloBuf);
if (helloFasyncQueue)
{
kill_fasync(&helloFasyncQueue, SIGIO, POLL_IN);
}
return copySize;
}
static struct file_operations hello_ops = {
.open = hello_open,
.release = hello_release,
.read = hello_read,
.write = hello_write,
.fasync = hello_fasync,
};
static int hello_init(void)
{
int ret = 0;
printk("hello_init\n");
ret = register_chrdev( major, "hello", &hello_ops);
if(ret < 0)
{
printk("register_chrdev failed.\n");
return ret;
}
helloClass = class_create(THIS_MODULE, "hellocls");
if (IS_ERR(helloClass))
{
printk("class_create failed.\n");
ret = PTR_ERR(helloClass);
goto error_exit1;
}
helloNum = MKDEV(major,minor);
printk("major:%d, minor:%d\n", MAJOR(helloNum), MINOR(helloNum));
helloDev = device_create(helloClass, NULL, helloNum, NULL, "hello0");
if (IS_ERR(helloDev))
{
printk("device_create failed.\n");
ret = PTR_ERR(helloDev);
goto error_exit2;
}
return 0;
error_exit2:
class_destroy(helloClass);
error_exit1:
unregister_chrdev(major,"hello");
return ret;
}
static void hello_exit(void)
{
printk("hello_exit\n");
device_destroy(helloClass, helloNum);
class_destroy(helloClass);
unregister_chrdev(major,"hello");
}
MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/select.h>
#include <time.h>
#include <string.h>
#include <signal.h>
int readFd = -1;
void *write_pthread(void *args)
{
int fd = open("/dev/hello0", O_RDWR, 666);
if (fd < 0)
{
perror("open faied");
return NULL;
}
printf("open successed:%d\n", fd);
char buf[32] = {0};
int cnt = 0;
while(1)
{
sleep(5);
memset(buf, 0x0, sizeof(buf));
sprintf(buf, "hello_%d", cnt++);
printf("write:%s\n", buf);
write(fd, buf, strlen(buf));
}
return NULL;
}
void SIGIO_handler(int sig)
{
char buf[32] = {0};
read(readFd, buf, sizeof(buf));
printf("read:%s\n", buf);
}
int main(int argc, char * argv [ ])
{
readFd = open("/dev/hello0", O_RDWR, 666);
if (readFd < 0)
{
perror("open faied");
return -1;
}
printf("open successed:%d\n", readFd);
signal(SIGIO, SIGIO_handler);
fcntl(readFd, F_SETOWN, getpid());
int oflags = fcntl(readFd, F_GETFL);
fcntl(readFd, F_SETFL, oflags | FASYNC);
pthread_t tid;
pthread_create(&tid, NULL, write_pthread, NULL);
while(1)
{
sleep(2);
}
return 0;
}