- 浏览: 982383 次
- 性别:
- 来自: 广州
最新评论
-
qingchuwudi:
有用,非常感谢!
erlang进程的优先级 -
zfjdiamond:
你好 这条命令 在那里输入??
你们有yum 我有LuaRocks -
simsunny22:
这个是在linux下运行的吧,在window下怎么运行escr ...
escript的高级特性 -
mozhenghua:
http://www.erlang.org/doc/apps/ ...
mnesia 分布协调的几个细节 -
fxltsbl:
A new record of 108000 HTTP req ...
Haproxy 1.4-dev2: barrier of 100k HTTP req/s crossed
3.2 The driver
Although Erlang drivers in general may be beyond the scope of this document, a brief introduction seems to be in place.
3.2.1 Drivers in general
An Erlang driver is a native code module written in C (or assembler) which serves as an interface for some special operating system service. This is a general mechanism that is used throughout the Erlang emulator for all kinds of I/O. An Erlang driver can be dynamically linked (or loaded) to the Erlang emulator at runtime by using the erl_ddll Erlang module. Some of the drivers in OTP are however statically linked to the runtime system, but that's more an optimization than a necessity.
The driver data-types and the functions available to the driver writer are defined in the header file erl_driver.h (there is also an deprecated version called driver.h, don't use that one.) seated in Erlang's include directory (and in $ERL_TOP/erts/emulator/beam in the source code distribution). Refer to that file for function prototypes etc.
When writing a driver to make a communications protocol available to Erlang, one should know just about everything worth knowing about that particular protocol. All operation has to be non blocking and all possible situations should be accounted for in the driver. A non stable driver will affect and/or crash the whole Erlang runtime system, which is seldom what's wanted.
The emulator calls the driver in the following situations:
1. When the driver is loaded. This call-back has to have a special name and will inform the emulator of what call-backs should be used by returning a pointer to a ErlDrvEntry struct, which should be properly filled in (see below).
2. When a port to the driver is opened (by a open_port call from Erlang). This routine should set up internal data structures and return an opaque data entity of the type ErlDrvData, which is a data-type large enough to hold a pointer. The pointer returned by this function will be the first argument to all other call-backs concerning this particular port. It is usually called the port handle. The emulator only stores the handle and does never try to interpret it, why it can be virtually anything (well anything not larger than a pointer that is) and can point to anything if it is a pointer. Usually this pointer will refer to a structure holding information about the particular port, as i t does in our example.
3. When an Erlang process sends data to the port. The data will arrive as a buffer of bytes, the interpretation is not defined, but is up to the implementor. This call-back returns nothing to the caller, answers are sent to the caller as messages (using a routine called driver_output available to all drivers). There is also a way to talk in a synchronous way to drivers, described below. There can be an additional call-back function for handling data that is fragmented (sent in a deep io-list). That interface will get the data in a form suitable for Unix writev rather than in a single buffer. There is no need for a distribution driver to implement such a call-back, so we wont.
4. When a file descriptor is signaled for input. This call-back is called when the emulator detects input on a file descriptor which the driver has marked for monitoring by using the interface driver_select. The mechanism of driver select makes it possible to read non blocking from file descriptors by calling driver_select when reading is needed and then do the actual reading in this call-back (when reading is actually possible). The typical scenario is that driver_select is called when an Erlang process orders a read operation, and that this routine sends the answer when data is available on the file descriptor.
5. When a file descriptor is signaled for output. This call-back is called in a similar way as the previous, but when writing to a file descriptor is possible. The usual scenario is that Erlang orders writing on a file descriptor and that the driver calls driver_select. When the descriptor is ready for output, this call-back is called an the driver can try to send the output. There may of course be queuing involved in such operations, and there are some convenient queue routines available to the driver writer to use in such situations.
6. When a port is closed, either by an Erlang process or by the driver calling one of the driver_failure_XXX routines. This routine should clean up everything connected to one particular port. Note that when other call-backs call a driver_failure_XXX routine, this routine will be immediately called and the call-back routine issuing the error can make no more use of the data structures for the port, as this routine surely has freed all associated data and closed all file descriptors. If the queue utility available to driver writes is used, this routine will however not be called until the queue is empty.
7. When an Erlang process calls erlang:driver_control/2, which is a synchronous interface to drivers. The control interface is used to set driver options, change states of ports etc. We'll use this interface quite a lot in our example.
8. When a timer expires. The driver can set timers with the function driver_set_timer. When such timers expire, a specific call-back function is called. We will not use timers in our example.
9. When the whole driver is unloaded. Every resource allocated by the driver should be freed.
Although Erlang drivers in general may be beyond the scope of this document, a brief introduction seems to be in place.
3.2.1 Drivers in general
An Erlang driver is a native code module written in C (or assembler) which serves as an interface for some special operating system service. This is a general mechanism that is used throughout the Erlang emulator for all kinds of I/O. An Erlang driver can be dynamically linked (or loaded) to the Erlang emulator at runtime by using the erl_ddll Erlang module. Some of the drivers in OTP are however statically linked to the runtime system, but that's more an optimization than a necessity.
The driver data-types and the functions available to the driver writer are defined in the header file erl_driver.h (there is also an deprecated version called driver.h, don't use that one.) seated in Erlang's include directory (and in $ERL_TOP/erts/emulator/beam in the source code distribution). Refer to that file for function prototypes etc.
When writing a driver to make a communications protocol available to Erlang, one should know just about everything worth knowing about that particular protocol. All operation has to be non blocking and all possible situations should be accounted for in the driver. A non stable driver will affect and/or crash the whole Erlang runtime system, which is seldom what's wanted.
The emulator calls the driver in the following situations:
1. When the driver is loaded. This call-back has to have a special name and will inform the emulator of what call-backs should be used by returning a pointer to a ErlDrvEntry struct, which should be properly filled in (see below).
2. When a port to the driver is opened (by a open_port call from Erlang). This routine should set up internal data structures and return an opaque data entity of the type ErlDrvData, which is a data-type large enough to hold a pointer. The pointer returned by this function will be the first argument to all other call-backs concerning this particular port. It is usually called the port handle. The emulator only stores the handle and does never try to interpret it, why it can be virtually anything (well anything not larger than a pointer that is) and can point to anything if it is a pointer. Usually this pointer will refer to a structure holding information about the particular port, as i t does in our example.
3. When an Erlang process sends data to the port. The data will arrive as a buffer of bytes, the interpretation is not defined, but is up to the implementor. This call-back returns nothing to the caller, answers are sent to the caller as messages (using a routine called driver_output available to all drivers). There is also a way to talk in a synchronous way to drivers, described below. There can be an additional call-back function for handling data that is fragmented (sent in a deep io-list). That interface will get the data in a form suitable for Unix writev rather than in a single buffer. There is no need for a distribution driver to implement such a call-back, so we wont.
4. When a file descriptor is signaled for input. This call-back is called when the emulator detects input on a file descriptor which the driver has marked for monitoring by using the interface driver_select. The mechanism of driver select makes it possible to read non blocking from file descriptors by calling driver_select when reading is needed and then do the actual reading in this call-back (when reading is actually possible). The typical scenario is that driver_select is called when an Erlang process orders a read operation, and that this routine sends the answer when data is available on the file descriptor.
5. When a file descriptor is signaled for output. This call-back is called in a similar way as the previous, but when writing to a file descriptor is possible. The usual scenario is that Erlang orders writing on a file descriptor and that the driver calls driver_select. When the descriptor is ready for output, this call-back is called an the driver can try to send the output. There may of course be queuing involved in such operations, and there are some convenient queue routines available to the driver writer to use in such situations.
6. When a port is closed, either by an Erlang process or by the driver calling one of the driver_failure_XXX routines. This routine should clean up everything connected to one particular port. Note that when other call-backs call a driver_failure_XXX routine, this routine will be immediately called and the call-back routine issuing the error can make no more use of the data structures for the port, as this routine surely has freed all associated data and closed all file descriptors. If the queue utility available to driver writes is used, this routine will however not be called until the queue is empty.
7. When an Erlang process calls erlang:driver_control/2, which is a synchronous interface to drivers. The control interface is used to set driver options, change states of ports etc. We'll use this interface quite a lot in our example.
8. When a timer expires. The driver can set timers with the function driver_set_timer. When such timers expire, a specific call-back function is called. We will not use timers in our example.
9. When the whole driver is unloaded. Every resource allocated by the driver should be freed.
发表评论
-
OTP R14A今天发布了
2010-06-17 14:36 2677以下是这次发布的亮点,没有太大的性能改进, 主要是修理了很多B ... -
R14A实现了EEP31,添加了binary模块
2010-05-21 15:15 3030Erlang的binary数据结构非常强大,而且偏向底层,在作 ... -
如何查看节点的可用句柄数目和已用句柄数
2010-04-08 03:31 4814很多同学在使用erlang的过程中, 碰到了很奇怪的问题, 后 ... -
获取Erlang系统信息的代码片段
2010-04-06 21:49 3475从lib/megaco/src/tcp/megaco_tcp_ ... -
iolist跟list有什么区别?
2010-04-06 20:30 6529看到erlang-china.org上有个 ... -
erlang:send_after和erlang:start_timer的使用解释
2010-04-06 18:31 8386前段时间arksea 同学提出这个问题, 因为文档里面写的很不 ... -
Latest news from the Erlang/OTP team at Ericsson 2010
2010-04-05 19:23 2013参考Talk http://www.erlang-factor ... -
对try 异常 运行的疑问,为什么出现两种结果
2010-04-05 19:22 2842郎咸武<langxianzhe@163.com> ... -
Erlang ERTS Async基础设施
2010-03-19 00:03 2517其实Erts的Async做的很不错的, 相当的完备, 性能又高 ... -
CloudI 0.0.9 Released, A Cloud as an Interface
2010-03-09 22:32 2476基于Erlang的云平台 看了下代码 质量还是不错的 完成了不 ... -
Memory matters - even in Erlang (再次说明了了解内存如何工作的必要性)
2010-03-09 20:26 3439原文地址:http://www.lshift.net/blog ... -
Some simple examples of using Erlang’s XPath implementation
2010-03-08 23:30 2050原文地址 http://www.lshift.net/blog ... -
lcnt 环境搭建
2010-02-26 16:19 2614抄书:otp_doc_html_R13B04/lib/tool ... -
Erlang强大的代码重构工具 tidier
2010-02-25 16:22 2486Jan 29, 2010 We are very happy ... -
[Feb 24 2010] Erlang/OTP R13B04 has been released
2010-02-25 00:31 1387Erlang/OTP R13B04 has been rele ... -
R13B04 Installation
2010-01-28 10:28 1390R13B04后erlang的源码编译为了考虑移植性,就改变了编 ... -
Running tests
2010-01-19 14:51 1486R13B03以后 OTP的模块加入了大量的测试模块,这些模块都 ... -
R13B04在细化Binary heap
2010-01-14 15:11 1508从github otp的更新日志可以清楚的看到otp R13B ... -
R13B03 binary vheap有助减少binary内存压力
2009-11-29 16:07 1668R13B03 binary vheap有助减少binary内存 ... -
erl_nif 扩展erlang的另外一种方法
2009-11-26 01:02 3218我们知道扩展erl有2种方法, driver和port. 这2 ...
相关推荐
Design of High Voltage xDSL Line Drivers in Standard CMOS PASSCODE: 123456
Linux Device Drivers Development: Develop customized drivers for embedded Linux AZW3版(kindle格式),可以用开源软件calibre-ebook(https://calibre-ebook.com/)打开,里面的图片可以用该软件还原到原本的大...
- **《Linux Device Drivers》一书**:这本书作为经典的参考指南,已经帮助无数程序员掌握了如何为Linux操作系统支持计算机外围设备以及如何为新硬件开发软件。 - **新版书籍更新**:第三版覆盖了Linux内核2.6的重要...
CP2101_Drivers CP2101_Drivers CP2101_Drivers
《索尼手机刷机驱动详解——Flashtool-drivers.zip深度解析》 在智能手机的世界里,刷机是一项深受技术爱好者喜爱的操作,它可以让用户自定义系统、优化性能或尝试新的功能。尤其对于索尼手机用户来说,拥有合适的...
《Linux Device Drivers》这本书主要关注的是为Linux系统编写设备驱动程序。这不仅是针对新硬件产品不断涌现的需求,也包括理解Linux内核的工作原理以及如何根据自身需求或兴趣对其进行适应和修改。Linux作为一个...
这个压缩包“drivers_drivers_linux_Kernel_”包含了书中的样例代码,是学习和理解Linux驱动开发的重要资源。 Linux驱动程序是连接硬件与操作系统之间的桥梁,它们负责向Linux内核提供与硬件交互的接口,使得内核...
Selenium Client Drivers是Selenium WebDriver的核心组成部分,它们是用于与不同的浏览器进行交互的接口。Selenium WebDriver是一个自动化测试工具,允许开发者模拟用户行为并进行网页应用程序的自动化测试。在这个...
Linux Device Drivers, 2nd Edition, In PDF Format Preface Chapter 1: An Introduction to Device Drivers Chapter 2: Building and Running Modules Chapter 3: Char Drivers Chapter 4: Debugging ...
Drivers in User Space Process Scheduling and Response Times Accessing I/O Regions Accessing Memory Regions User Mode SCSI User Mode USB User Mode I2C UIO Looking at the Sources Chapter 20. ...
### 美国汽车行业中成品库存的驱动因素 #### 引言 汽车制造业对全球经济具有重要影响,并在产品设计和制造技术方面产生了诸多创新(例如,装配线、准时制库存系统、看板管理等)。因此,它一直是大量实证研究的...
《P2K Drivers 2.9:E98设备刷机关键驱动详解》 在智能手机领域,刷机是一项常见的操作,它可以为用户带来更个性化的系统体验或解决设备问题。"P2K Drivers 2.9"是针对E98手机进行刷机的重要驱动程序,本文将详细...
《Linux Device Drivers 3rd》是Linux设备驱动程序开发领域的一本经典著作,结合源代码进行学习,对于想要深入理解Linux内核以及进行驱动开发的工程师来说,是必不可少的参考资料。这本书详细阐述了如何为Linux操作...
The goal of this session is to help users understand the Linux kernel DMA framework and how it can be used in a device driver
《Essential Linux Device Drivers》是Linux世界中一本非常重要的书籍,它深入浅出地介绍了Linux设备驱动程序的开发和工作原理。这本书对于那些想要理解Linux内核如何与硬件交互,或者想要编写自己的设备驱动程序的...
SQL Server Microsoft JDBC Drivers