连接应用程序与设备驱动程序

发布于 2024-11-18 08:46:24 字数 439 浏览 1 评论 0原文

这是一道面试题。

我已经为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不弃不离 2024-11-25 08:46:24

马丁·贝克特总结道。其实并没有更复杂,
尽管你可以说同样的话,但稍微更详细一点。这是我的
尝试一下:

程序执行 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 the
kernel reads /dev/mydevice from disk. It is just an inode, with no
associated 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 through
register_chrdev(), and it calls it's .open field. It returns to the
program a file descriptor that it associated with this particular
struct file_operations. Next, when the kernel receives a syscall like
write(fd, buf, count), it will call the .write field and so on.

半岛未凉 2024-11-25 08:46:24

在 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文