/dev、/sys/devices 和 /sys/dev 之间区别

本文介绍 Linux 中的设备模型实现方式,包括 /dev 和 /sys/devices 目录的作用,sysfs 文件系统的结构和组织方式,以及 udev 如何通过内核事件动态管理设备文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/dev,设备文件存储目录,应用程序通过对这些文件的读写和控制,可以访问实际的设备;

/sys/devices 目录,按照设备挂接的总线类型,组织成层次结构,保存了系统所有的设备;是文件系统管理设备的最重要的目录结构;

这是内核设备按总线类型分层放置的目录结构, devices 中的所有设备都是连接于某种总线之下,在这里的每一种具体总线之下可以

找到每一个具体设备的符号链接,它也是构成 Linux 统一设备模型的一部分;

/sys/dev 下有两个子目录,block和char,存放的是块设备和字符设备的主次号码,形式为(major:minor),它指向 /sys/devices 目录下的设备。

1. sysfs的挂载点是/sys目录, sysfs是一个虚拟的文件系统(还有其它的虚拟文件系统,例如usbfs和procfs),sysfs导出了内核的数据结构。
2. /sys/dev/ 和/sys/devices是sysfs按面向对象管理的思想来组织,sysfs最主要是用来描绘Linux kernel 2.6中的设备驱动模型,用户态的
后台程序会动态地周期性的扫描/sys目录中的属性项来自动管理设备文件(也称为设备节点),从而在/dev目录会建立或者删除对应的设备文件。

Documentation\filesystems

Top Level Directory Layout
~~~~~~~~~~~~~~~~~~~~~~~~~~

The sysfs directory arrangement exposes the relationship of kernel
data structures. 

The top level sysfs directory looks like:

block/
bus/
class/
dev/
devices/
firmware/
net/
fs/

<span style="color:#ff0000;">devices/ contains a filesystem representation of the device tree. It maps
directly to the internal kernel device tree, which is a hierarchy of
struct device.</span> 

bus/ contains flat directory layout of the various bus types in the
kernel. Each bus's directory contains two subdirectories:

	devices/
	drivers/

devices/ contains symlinks for each device discovered in the system
that point to the device's directory under root/.

drivers/ contains a directory for each device driver that is loaded
for devices on that particular bus (this assumes that drivers do not
span multiple bus types).

fs/ contains a directory for some filesystems.  Currently each
filesystem wanting to export attributes must create its own hierarchy
below fs/ (see ./fuse.txt for an example).

<span style="color:#ff0000;">dev/ contains two directories char/ and block/. Inside these two
directories there are symlinks named <major>:<minor>.  These symlinks
point to the sysfs directory for the given device.  /sys/dev provides a
quick way to lookup the sysfs interface for a device from the result of
a stat(2) operation.</span>

More information can driver-model specific features can be found in
Documentation/driver-model/. 
udev:
-----------------------------
udev is a device management framework that replaced the devfs facility in the Linux 2.6 kernel. It is composed of some kernel services and the udevd daemon. The kernel informs the udevd daemon when certain events happen. The udevd daemon is configured to respond to some events with actions. Always, it means adding/removing device file under /dev dynamically.

* When the kernel detects that a device has been added or removed, a uevent is sent to
the udevd daemon through a netlink socket
* When udevd receives the uevent, it matches its configured rules against the available
device attributes provided in sysfs
* If a match is found, one or more actions (e.g., create device node, remove device node,
install firmware, etc.) are taken

