在网上看到的,讲的不错,遂转载过来。原文地址:http://blog.chinaunix.net/uid-25014876-id-62725.html
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
异步通知fasync是应用于系统调用signal和sigaction函数,下面我会使用signal函数。简单的说,signal函数就是让一个信号与与一个函数对应,没当接收到这个信号就会调用相应的函数。
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
一、什么是异步通知
个人认为,异步通知类似于中断的机制,如下面的将要举例的程序,当设备可写时,设备驱动函数发送一个信号给内核,告知内核有数据可读,在条件不满足之前,并不会造成阻塞。而不像之前学的阻塞型IO和poll,它们是调用函数进去检查,条件不满足时还会造成阻塞。
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
二、应用层中启用异步通知机制
其实就三个步骤:
1)signal(SIGIO, sig_handler);
调用signal函数,让指定的信号SIGIO与处理函数sig_handler对应。
2)fcntl(fd, F_SET_OWNER, getpid());
指定一个进程作为文件的“属主(filp->owner)”,这样内核才知道信号要发给哪个进程。
3)f_flags = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, f_flags | FASYNC);
在设备文件中添加FASYNC标志,驱动中就会调用将要实现的test_fasync函数。
三个步骤执行后,一旦有信号产生,相应的进程就会收到。
来个应用程序:
/*3rd_char_7/1st/app/monitor.c*/ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/select.h> #include <unistd.h> #include <signal.h> unsigned int flag; void sig_handler(int sig) { printf("<app>%s\n", __FUNCTION__); flag = 1; } int main(void) { char buf[20]; int fd; int f_flags; flag = 0; fd = open("/dev/test", O_RDWR); if(fd < 0) { perror("open"); return -1; } /*三个步骤*/ signal(SIGIO, sig_handler); fcntl(fd, F_SETOWN, getpid()); f_flags = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, FASYNC | f_flags); while(1) { printf("waiting \n"); //在还没收到信号前,程序还在不停的打印 sleep(4); if(flag) break; } read(fd, buf, 10); printf("finish: read[%s]\n", buf); close(fd); return 0; }
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
三、驱动中需要实现的异步通知
上面说的三个步骤,内核已经帮忙实现了前两个步骤,只需要我们稍稍实现第三个步骤的一个简单的传参。
实现异步通知,内核需要知道几个东西:哪个文件(filp),什么信号(SIGIIO),发给哪个进程(pid),收到信号后做什么(sig_handler)。这些都由前两个步骤完成了。
回想一下,在实现等待队列中,我们需要将一个等待队列wait_queue_t添加到指定的等待队列头wait_queue_head_t中。
在这里,同样需要把一个结构体struct fasync_struct添加到内核的异步队列头(名字是我自己取的)中。这个结构体用来存放对应设备文件的信息(如fd, filp)并交给内核来管理。一但收到信号,内核就会在这个所谓的异步队列头找到相应的文件(fd),并在filp->owner中找到对应的进程PID,并且调用对应的sig_handler了。
看一下fasync_struct
struct fasync_struct { int magic; int fa_fd; struct fasync_struct *fa_next; /* singly linked list */ //一看就觉得他是链表 struct file *fa_file; };
上面说所的步骤都是由内核来完成,我们只要做两件事情:
1)定义结构体fasync_struct。
struct fasync_struct *async_queue;
2)实现test_fasync,把函数fasync_helper将fd,filp和定义的结构体传给内核。
int test_fasync (int fd, struct file *filp, int mode) { struct _test_t *dev = filp->private_data; return fasync_helper(fd, filp, mode, &dev->async_queue); }
讲一下函数fasync_helper:
int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
一看就知道,前面的三个参数其实就是teat_fasync的三个参数,只要我们定义号的fasync_struct结构体也传进去就可以了。内核会完成我上面红色自己所说的事情。
另外还有两件事:
3)当设备可写时,调用函数kill_fasync发送信号SIGIO给内核。
if (dev->async_queue){ kill_fasync(&dev->async_queue, SIGIO, POLL_IN); }
讲解一下这个函数:
void kill_fasync(struct fasync_struct **fp, int sig, int band)
sig就是我们要发送的信号。
band(带宽),一般都是使用POLL_IN,表示设备可读,如果设备可写,使用POLL_OUT
4)当设备关闭时,需要将fasync_struct从异步队列中删除:
test_fasync(-1, filp, 0);
删除也是调用test_fasync,不过改了一下参数而已。
既然说完了就上程序:上面的函数需要包含<linux/fs.h>
/*3rd_char_7/1st/test.c*/ struct _test_t{ char kbuf[DEV_SIZE]; unsigned int major; unsigned int minor; unsigned int cur_size; dev_t devno; struct cdev test_cdev; wait_queue_head_t test_queue; wait_queue_head_t read_queue; wait_queue_head_t write_queue; struct fasync_struct *async_queue; //1.定义结构体 }; !!J÷浴!!!? ssize_t test_write(struct file *filp, const char __user *buf, size_t count, loff_t *offset) { int ret; struct _test_t *dev = filp->private_data; if(copy_from_user(dev->kbuf, buf, count)){ ret = - EFAULT; }else{ ret = count; dev->cur_size += count; P_DEBUG("write %d bytes, cur_size:[%d]\n", count, dev->cur_size); P_DEBUG("kbuf is [%s]\n", dev->kbuf); wake_up_interruptible(&dev->test_queue); wake_up_interruptible(&dev->read_queue); if (dev->async_queue){ kill_fasync(&dev->async_queue, SIGIO, POLL_IN); //3.可写时发送信号 } } return ret; //返回实际写入的字节数或错误号 } !!J÷浴!!!? int test_fasync (int fd, struct file *filp, int mode) //2.实现test_fasync { struct _test_t *dev = filp->private_data; return fasync_helper(fd, filp, mode, &dev->async_queue); } int test_close(struct inode *node, struct file *filp) { test_fasync(-1, filp, 0); //4文件关闭时将结构体从伊部队列中删除 return 0; } struct file_operations test_fops = { .open = test_open, .release = test_close, .write = test_write, .read = test_read, .poll = test_poll, .fasync = test_fasync, //此步骤切记 }; .。。。。。。
程序写完了就得验证一下:
[root: app]# insmod ../test.ko major[253] minor[0] hello kernel [root: app]# mknod /dev/test c 253 0 [root: app]# ./monitor& //后台运行monitor waiting [root: app]# waiting //不停的打印,没有休眠 waiting waiting waiting waiting waiting [root: app]# ./app_write //调用函数写数据, <kernel>[test_write]write 8 bytes, cur_size:[8] <kernel>[test_write]kbuf is [techbulo] <app>s<kernel>[test_read]read data..... //写完后minoter接收到信号,跳出循环读数据 <kernel>[test_read]read 8 bytes, cur_size:[0] ig_handler //这是在sig_hanler里面打印的,本应出现在读函数之前,因为各个函数抢着打印,所以,出现了乱序,不过不影响验证。 finish: read[techbulo] [1] + Done ./monitor
贴张图总结一下:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
四、阻塞型IO、poll和异步通知的区别:
宋宝华书上的图,描述的挺好的:图片不态清晰,将就一下。
一个最重要的区别:
1)异步通知是不会造成阻塞的。
2)调用阻塞IO时如果条件不满足,会在驱动函数中的test_read或test_write中阻塞。
3)如果条件不满足,selcet会在系统调用中阻塞。
所谓的异步,就是进程可以在信号没到前干别的事情,等到信号到来了,进程就会被内核通知去做相应的信号操作。进程是不知道信号什么时候来的。
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
五,总结
今天只是讲了异步通知在内核中的实现,并且对应的应用函数和驱动函数需要做什么事情。最后总结了一下阻塞IO、poll和异步通知的区别。
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx