9_1_异步通知

异步通知:即一旦设备就绪,则主动通知应用程序,这样应用程序就不用查询设备状态。
一个进程收到一个信号,与处理器收到一个中断类似。
信号是异步的,一个进程不必通过任何操作来等待信号到达。
阻塞I/O:一直等待设备可访问后再访问
非阻塞I/O中使用poll():查询设备是否可访问。
异步通知:设备通知自身可访问,实现了异步I/O
这三中方式I/O可以互为补充。
1.linux 信号
linux支持使用信号进行进程间通信(IPC)。
在用户程序中,为了捕获信号,可以用signal()函数来设置对应信号的处理函数。

void (*signal(int signum, void(*handler)(int)))(int);
这个较难理解,它可以分解为
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
第一个参数指定信号的值,第二个参数是对应信号值的处理函数。
如果signal()调用成功,它返回最后一次信号signum绑定的处理函数handler值,失败则返回SIG_ERR.
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); 
用于改变进程接收到特定信号后的行为。
第一个参数是信号值,第二个参数是对特定信号的处理函数,第三个参数是原来相应信号的处理函数。
int fasync_helper(int fd, struct file *filp, int mode, struct fasync_struct **fa);  
处理FASYNC标志变更的函数。
void kill_fasync(struct fasync_struct **fa, int sig, int band);  
释放信号用的函数。
globalfifo_async.c

