驱动思想之机制和策略

驱动程序的角色

作为一个程序员, 你能够对你的驱动作出你自己的选择, 并且在所需的编程时间和结果的灵活性之间, 选择一个可接受的平衡. 尽管说一个驱动是"灵活"的, 听起来有些奇怪, 但是我们喜欢这个字眼, 因为它强调了一个驱动程序的角色是提供机制, 而不是策略.

机制和策略的区分是其中一个在 Unix 设计背后的最好观念. 大部分的编程问题其实可以划分为两部分:" 提供什么能力"(机制) "如何使用这些能力"(策略). 如果这两方面由程序的不同部分来表达, 或者甚至由不同的程序共同表达, 软件包是非常容易开发和适应特殊的需求.

例如, 图形显示的 Unix 管理划分为 X 服务器, 它理解硬件以及提供了统一的接口给用户程序, 还有窗口和会话管理器, 它实现了一个特别的策略, 而对硬件一无所知. 人们可以在不同的硬件上使用相同的窗口管理器, 而且不同的用户可以在同一台工作站上运行不同的配置. 甚至完全不同的桌面环境, 例如 KDE 和 GNOME, 可以在同一系统中共存. 另一个例子是 TCP/IP 网络的分层结构: 操作系统提供 socket 抽象层, 它对要传送的数据而言不实现策略, 而不同的服务器负责各种服务( 以及它们的相关策略). 而且, 一个服务器, 例如 ftpd 提供文件传输机制, 同时用户可以使用任何他们喜欢的客户端; 无论命令行还是图形客户端都存在, 并且任何人都能编写一个新的用户接口来传输文件.

在驱动相关的地方, 机制和策略之间的同样的区分都适用. 软驱驱动是不含策略的--它的角色仅仅是将磁盘表现为一个数据块的连续阵列. 系统的更高级部分提供了策略, 例如谁可以存取软驱驱动, 这个软驱是直接存取还是要通过一个文件系统, 以及用户是否可以加载文件系统到这个软驱. 因为不同的环境常常需要不同的使用硬件的方式, 尽可能对策略透明是非常重要的.

在编写驱动时, 程序员应当特别注意这个基础的概念: 编写内核代码来存取硬件, 但是不能强加特别的策略给用户, 因为不同的用户有不同的需求. 驱动应当做到使硬件可用, 将所有关于如何使用硬件的事情留给应用程序. 一个驱动, 这样, 就是灵活的, 如果它提供了对硬件能力的存取, 没有增加约束. 然而, 有时必须作出一些策略的决定. 例如, 一个数字 I/O 驱动也许只提供对硬件的字符存取, 以便避免额外的代码处理单个位.

你也可以从不同的角度看你的驱动: 它是一个存在于应用程序和实际设备间的软件层. 驱动的这种特权的角色允许驱动程序员y严密地选择设备应该如何表现: 不同的驱动可以提供不同的能力, 甚至是同一个设备. 实际的驱动设计应当是在许多不同考虑中的平衡. 例如, 一个单个设备可能由不同的程序并发使用, 驱动程序员有完全的自由来决定如何处理并发性. 你能在设备上实现内存映射而不依赖它的硬件能力, 或者你能提供一个用户库来帮助应用程序员在可用的原语之上实现新策略, 等等. 一个主要的考虑是在展现给用户尽可能多的选项, 和你不得不花费的编写驱动的时间之间做出平衡, 还有需要保持事情简单以避免错误潜入.

对策略透明的驱动有一些典型的特征. 这些包括支持同步和异步操作, 可以多次打开的能力, 利用硬件全部能力, 没有软件层来"简化事情"或者提供策略相关的操作. 这样的驱动不但对他们的最终用户好用, 而且证明也是易写易维护的. 成为策略透明的实际是一个共同的目标, 对软件设计者来说.

许多设备驱动, 确实, 是与用户程序一起发行的, 以便帮助配置和存取目标设备. 这些程序包括简单的工具到完全的图形应用. 例子包括 tunelp 程序, 它调整并口打印机驱动如何操作, 还有图形的 cardctl 工具, 它是 PCMCIA 驱动包的一部分. 经常会提供一个客户库, 它提供了不需要驱动自身实现的功能.

本书的范围是内核, 因此我们尽力不涉及策略问题, 应用程序, 以及支持库. 有时我们谈论不同的策略以及如何支持他们, 但是我们不会进入太多有关使用设备的程序的细节, 或者是他们强加的策略的细节. 但是, 你应当理解, 用户程序是一个软件包的构成部分, 并且就算是对策略透明的软件包在发行时也会带有配置文件, 来对底层的机制应用缺省的动作。

The Role of the Device Driver

As a programmer, you are able to make your own choices about your driver, and choose an acceptable trade-off between the programming time required and the flexibility of the result. Though it may appear strange to say that a driver is “flexible,” we like this word because it emphasizes that the role of a device driver is providing mechanism, notpolicy.

