Home >Operation and Maintenance >Linux Operation and Maintenance >what is linux device file
In Linux, a device file is an interface to a device driver that allows an application to interact with a device using its device driver through standard input and output system calls. There are two commonly used device files in Linux systems: character special files and block special files. The difference between them lies in the amount of data read and written by the operating system and hardware.
#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
Device file overview
In Unix-like operations In the system, a device file or special file is the interface to the device driver and appears in the file system as if it were an ordinary file. There are also special files in MS-DOS, OS/2 and Microsoft Windows. These special files allow an application to interact with a device using its device driver through standard input/output system calls. Using standard system calls simplifies many programming tasks and enables consistent user-space I/O mechanisms regardless of the device's features and capabilities.
Device files typically provide a simple interface to standard devices (such as printers and serial ports), but can also be used to access specific unique resources on these devices (such as disk partitions). Additionally, device files are useful for accessing system resources that are not associated with any actual device, such as data sinks and random number generators.
Figure 1 The relationship between applications, block files, drivers, and hardware
There are two commonly used devices in Unix-like operating systems files, called character special files and block special files. The difference between them is the amount of data read and written by the operating system and hardware. Unlike named pipes, which can be called device special files, named pipes are not connected to a device but are not ordinary files either.
In some Unix-like systems, most device files are managed as part of a virtual file system traditionally mounted at /dev, possibly associated with a controlling daemon that Monitor hardware additions and removals at runtime and make corresponding changes to the device file system. If changes to the device file system are not automatically completed by the kernel, the control daemon may call scripts in the system or user space to handle special device requirements. .
FreeBSD and DragonFly BSD implementations have named the virtual device file system devfs and the associated daemon devd. Linux primarily uses a userspace implementation called udev, but there are many variations. Operating systems such as Darwin and macOS based on it have purely kernel-based device file systems.
On Unix systems that support chroot process isolation, such as Solaris Containers, typically each chroot environment requires its own /dev; these mount points will be located individually in the global file system tree Visible on the host operating system on the node. Hardware isolation can be enforced by the chroot environment by restricting the population of device nodes to a chroot instance of /dev (a program cannot plug into the hardware, and it can neither see nor access the name - this is stronger access control than Unix file system permissions form).
Device nodes correspond to resources that have been allocated by the operating system kernel. Unix identifies these resources by a major number and a minor number, both stored as part of the node structure. The assignment of these numbers occurs uniquely on different operating systems and on different computer platforms. Typically, a major number identifies a device driver, and a minor number identifies the specific device that the driver controls (perhaps over many devices): In this case, the system may pass the minor number to the driver. However, in the case of dynamically allocated numbers, this may not be the case (e.g. on FreeBSD 5 and above).
Like other special file types, computer systems use standard system calls to access device nodes and treat them as ordinary computer files. There are two standard types of device files; unfortunately, for historical reasons, their names are rather counter-intuitive, so it is often incorrect to explain the difference between the two.
Block file classification
Character devices (Character devices)
Character special files or character devices provide unbuffered, direct access to hardware devices. They do not necessarily allow a program to read or write a single character at a time; this depends on the device in question. For example, a hard disk's character device will typically require all read and write operations to be aligned to block boundaries and never allow individual byte reads.
Character devices are sometimes called raw devices to avoid confusion surrounding the fact that character devices on block-based hardware typically require programs to read and write aligned blocks.
Block devices (Block devices)
Block special files or block devices provide buffered access to hardware devices , and provide some abstract details. Unlike character devices, block devices will always allow the programmer to read or write blocks of any size (including single characters/bytes) and any alignment. The disadvantage is that since block devices are buffered, the programmer has no idea how long it will take before the write data is passed from the kernel's buffer to the actual device, or indeed in what order the two separate writes will arrive at the physical device? Additionally, if the same hardware exposes character and block devices, there is a risk of data corruption due to the client using the character device not being aware of changes made in the block device's buffers.
Pseudo-devicesPseudo-devices
Device nodes on Unix-like systems do not necessarily have to be the same as physical devices Corresponding. Nodes lacking this correspondence form a set of pseudo-devices. They provide various functions handled by the operating system. Some of the most commonly used (character-based) pseudo devices include:
/ dev / null - 接受并放弃所有输入; 不产生输出(总是在读取时返回文件结束指示) / dev / zero - 接受并放弃所有输入; 产生连续的NUL(零值)字节流 / dev / full - 读取时会产生连续的NUL(零值)字节流,并在写入时返回“磁盘已满”消息 / dev / random和/ dev / urandom - 它们产生一个可变长度的伪随机数字流。
Node created
Node created by mknod system Call create. The command line program used to create nodes is also called mknod. The usual file system calls (rename, unlink) and commands (mv, rm) can move or delete nodes. When passing option -R or -a when copying a device node, the cp -l command creates a new device node with the same properties as the original.
Some Unix versions include a script called makedev or MAKEDEV that creates all the necessary devices in the directory /dev. This only makes sense on systems whose devices are statically assigned a major number (e.g. by hardcoding it in their kernel module).
Block File Naming Rules
The following prefixes are used in the names of certain devices in the /dev hierarchy to identify the type of device:
lp:行式打印机(比较lp) pt:伪终端(虚拟终端) tty:终端 一些额外的前缀已经在一些操作系统中普遍使用: fb:帧缓冲区 fd :(平台)软盘,尽管这个缩写也常用来指文件描述符 高清:(“经典”)IDE驱动程序(以前用于ATA硬盘驱动器,ATAPI光盘驱动器等) hda:第一个ATA通道上的主设备(通常由主编号3和次编号0标识) hdb:第一个ATA通道上的从属设备 hdc:第二个ATA通道上的主设备 hdc1:此磁盘上的第一个主分区(示例) hdc5:扩展分区中的第一个逻辑驱动器(示例) hdd:第二个ATA通道上的从属设备 parport,pp:并行端口 SCSI驱动程序,也被libATA(现代PATA / SATA驱动程序),USB,IEEE 1394等使用。 sd:大容量存储驱动程序 sda:首次注册的设备 sda4:此磁盘上的最后一个分区(示例) sda6:扩展分区中的第二个逻辑驱动器(示例) sdb,sdc等:第二,第三等注册设备 ses:机箱驱动程序 sg:通用SCSI层 sr:“ROM”驱动程序(面向数据的光盘驱动器; scd只是一个辅助别名) st:磁带驱动器 tty:终端 ttyS :(平台)串口驱动程序 ttyUSB:USB串口转换器,调制解调器等
The canonical list of prefixes used in Linux can be found in the Linux Device List, which is the official registry of assigned device numbers and /dev directory nodes for the Linux operating system.
For most devices, this prefix is followed by a number that uniquely identifies the specific device. For hard drives, letters are used to identify the device, and numbers are used to identify the partitions. Therefore, the file system can find the area on the disk through /dev/sda3; associate /dev/pts/14 with the networked terminal session.
Related commands for operating device files
mknod
Everyone If you are interested in embedded, you can check how to use this command by yourself.
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of what is linux device file. For more information, please follow other related articles on the PHP Chinese website!