1 /*======================================================================
  2 A globalfifo driver as an example of char device drivers 
  3 This example is to introduce asynchronous notifier
  4 
  5 The initial developer of the original code is Baohua Song
  6 <author@linuxdriver.cn>. All Rights Reserved.
  7 ======================================================================*/
  8 #include <linux/module.h>
  9 #include <linux/types.h>
 10 #include <linux/fs.h>
 11 #include <linux/errno.h>
 12 #include <linux/mm.h>
 13 #include <linux/sched.h>
 14 #include <linux/init.h>
 15 #include <linux/cdev.h>
 16 #include <asm/io.h>
 17 #include <asm/system.h>
 18 #include <asm/uaccess.h>
 19 #include <linux/poll.h>
 20 
 21 #define GLOBALFIFO_SIZE 0x1000 /*全局fifo最大4K字节*/
 22 #define FIFO_CLEAR 0x1 /*清0全局内存的长度*/
 23 #define GLOBALFIFO_MAJOR 249 /*预设的globalfifo的主设备号*/
 24 
 25 static int globalfifo_major = GLOBALFIFO_MAJOR;
 26 /*globalfifo设备结构体*/
 27 struct globalfifo_dev
 28 {
 29 struct cdev cdev; /*cdev结构体*/
 30 unsigned int current_len; /*fifo有效数据长度*/
 31 unsigned char mem[GLOBALFIFO_SIZE]; /*全局内存*/
 32 struct semaphore sem; /*并发控制用的信号量*/
 33 wait_queue_head_t r_wait; /*阻塞读用的等待队列头*/
 34 wait_queue_head_t w_wait; /*阻塞写用的等待队列头*/
 35 struct fasync_struct *async_queue; /* 异步结构体指针,用于读 */
 36 };
 37 
 38 struct globalfifo_dev *globalfifo_devp; /*设备结构体指针*/
 39 /*文件打开函数*/
 40 int globalfifo_open(struct inode *inode, struct file *filp)
 41 {
 42 /*将设备结构体指针赋值给文件私有数据指针*/
 43 filp->private_data = globalfifo_devp;
 44 return 0;
 45 }
 46 /*文件释放函数*/
 47 int globalfifo_release(struct inode *inode, struct file *filp)
 48 {
 49 /* 将文件从异步通知列表中删除 */
 50 globalfifo_fasync( - 1, filp, 0);
 51 
 52 return 0;
 53 }
 54 
 55 /* ioctl设备控制函数 */
 56 static int globalfifo_ioctl(struct inode *inodep, struct file *filp, unsigned
 57 int cmd, unsigned long arg)
 58 {
 59 struct globalfifo_dev *dev = filp->private_data;/*获得设备结构体指针*/
 60 
 61 switch (cmd)
 62 {
 63 case FIFO_CLEAR:
 64 down(&dev->sem); //获得信号量 

 65 dev->current_len = 0;
 66 memset(dev->mem,0,GLOBALFIFO_SIZE);
 67 up(&dev->sem); //释放信号量

 68 
 69 printk(KERN_INFO "globalfifo is set to zero\n");
 70 break;
 71 
 72 default:
 73 return - EINVAL;
 74 }
 75 return 0;
 76 }
 77 
 78 static unsigned int globalfifo_poll(struct file *filp, poll_table *wait)
 79 {
 80 unsigned int mask = 0;
 81 struct globalfifo_dev *dev = filp->private_data; /*获得设备结构体指针*/
 82 
 83 down(&dev->sem);
 84 
 85 poll_wait(filp, &dev->r_wait, wait);
 86 poll_wait(filp, &dev->w_wait, wait);
 87 /*fifo非空*/
 88 if (dev->current_len != 0)
 89 {
 90 mask |= POLLIN | POLLRDNORM; /*标示数据可获得*/
 91 }
 92 /*fifo非满*/
 93 if (dev->current_len != GLOBALFIFO_SIZE)
 94 {
 95 mask |= POLLOUT | POLLWRNORM; /*标示数据可写入*/
 96 }
 97 
 98 up(&dev->sem);
 99 return mask;
100 }
101 
102 /* globalfifo fasync函数*/
103 static int globalfifo_fasync(int fd, struct file *filp, int mode)
104 {
105 struct globalfifo_dev *dev = filp->private_data;
106 return fasync_helper(fd, filp, mode, &dev->async_queue);
107 }
108 
109 
110 /*globalfifo读函数*/
111 static ssize_t globalfifo_read(struct file *filp, char __user *buf, size_t count,
112 loff_t *ppos)
113 {
114 int ret;
115 struct globalfifo_dev *dev = filp->private_data; //获得设备结构体指针

116 DECLARE_WAITQUEUE(wait, current); //定义等待队列

117 
118 down(&dev->sem); //获得信号量

119 add_wait_queue(&dev->r_wait, &wait); //进入读等待队列头

120 
121 /* 等待FIFO非空 */
122 if (dev->current_len == 0)
123 {
124 if (filp->f_flags &O_NONBLOCK)
125 {
126 ret = - EAGAIN;
127 goto out;
128 }
129 __set_current_state(TASK_INTERRUPTIBLE); //改变进程状态为睡眠

130 up(&dev->sem);
131 
132 schedule(); //调度其他进程执行

133 if (signal_pending(current))
134 //如果是因为信号唤醒

135 {
136 ret = - ERESTARTSYS;
137 goto out2;
138 }
139 
140 down(&dev->sem);
141 }
142 
143 /* 拷贝到用户空间 */
144 if (count > dev->current_len)
145 count = dev->current_len;
146 
147 if (copy_to_user(buf, dev->mem, count))
148 {
149 ret = - EFAULT;
150 goto out;
151 }
152 else
153 {
154 memcpy(dev->mem, dev->mem + count, dev->current_len - count); //fifo数据前移

155 dev->current_len -= count; //有效数据长度减少

156 printk(KERN_INFO "read %d bytes(s),current_len:%d\n", count, dev->current_len);
157 
158 wake_up_interruptible(&dev->w_wait); //唤醒写等待队列

159 
160 ret = count;
161 }
162 out: up(&dev->sem); //释放信号量

163 out2:remove_wait_queue(&dev->w_wait, &wait); //从附属的等待队列头移除

164 set_current_state(TASK_RUNNING);
165 return ret;
166 }
167 
168 
169 /*globalfifo写操作*/
170 static ssize_t globalfifo_write(struct file *filp, const char __user *buf,
171 size_t count, loff_t *ppos)
172 {
173 struct globalfifo_dev *dev = filp->private_data; //获得设备结构体指针

174 int ret;
175 DECLARE_WAITQUEUE(wait, current); //定义等待队列

176 
177 down(&dev->sem); //获取信号量

178 add_wait_queue(&dev->w_wait, &wait); //进入写等待队列头

179 
180 /* 等待FIFO非满 */
181 if (dev->current_len == GLOBALFIFO_SIZE)
182 {
183 if (filp->f_flags &O_NONBLOCK)
184 //如果是非阻塞访问

185 {
186 ret = - EAGAIN;
187 goto out;
188 }
189 __set_current_state(TASK_INTERRUPTIBLE); //改变进程状态为睡眠

190 up(&dev->sem);
191 
192 schedule(); //调度其他进程执行

193 if (signal_pending(current))
194 //如果是因为信号唤醒

195 {
196 ret = - ERESTARTSYS;
197 goto out2;
198 }
199 
200 down(&dev->sem); //获得信号量

201 }
202 
203 /*从用户空间拷贝到内核空间*/
204 if (count > GLOBALFIFO_SIZE - dev->current_len)
205 count = GLOBALFIFO_SIZE - dev->current_len;
206 
207 if (copy_from_user(dev->mem + dev->current_len, buf, count))
208 {
209 ret = - EFAULT;
210 goto out;
211 }
212 else
213 {
214 dev->current_len += count;
215 printk(KERN_INFO "written %d bytes(s),current_len:%d\n", count, dev
216 ->current_len);
217 
218 wake_up_interruptible(&dev->r_wait); //唤醒读等待队列

219 /* 产生异步读信号 */
220 if (dev->async_queue)
221 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
222 
223 ret = count;
224 }
225 
226 out: up(&dev->sem); //释放信号量

227 out2:remove_wait_queue(&dev->w_wait, &wait); //从附属的等待队列头移除

228 set_current_state(TASK_RUNNING);
229 return ret;
230 }
231 
232 
233 /*文件操作结构体*/
234 static const struct file_operations globalfifo_fops =
235 {
236 .owner = THIS_MODULE,
237 .read = globalfifo_read,
238 .write = globalfifo_write,
239 .ioctl = globalfifo_ioctl,
240 .poll = globalfifo_poll,
241 .open = globalfifo_open,
242 .release = globalfifo_release,
243 .fasync = globalfifo_fasync,
244 };
245 
246 /*初始化并注册cdev*/
247 static void globalfifo_setup_cdev(struct globalfifo_dev *dev, int index)
248 {
249 int err, devno = MKDEV(globalfifo_major, index);
250 
251 cdev_init(&dev->cdev, &globalfifo_fops);
252 dev->cdev.owner = THIS_MODULE;
253 dev->cdev.ops = &globalfifo_fops;
254 err = cdev_add(&dev->cdev, devno, 1);
255 if (err)
256 printk(KERN_NOTICE "Error %d adding LED%d", err, index);
257 }
258 
259 /*设备驱动模块加载函数*/
260 int globalfifo_init(void)
261 {
262 int ret;
263 dev_t devno = MKDEV(globalfifo_major, 0);
264 
265 /* 申请设备号*/
266 if (globalfifo_major)
267 ret = register_chrdev_region(devno, 1, "globalfifo");
268 else /* 动态申请设备号 */
269 {
270 ret = alloc_chrdev_region(&devno, 0, 1, "globalfifo");
271 globalfifo_major = MAJOR(devno);
272 }
273 if (ret < 0)
274 return ret;
275 /* 动态申请设备结构体的内存*/
276 globalfifo_devp = kmalloc(sizeof(struct globalfifo_dev), GFP_KERNEL);
277 if (!globalfifo_devp) /*申请失败*/
278 {
279 ret = - ENOMEM;
280 goto fail_malloc;
281 }
282 
283 memset(globalfifo_devp, 0, sizeof(struct globalfifo_dev));
284 
285 globalfifo_setup_cdev(globalfifo_devp, 0);
286 
287 init_MUTEX(&globalfifo_devp->sem); /*初始化信号量*/
288 init_waitqueue_head(&globalfifo_devp->r_wait); /*初始化读等待队列头*/
289 init_waitqueue_head(&globalfifo_devp->w_wait); /*初始化写等待队列头*/
290 
291 return 0;
292 
293 fail_malloc: unregister_chrdev_region(devno, 1);
294 return ret;
295 }
296 
297 
298 /*模块卸载函数*/
299 void globalfifo_exit(void)
300 {
301 cdev_del(&globalfifo_devp->cdev); /*注销cdev*/
302 kfree(globalfifo_devp); /*释放设备结构体内存*/
303 unregister_chrdev_region(MKDEV(globalfifo_major, 0), 1); /*释放设备号*/
304 }
305 
306 MODULE_AUTHOR("Song Baohua");
307 MODULE_LICENSE("Dual BSD/GPL");
308 
309 module_param(globalfifo_major, int, S_IRUGO);
310 
311 module_init(globalfifo_init);
312 module_exit(globalfifo_exit);