The distinction between mechanism and policy is one of the best ideas behind the Unix design. Most programming problems can indeed be split into two parts: “what capabilities are to be provided” (the mechanism) and “how those capabilities can be used” (the policy). If the two issues are addressed by different parts of the program, or even by different programs altogether, the software package is much easier to develop and to adapt to particular needs.

For example, Unix management of the graphic display is split between the X server, which knows the hardware and offers a unified interface to user programs, and the window and session managers, which implement a particular policy without knowing anything about the hardware. People can use the same window manager on different hardware, and different users can run different configurations on the same workstation. Even completely different desktop environments, such as KDE and GNOME, can coexist on the same system. Another example is the layered structure of TCP/IP networking: the operating system offers the socket abstraction, which implements no policy regarding the data to be transferred, while different servers are in charge of the services (and their associated policies). Moreover, a server like ftpd provides the file transfer mechanism, while users can use whatever client they prefer; both command-line and graphic clients exist, and anyone can write a new user interface to transfer files.

Where drivers are concerned, the same separation of mechanism and policy applies. The floppy driver is policy free—its role is only to show the diskette as a continuous array of data blocks. Higher levels of the system provide policies, such as who may access the floppy drive, whether the drive is accessed directly or via a filesystem, and whether users may mount filesystems on the drive. Since different environments usually need to use hardware in different ways, it’s important to be as policy free as possible.

When writing drivers, a programmer should pay particular attention to this fundamental concept: write kernel code to access the hardware, but don’t force particular policies on the user, since different users have different needs. The driver should deal with making the hardware available, leaving all the issues abouthowto use the hardware to the applications. A driver, then, is flexible if it offers access to the hardware capabilities without adding constraints. Sometimes, however, some policy decisions must be made. For example, a digital I/O driver may only offer byte-wide access to the hardware in order to avoid the extra code needed to handle individual bits.

You can also look at your driver from a different perspective: it is a software layer that lies between the applications and the actual device. This privileged role of the driver allows the driver programmer to choose exactly how the device should appear: different drivers can offer different capabilities, even for the same device. The actual driver design should be a balance between many different considerations. For instance, a single device may be used concurrently by different programs, and the driver programmer has complete freedom to determine how to handle concurrency.

You could implement memory mapping on the device independently of its hardware capabilities, or you could provide a user library to help application programmers implement new policies on top of the available primitives, and so forth. One major consideration is the trade-off between the desire to present the user with as many options as possible and the time you have to write the driver, as well as the need to keep things simple so that errors don’t creep in. Policy-free drivers have a number of typical characteristics. These include support for both synchronous and asynchronous operation, the ability to be opened multiple times, the ability to exploit the full capabilities of the hardware, and the lack of software layers to “simplify things” or provide policy-related operations. Drivers of this sort not only work better for their end users, but also turn out to be easier to write and maintain as well. Being policy-free is actually a common target for software designers.

Many device drivers, indeed, are released together with user programs to help with configuration and access to the target device. Those programs can range from simple utilities to complete graphical applications. Examples include thetunelp program, which adjusts how the parallel port printer driver operates, and the graphicalcardctl utility that is part of the PCMCIA driver package. Often a client library is provided as well, which provides capabilities that do not need to be implemented as part of the driver itself.

The scope of this book is the kernel, so we try not to deal with policy issues or with application programs or support libraries. Sometimes we talk about different policies and how to support them, but we won’t go into much detail about programs using the device or the policies they enforce. You should understand, however, that user programs are an integral part of a software package and that even policy-free packages are distributed with configuration files that apply a default behavior to the underlying mechanisms.

From: LDD3

转载于:https://www.cnblogs.com/embedded-linux/p/5941241.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/417971.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vba 执行网页javascript_JavaScript秘密笔记 第一集

1. 什么是JavaScript2. 如何使用JavaScript3. *变量4. *数据类型谁记得笔记越多,谁学的越烂!1. 什么是JavaScript:前端三大语言:HTML: 专门编写网页内容的语言CSS: 专门编写网页样式的语言问题: 使用HTML和CSS做出的网页,只能看不能用——静态…

文字

标题&#xff1a; 标题的大小一共有六种&#xff0c;两个标签一组&#xff0c;也就是从<h1>到<h6>&#xff0c;<h1>最大&#xff0c;<h6>最小。使用标题标签时&#xff0c;该标签会将字体变成粗体字&#xff0c;并且会自成一行。 一般&#xff1a; <…

一定质量的封闭气体被压缩后_多晶硅氯氢化装置补充氢隔膜压缩机十字头铜套磨损原因分析与改善探讨...

