有人可以说明 linux 设备号的次设备号到底是做什么用的吗?

发布于 2024-09-04 08:41:28 字数 93 浏览 5 评论 0原文

次要编号由内核使用 准确确定哪个设备是 被提及。

上面的解释只是两个摘要,有人可以说明吗?

The minor number is used by the kernel
to determine exactly which device is
being referred to.

The above explanation is just two abstract, can someone illustrate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

娇纵 2024-09-11 08:41:28

主设备号标识要使用的设备驱动程序,次设备号指示哪个设备。例如,如果您有多个分区,则每个分区都有自己的次要分区:

brw-rw---- 1 root disk 8, 0 Jun  3 20:48 /dev/sda
brw-rw---- 1 root disk 8, 1 Jun  3 20:48 /dev/sda1
brw-rw---- 1 root disk 8, 2 Jun  3 20:48 /dev/sda2

在这种情况下,次要分区 0 是原始驱动器,次要分区 1 是分区 1,次要分区 2 是分区 2,等等。但是,并非所有设备都使用 0 作为特殊情况。串行设备从 0 开始编号,其中 /dev/tty0 只是系统上的第一个(虚拟)终端设备:

crw--w---- 1 root tty  4, 0 Jun  3 20:48 /dev/tty0
crw------- 1 root root 4, 1 Jun  3 20:50 /dev/tty1
crw------- 1 root root 4, 2 Jun  3 20:50 /dev/tty2
crw------- 1 root root 4, 3 Jun  3 20:50 /dev/tty3

无论哪种情况,当打开设备文件时,内核都将使用主要的来确定哪个模块将处理该文件,并将次要函数传递给使用 register_chrdev()struct file_operations 结构的 open() 方法代码>.

The major number identifies the device driver to use, the minor number indicates which device. If you have multiple partitions, for instance, each gets its own minor:

brw-rw---- 1 root disk 8, 0 Jun  3 20:48 /dev/sda
brw-rw---- 1 root disk 8, 1 Jun  3 20:48 /dev/sda1
brw-rw---- 1 root disk 8, 2 Jun  3 20:48 /dev/sda2

Minor 0 in this case is the raw drive, minor 1 is partition 1, minor 2 is partition 2, etc. Not all devices use 0 as a special case, however. The serial devices start their numbering at 0, where /dev/tty0 is just the first (virtual) terminal device on the system:

crw--w---- 1 root tty  4, 0 Jun  3 20:48 /dev/tty0
crw------- 1 root root 4, 1 Jun  3 20:50 /dev/tty1
crw------- 1 root root 4, 2 Jun  3 20:50 /dev/tty2
crw------- 1 root root 4, 3 Jun  3 20:50 /dev/tty3

In either event, when the device file is opened the kernel will use the major number to determine which module will handle the file and passes the minor to the open() method of the struct file_operations structure that was registered with register_chrdev().

窗影残 2024-09-11 08:41:28

就像在 C 语言中一样,从 0 开始计数...这可能是驱动程序编写者遵循的一种约定...毕竟 Linux 中的所有内容都基于 C 或某种程度上 C++。它只是一个编号方案...也可以从 0 到 255 之间的任何数字开始命名您的设备,但请小心不要超过次要编号的 255 标记,否则您可能会覆盖下一个可用的主要编号..希望这个答案有帮助

as in the language C ,counting starts from 0 ...it could be a sort of convention followed by driver writers ...after all everything in Linux is based upon C or to some extent C++.Its just a numbering scheme ..You can also start naming your devices from any number between 0 and 255,but please be carefull do not cross the 255 mark for minor numbers else you may overwrite the next available major number..hope this answer helps

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