Table of Contents
Linux device file (Device file)

what is linux device file

Mar 24, 2022 pm 01:08 PM
linux

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.

what is linux device file

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

Linux device file (Device file)

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.

what is linux device fileFigure 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 - 它们产生一个可变长度的伪随机数字流。
Copy after login

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串口转换器,调制解调器等
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Centos options after stopping maintenance Centos options after stopping maintenance Apr 14, 2025 pm 08:51 PM

CentOS has been discontinued, alternatives include: 1. Rocky Linux (best compatibility); 2. AlmaLinux (compatible with CentOS); 3. Ubuntu Server (configuration required); 4. Red Hat Enterprise Linux (commercial version, paid license); 5. Oracle Linux (compatible with CentOS and RHEL). When migrating, considerations are: compatibility, availability, support, cost, and community support.

How to install centos How to install centos Apr 14, 2025 pm 09:03 PM

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

What to do if the docker image fails What to do if the docker image fails Apr 15, 2025 am 11:21 AM

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

See all articles