Linux驱动中completion接口浅析(wait_for_complete例子,很好)
completion是一种轻量级的机制,它允许一个线程告诉另一个线程工作已经完成。可以利用下面的宏静态创建completion: DECLARE_COMPLETION(my_completion);
如果运行时创建completion,则必须采用以下方法动态创建和初始化: struct compltion my_completion; init_completion(&my_completion);
completion的相关定义包含在kernel/include/Linux/completion.h中:
struct completion { unsigned int done; wait_queue_head_t wait; };
#define COMPLETION_INITIALIZER(work) / { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
#define DECLARE_COMPLETION(work) / struct completion work = COMPLETION_INITIALIZER(work)
static inline void init_completion(struct completion *x) { x->done = 0; init_waitqueue_head(&x->wait); }
要等待completion,可进行如下调用: void wait_for_completion(struct completion *c);
触发completion事件,调用: void complete(struct completion *c); //唤醒一个等待线程 void complete_all(struct completion *c);//唤醒所有的等待线程
为说明completion的使用方法,将《Linux设备驱动程序》一书中的complete模块的代码摘抄如下: /* * complete.c -- the writers awake the readers * * Copyright (C) 2003 Alessandro Rubini and Jonathan Corbet * Copyright (C) 2003 O'Reilly & Associates * * The source code in this file can be freely used, adapted, * and redistributed in source or binary form, so long as an * acknowledgment appears in derived source files. The citation * should list that the code comes from the book "Linux Device * Drivers" by Alessandro Rubini and Jonathan Corbet, published * by O'Reilly & Associates. No warranty is attached; * we cannot take responsibility for errors or fitness for use. * * $Id: complete.c,v 1.2 2004/09/26 07:02:43 gregkh Exp $ */
#include <linux/module.h> #include <linux/init.h>
#include <linux/sched.h> /* current and everything */ #include <linux/kernel.h> /* printk() */ #include <linux/fs.h> /* everything... */ #include <linux/types.h> /* size_t */ #include <linux/completion.h>
MODULE_LICENSE("Dual BSD/GPL");
static int complete_major = 253;//指定主设备号
DECLARE_COMPLETION(comp);
ssize_t complete_read (struct file *filp, char __user *buf, size_t count, loff_t *pos) { printk(KERN_DEBUG "process %i (%s) going to sleep/n", current->pid, current->comm); wait_for_completion(&comp); printk(KERN_DEBUG "awoken %i (%s)/n", current->pid, current->comm); return 0; /* EOF */ }
ssize_t complete_write (struct file *filp, const char __user *buf, size_t count, loff_t *pos) { printk(KERN_DEBUG "process %i (%s) awakening the readers.../n", current->pid, current->comm); complete(&comp); return count; /* succeed, to avoid retrial */ }
struct file_operations complete_fops = { .owner = THIS_MODULE, .read = complete_read, .write = complete_write, };
int complete_init(void) { int result;
/* * Register your major, and accept a dynamic number */ result = register_chrdev(complete_major, "complete", &complete_fops); if (result < 0) return result; if (complete_major == 0) complete_major = result; /* dynamic */ return 0; }
void complete_cleanup(void) { unregister_chrdev(complete_major, "complete"); }
module_init(complete_init); module_exit(complete_cleanup);
该模块定义了一个简单的completion设备:任何试图从该设备中读取的进程都将等待,直到其他设备写入该设备为止。编译此模块的Makefile如下: obj-m := complete.o KDIR := /lib/modules/$(Shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) M=$(PWD) modules clean: rm -f *.ko *.o *.mod.c
在linux终端中执行以下命令,编译生成模块,并进行动态加载。 #make #mknod completion c 253 0 #insmod complete.ko 再打开三个终端,一个用于读进程: #cat completion 一个用于写进程: #echo >completion 另一个查看系统日志: #tail -f /var/log/messages
值得注意的是,当我们使用的complete_all接口时,如果要重复使用一个completion结构,则必须执行 INIT_COMPLETION(struct completion c)来重新初始化它。可以在kernel/include/linux/completion.h中找到这个宏的定义: #define INIT_COMPLETION(x) ((x).done = 0)
以下代码对书中原有的代码进行了一番变动,将唤醒接口由原来的complete换成了complete_all,并且为了重复利用completion结构,所有读进程都结束后就重新初始化completion结构,具体代码如下: #include <linux/module.h> #include <linux/init.h>
#include <linux/sched.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/types.h> #include <linux/completion.h>
MODULE_LICENSE("Dual BSD/GPL");
#undef KERN_DEBUG #define KERN_DEBUG "<1>"
static int complete_major=253; static int reader_count = 0;
DECLARE_COMPLETION(comp);
ssize_t complete_read (struct file *filp,char __user *buf,size_t count,loff_t *pos) { printk(KERN_DEBUG "process %i (%s) going to sleep,waiting for writer/n",current->pid,current->comm); reader_count++; printk(KERN_DEBUG "In read ,before comletion: reader count = %d /n",reader_count); wait_for_completion(&comp); reader_count--; printk(KERN_DEBUG "awoken %s (%i) /n",current->comm,current->pid); printk(KERN_DEBUG "In read,after completion : reader count = %d /n",reader_count);
/*如果使用complete_all,则completion结构只能用一次,再次使用它时必须调用此宏进行重新初始化*/ if(reader_count == 0) INIT_COMPLETION(comp);
return 0; }
ssize_t complete_write(struct file *filp,const char __user *buf,size_t count,loff_t *pos) { printk(KERN_DEBUG "process %i (%s) awoking the readers.../n",current->pid,current->comm); printk(KERN_DEBUG "In write ,before do complete_all : reader count = %d /n",reader_count);
if(reader_count != 0) complete_all(&comp);
printk(KERN_DEBUG "In write ,after do complete_all : reader count = %d /n",reader_count);
return count; }
struct file_operations complete_fops={ .owner = THIS_MODULE, .read = complete_read, .write = complete_write, };
int complete_init(void) { int result;
result=register_chrdev(complete_major,"complete",&complete_fops); if(result<0) return result; if(complete_major==0) complete_major =result;
printk(KERN_DEBUG "complete driver test init! complete_major=%d/n",complete_major); printk(KERN_DEBUG "静态初始化completion/n");
return 0; }
void complete_exit(void) { unregister_chrdev(complete_major,"complete"); printk(KERN_DEBUG "complete driver is removed/n"); }
module_init(complete_init); module_exit(complete_exit);
这里测试步骤和上述一样,只不过需要多打开几个终端来执行多个进程同时读操作。
Linux驱动中completion接口浅析(wait_for_complete例子,很好)的更多相关文章
- Linux驱动中completion接口浅析(wait_for_complete例子,很好)【转】
转自:http://blog.csdn.net/batoom/article/details/6298267 completion是一种轻量级的机制,它允许一个线程告诉另一个线程工作已经完成.可以利用 ...
- Linux驱动中的platform总线分析
copy from :https://blog.csdn.net/fml1997/article/details/77622860 概述 从Linux2.6内核起,引入一套新的驱动管理和注册机制:pl ...
- Linux驱动中的EPROBE_DEFER是个啥
Linux kernel 驱动中,有不少驱动会引用到 EPROBE_DEFER 这个错误号.比如下面这个例子,对 devm_gpiod_get 的返回值进行判断,如果有错误且错误号不是 -EPRBO ...
- 嵌入式Linux驱动笔记(十八)------浅析V4L2框架之ioctl【转】
转自:https://blog.csdn.net/Guet_Kite/article/details/78574781 权声明:本文为 风筝 博主原创文章,未经博主允许不得转载!!!!!!谢谢合作 h ...
- linux驱动中printk的使用注意事项
今天在按键驱动中增加printk(KERN_INFO "gpio_keys_gpio_isr()\n");在驱动加载阶段可以输出调试信息,但驱动加载起来后的信息,在串口端看不到输出 ...
- 为什么linux驱动中变量或者函数都用static修饰?(知乎问题)
static定义的全局变量 或函数也只能作用于当前的文件. 世界硬件厂商太多,定义static为了防止变量或 函数 重名,定义成static, 就算不同硬件驱动中的 变更 或函数重名了也没关系 .
- Linux内核中的常用宏container_of其实很简单【转】
转自:http://blog.csdn.net/npy_lp/article/details/7010752 开发平台:Ubuntu11.04 编 译器:gcc version 4.5.2 (Ubun ...
- Linux驱动中常用的宏
.module_i2c_driver(adxl34x_driver)展开为 static int __int adxl34x_driver_init(void) { return i2c_regist ...
- Linux驱动中获取系统时间
最近在做VoIP方面的驱动,总共有16个FXS口和FXO口依次初始化,耗用的时间较多.准备将其改为多线程,首先需要确定哪个环节消耗的时间多,这就需要获取系统时间. #include <linux ...
随机推荐
- Beta版本讨论
目录 组员:胡绪佩 组员:何家伟 组员:黄鸿杰 组员: 翟丹丹 组员:周政演 组员:胡青元 组员:庄卉 组员:刘恺琳 组员:何宇恒 组员:刘一好 组员:葛家灿 组员:胡绪佩 总结 通过这次的Beta版 ...
- python文本替换
file_data = '' str1 = ' str2 = ' with open(loginfofile, 'r+') as f: #打开文件,r+模式,读取 for line in f: if ...
- [转帖]VMware Vsphere 6.0安装部署 (一) 总体部署架构
(一)总体部署架构本教程用于学习目的,力求详尽的介绍安装部署过程和各组件之间的关系,部署过程从最简单的模型开始,系列文章按时间顺序依次展开,每篇介绍一个组件. 开始阶段,按照一台物理服务器,部署所有V ...
- 深入理解Java反射+动态代理
答: 反射机制的定义: 是在运行状态中,对于任意的一个类,都能够知道这个类的所有属性和方法,对任意一个对象都能够通过反射机制调用一个类的任意方法,这种动态获取类信息及动态调用类对象方法的功能称为j ...
- CF 1070J Streets and Avenues in Berhattan
DP的数组f其实开得不够大,应该开200000,但是它在cf上就是过了... 题意是把一堆字母分别分配到行和列. 分析一下,答案实际上只和n行中和m列中每种字母分配的个数有关.而且答案只和" ...
- Simple Cycles Edges CodeForces - 962F(点双连通分量)
题意: 求出简单环的所有边,简单环即为边在一个环内 解析: 求出点双连通分量,如果一个连通分量的点数和边数相等,则为一个简单环 点双连通分量 任意两个点都至少存在两条点不重复的路径 即任意两条边都 ...
- 洛谷P1450 [HAOI2008]硬币购物(背包问题,容斥原理)
洛谷题目传送门 我实在是太弱了,第一次正儿八经写背包DP,第一次领会如此巧妙的容斥原理的应用...... 对每次询问都做一遍多重背包,显然T飞,就不考虑了 关键就在于每次询问如何利用重复的信息 我这么 ...
- 【BZOJ1044】[HAOI2008]木棍分割(动态规划,贪心)
[BZOJ1044][HAOI2008]木棍分割(动态规划,贪心) 题面 BZOJ 洛谷 题解 第一问随便二分一下就好了,贪心\(check\)正确性显然. 第二问随便前缀和+单调队列优化一下\(dp ...
- IE条件注释判断
相信大家都知道IE有专门的注释条件判断来引入一些css.js.html代码,但是语法有点拗口,记不住,下面我来做一下笔记: 正常的html注释: <!--注释注释注释注释...--> 注释 ...
- day63_SpringMVC学习笔记_01
1.JAVAEE体系结构 JAVAEE体系结构图如下所示: 2.什么是springmvc? 什么是mvc? Model1 Model2 SpringMVC是什么? SpringMVC是一个web层mv ...