连接应用程序与设备驱动程序
这是一道面试题。
我已经为 char 设备编写了设备驱动程序,所以我知道代码结构如下所示:
struct file_operations something {
.owner=my_device_open;
.read=my_device_read;
.close=my_device_close;
.write=my_device_write;
}
当设备驱动程序处于活动状态时,
/dev/mydevice
您可以实际读取和写入它。 但我不清楚应用程序如何读取或写入该设备。 我知道 insmod
会将模块插入内核,并且 register_chrdev();
将在内核中注册驱动程序,但应用程序将如何与该驱动程序通信。 让我知道正确答案是什么。
This is an interview question.
I had written device driver for a char device so I know that code structure looks like this
struct file_operations something {
.owner=my_device_open;
.read=my_device_read;
.close=my_device_close;
.write=my_device_write;
}
When the device driver is active then in
/dev/mydevice
you can actually read and write into it.
But what I was not clear is how an application will read or write to this device.
I know insmod
will insert the module to kernel,and register_chrdev();
will register the driver in kernel but how will application program communicate with this driver.
Let me know what will be correct answer for it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
马丁·贝克特总结道。其实并没有更复杂,
尽管你可以说同样的话,但稍微更详细一点。这是我的
尝试一下:
程序执行
open("/dev/mydevice", flags)
系统调用,然后内核从磁盘读取
/dev/mydevice
。它只是一个 inode,没有关联的数据块,但它包含两个重要的部分
信息:主要编号和次要编号。从这些数字来看,
内核找到您通过提供的
struct file_operations
register_chrdev()
,它调用它的.open
字段。它返回到编程一个与该特定相关的文件描述符
结构文件操作
。接下来,当内核收到类似的系统调用时write(fd, buf, count)
,它会调用.write
字段等等。Well Martin Beckett summed it up. It is not really more complex,
although you could say the same with slightly more detail. Here is my
try at it:
The program performs an
open("/dev/mydevice", flags)
syscall, then thekernel reads
/dev/mydevice
from disk. It is just an inode, with noassociated data blocks, but it holds two important pieces of
information: the major number and the minor number. From these numbers,
the kernel finds the
struct file_operations
that you provided throughregister_chrdev()
, and it calls it's.open
field. It returns to theprogram a file descriptor that it associated with this particular
struct file_operations
. Next, when the kernel receives a syscall likewrite(fd, buf, count)
, it will call the.write
field and so on.在 unix 中,它只是将设备节点作为文件打开,并从中发送/接收数据和命令。
Unix 的美妙之处在于,从应用程序的角度来看,设备没有什么特别之处 - 它们只是文件(除了用于设置某些模式的 ioctl)。内核中需要做一些工作来适应这一点,但这就是内核模块的问题。
或者你问的是更复杂的事情?
In unix it simply opens the device node as a file and sends/receives data and commands from it.
The beauty of Unix is that from an app's point of view there is nothing special about devices - they are just files (except for ioctls to set some modes). There is work to do in the kernel to accomodate this but that's the kernel modules problem.
Or were you asking something more complex?