潘祝新&#xff0c;王永(江苏中能硅业科技发展有限公司&#xff0c;江苏徐州221004)[摘要]&#xff1a;补充氢气压缩机是多晶硅行业氯氢化装置中的关键设备&#xff0c;它为生产系统的稳定运行不断的补充高纯氢气&#xff0c;其稳定而高效的运行对于生产系统的稳定性及提高氯硅…

vue 生成发布包_年轻人如何从0到1封装发布一个vue组件__Vue.js

封装发布组件是前端开发中非常重要的能力&#xff0c;通过对常用组件的封装可以提升团队开发的效率&#xff0c;避免重复劳作且不方便维护。好的组件的抽象和封装能让组件得到更广泛和多环境兼容的应用。本文讲述了如何一步步从0到1封装发布一个常用的toast组件的过程。本文是搭…

指针选择排序法,10个整数从小到大排序

//指针方法&#xff0c;选择排序法对10个int按从小到大排列 #include<stdio.h> main() {int n10,i,b,a[10],*p;int sort(int *q,int n);// scanf("%d",&10);for(pa;p<a10;p)//键盘输入数组元素scanf("%d",p);pa;//超重要!!!!!!!!!!不能忘sort…

实现拓扑图_20源码实现【自动寻路】 —【拓扑图】—网游城市互传最优路径算....

金猪脚本(原飞猪脚本)以按键精灵教学为主,涉及UiBot&#xff0c;Python,Lua等脚本编程语言,教学包括全自动办公脚本,游戏辅助脚本,引流脚本,网页脚本,安卓脚本,IOS脚本,注册脚本,点赞脚本,阅读脚本以及网赚脚本等各个领域。想学习按键精灵的朋友可以添加金猪脚本粉丝交流群:554…

注册表文件(*.reg)的编写及应用

编写 一、打开记事本&#xff0c;输入“Windows Registry Editor Version 5.00”。 二、按回车键两下以上&#xff08;至少保留一个空行&#xff09;。 三、输入修改内容&#xff08;常用设置 网吧修改&#xff09;。 四、保存为 *.reg 。 应用 双击打开出现一个提示框&#…

Liferay7 BPM门户开发之4: Activiti事件处理和监听Event handlers

事件机制从Activiti 5.15开始引入&#xff0c;这非常棒&#xff0c;他可以让你实现委托。 可以通过配置添加事件监听器&#xff0c;也可以通过Runtime API加入注册事件。 所有的事件参数子类型都来自org.activiti.engine.delegate.event.ActivitiEvent 包含的信息: typeexecuti…

lm358在哪个库里_库里8和aj35哪个好 库里8是安德玛的吗

库里8代作为全新的实战篮球鞋&#xff0c;也是带有库里logo的首款鞋&#xff0c;还是很受大家关注的&#xff0c;不过就是价格的话可能相对偏高一些&#xff0c;不过总体的性能还是不错的&#xff0c;那么大家觉得库里8和aj35哪个实战性能好呢&#xff1f;下面就和天马商盟的小…

Windows快捷键大全

天气:心情:Windows快捷键大全 一、常见用法&#xff1a; F1 显示当前程序或者windows的帮助内容。 F2 当你选中一个文件的话&#xff0c;这意味着“重命名” F3 当你在桌面上的时候是打开“查找&#xff1a;所有文件” 对话框 F10或ALT 激活当前程序的菜单栏 windows键或CTRLES…

ajax设置自定义请求头信息

客户端请求 $.ajax({ type:"post", url:urlstr, dataType:json, async:true, headers:{token:abck}, success:function (data) { console.log(data.info); console.log(JSON.stringify(data)); }, error:function (xhr,text) { alert(text); } }); 服务端代码 public…

HTML元素参考手册 HTML Elements Reference

a表示超链接的起始或目的位置。acronym表示取首字母的缩写词。address表示特定信息&#xff0c;如地址、签名、作者、文档信息。applet在页面上放置可执行内容。area定义一个客户端图像映射中一个超级链接区域的形状、坐标和关联 URL。b指定文本应以粗体显示。base指定一个显式…

ei加声调怎么加_ei在e还是i上标声调

展开全部ei在e上标声调。汉语拼音的标调口诀是&#xff1a;1、a母出现62616964757a686964616fe58685e5aeb931333431353864不放过&#xff0c; (即韵母中凡是有a的&#xff0c;标在a上。如lao,标在a上)&#xff1b;2、没有a母找 o e &#xff0c; (没有a&#xff0c;但有o 或e的…

Quartz简单实例

Quartz中提供了两种触发器&#xff0c;分别是CronTrigger和SimpleTrigger。 1. SimpleTrigger 每隔若干毫秒来触发纳入进度的任务。 2. CronTrigger 在特定“格林日历”时刻触发纳入进程的任务。 调度程序如下&#xff1a; public class TestQuartz {public void myTask() thro…