这就是支持异步通知的globalfifo_async.ko驱动的源码,很简单。
编译会报错:
得加上
#include <linux/slab.h>
另外,globalfifo_fasync()声明 要移到 golbalfifo_release()调用它之前.
用户空间验证异步通知程序 asyncmonitor.c

1 /*======================================================================
  2 A test program to access /dev/second
  3 This example is to help understand async IO 
  4 
  5 The initial developer of the original code is Baohua Song
  6 <author@linuxdriver.cn>. All Rights Reserved.
  7 ======================================================================*/
  8 #include <sys/types.h>
  9 #include <sys/stat.h>
 10 #include <stdio.h>
 11 #include <fcntl.h>
 12 #include <signal.h>
 13 #include <unistd.h>
 14 
 15 /*接收到异步读信号后的动作*/
 16 void input_handler(int signum)
 17 {
 18 printf("receive a signal from globalfifo,signalnum:%d\n",signum);
 19 }
 20 
 21 main()
 22 {
 23 int fd, oflags;
 24 fd = open("/dev/globalfifo", O_RDWR, S_IRUSR | S_IWUSR);
 25 if (fd != - 1)
 26 {
 27 //启动信号驱动机制

 28 signal(SIGIO, input_handler); //让input_handler()处理SIGIO信号

 29 fcntl(fd, F_SETOWN, getpid());
 30 oflags = fcntl(fd, F_GETFL);
 31 fcntl(fd, F_SETFL, oflags | FASYNC);
 32 while(1)
 33 {
 34 sleep(100);
 35 }
 36 }
 37 else
 38 {
 39 printf("device open failure\n");
 40 }
 41 }

xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/9_async_signal_aio$ sudo insmod globalfifo_async.ko
xxha@xxha-OptiPlex-780:~/share/learning/songbaohua/9_async_signal_aio$ ./asyncmonitor
xxha@xxha-OptiPlex-780:/dev$ sudo echo 0 > globalfifo
receive a signal from globalfifo,signalnum:29
会自动弹出信息,即调用input_handler()了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值