<think>嗯,用户问的是Linux系统中sys/class/ttydev/tty的区别。这个问题涉及到Linux设备模型的核心概念,需要从sysfsdevtmpfs两个虚拟文件系统的角度来解释。 用户之前已经了解过sys/class目录的作用,现在聚焦在tty这个具体设备类别上。从引用材料看,用户提供了4个参考资料,其中[1][4]专门讨论tty,[2][3]则解释sysdev目录的关系。这些材料质量不错,特别是[3]明确指出/sys/block已迁移到/sys/class/block,说明用户对Linux设备模型演进有一定了解。 用户可能正在调试终端设备问题,或者学习Linux设备驱动框架。考虑到ta引用了/proc/tty/drivers的内容,应该具备一定技术基础。不过问题本身比较基础,需要区分清楚三个层次:内核设备模型(sysfs)、设备节点(devtmpfs)终端控制的概念。 需要特别注意几个关键点:第一,/dev/tty是动态生成的设备节点,而/sys/class/tty是内核设备信息的映射;第二,/dev/tty的特殊语义(当前控制终端);第三,sysfs文件不是用来直接操作的接口。在解释时要避免把udev规则等复杂内容引入,但可以暗示这些机制的存在。 上次回答详细解释了sys/class的架构,这次应该延续那个框架,把tty作为具体案例来分析。用户似乎更喜欢表格对比的方式,但需要控制信息量,重点突出功能性访问方式的差异。终端子系统本身很复杂,暂时不需要展开termios或行规范这些内容。 对了,用户引用的材料[4]提到/dev/tty的设备号是(5,0),这个细节值得在回答中强调,因为能直观展示设备节点的本质。而sys/class/tty里看到的设备号则是实际物理终端的设备号,比如(4,0)对应vc/0,这个对比会很有说服力。</think>在Linux系统中,`/sys/class/tty/` `/dev/tty` 是**完全不同的概念**,分别属于内核设备模型用户空间设备接口,具体区别如下: --- ### 1. **功能定位** | **路径** | **作用** | **所属子系统** | |--------------------|--------------------------------------------------------------------------|----------------------| | `/sys/class/tty/` | **内核设备信息的映射**<br>提供所有TTY设备的**属性、状态配置接口**(只读或可写) | sysfs 虚拟文件系统 | | `/dev/tty` | **用户空间设备节点**<br>作为**当前进程的控制终端**的抽象接口,用于读写数据 | devtmpfs 文件系统 | --- ### 2. **内容与结构** #### **/sys/class/tty/** - **目录结构示例**: ``` /sys/class/tty/ ├── tty0/ # 虚拟控制台0 │ ├── active # 当前活动终端 │ ├── power/ # 电源管理状态 │ └── ... # 其他内核属性 ├── ttyS0/ # 串口设备0 (如COM1) │ ├── uartclk # 串口时钟频率 │ └── ... └── ttyUSB0/ # USB转串口设备0 ``` - **核心作用**: - 查看TTY设备属性(如波特率、数据位)。 - 配置底层参数(如通过`echo 115200 > /sys/class/tty/ttyS0/speed`修改串口波特率)[^1]。 - 符号链接指向实际设备位置(如`/sys/devices/pci0000:00/serial/`)。 #### **/dev/tty** - **本质**:一个**字符设备文件**,主设备号5,次设备号0。 - **行为特性**: - 始终指向**当前进程的控制终端**(通过`tty`命令可查看实际终端路径)。 - 若进程在图形界面启动,`/dev/tty`可能指向`/dev/pts/0`(伪终端); - 若在物理终端登录,则指向`/dev/tty1`等[^4]。 - **使用场景**: ```bash echo "Hello" > /dev/tty # 直接输出到当前终端 ``` --- ### 3. **访问方式对比** | **特性** | `/sys/class/tty/` | `/dev/tty` | |------------------|--------------------------------------------|--------------------------------| | **读写类型** | 属性文件(文本) | 设备文件(二进制数据流) | | **操作示例** | `cat /sys/class/tty/ttyS0/speed` | `echo "test" > /dev/tty` | | **修改配置** | 支持(如修改串口参数) | 不支持(仅数据读写) | | **动态性** | 随设备插拔动态增删 | 始终存在,指向当前终端 | --- ### 4. **关键区别总结** - **抽象层级** - `/sys/class/tty/` 是**内核级设备模型**,暴露硬件细节驱动接口。 - `/dev/tty` 是**用户级抽象**,屏蔽底层差异,提供统一访问入口[^2][^4]。 - **设计目的** - `/sys/class/tty/` 用于**系统管理**(如配置串口、调试驱动)。 - `/dev/tty` 用于**应用程序交互**(如Shell输入输出、终端程序)。 - **符号链接关系** 每个`/sys/class/tty/`下的设备(如`ttyS0`)会对应一个`/dev/`下的设备节点(如`/dev/ttyS0`),而`/dev/tty`是独立存在的特殊节点[^3]。 --- ### 示例场景 1. **查看串口信息** ```bash # 通过sysfs查看串口ttyS0的波特率 cat /sys/class/tty/ttyS0/speed ``` 2. **向当前终端输出数据** ```bash # 无论终端是物理控制台(tty1)还是SSH会话(pts/0),均输出到当前终端 echo "Message" > /dev/tty ``` --- **总结**: `/sys/class/tty/`是**内核设备信息的分类视图**,用于管理监控;`/dev/tty`是**进程控制终端的动态抽象**,用于数据交互。两者互补,共同构成Linux TTY子系统[^1][^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂奔的乌龟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值