[英]Embedded Linux: kernel drivers vs user space drivers?
Is there a best approach (or at least some advantages and disadvantages) when choosing between the use of kernel space or user space drivers in Linux? 在Linux中使用内核空间或用户空间驱动程序时,是否有最佳方法(或至少是一些优点和缺点)?
For example, let's say I'm developing a board for sensing humidity built on the Sensirion SHT21 sensor. 例如,假设我正在开发一种用于感应Sensirion SHT21传感器上的湿度的板。 My application will read a sample from the sensor and then present it in JSON form for a web application to consume. 我的应用程序将从传感器中读取一个样本,然后以JSON形式呈现它以供Web应用程序使用。
In order to "talk" with the SHT21 sensor I can either: 为了与SHT21传感器“对话”,我可以:
echo sht21 0x40 > /sys/class/i2c-adapter/i2c-0/new_device
and access the humidity readings via hwmon
, parse the output and then use it in my application 使用echo sht21 0x40 > /sys/class/i2c-adapter/i2c-0/new_device
实例化I2C设备并通过hwmon
访问湿度读数,解析输出然后在我的应用程序中使用它 write()
and read()
operations against /dev/i2c-0
and calculate the humidity myself, then use it in my application 编写我自己的软件,对/dev/i2c-0
执行所需的(根据数据表) write()
和read()
操作并自己计算湿度,然后在我的应用程序中使用它 The first approach makes use of the sht21
kernel driver, the latter works entirely in user space. 第一种方法使用sht21
内核驱动程序,后者完全在用户空间中工作。
Which one should I go for? 我应该去哪一个? How should I choose? 我该如何选择?
Of the top of my head: 在我的头顶:
Userland approach pros: Userland方法专业人士:
Userland approach cons: Userland方法缺点:
In the case of your application, putting that in perspective: 对于您的应用程序,请将其放在透视图中:
...all in all, I would 200% go with the userland approach. ......总而言之,我会200%采用用户态方法。
Kernel space may be technically more "fun" or "rewarding", but engineering put "pragmatic" before "fun". 内核空间在技术上可能更“有趣”或“有意义”,但工程学在“有趣”之前将“务实”放在一边。 :-) :-)
In both cases your driving what happens from user space, option 2 implies writing a driver which I would avoid, if it already exists your good. 在这两种情况下,你驾驶从用户空间发生的事情,选项2意味着写一个我会避免的驱动程序,如果它已经存在你的好处。
If there's a driver that parses and makes sense of some the data that's being used in production use it if it's easier to use or has a large user base. 如果有一个驱动程序解析并理解某些正在生产中使用的数据,那么如果它更容易使用或拥有大量用户群,则使用它。 If it's trivial to parse the raw data and you're truly squeezed for space (I doubt this because you running a kernel) then write your own parser etc. 如果解析原始数据是微不足道的并且你真的被挤压空间(我怀疑这是因为你运行了一个内核)然后编写你自己的解析器等。
To be short what's the least work ie the least complex, do that. 简而言之,即做最少的工作,即最不复杂的工作,做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.