Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does the linux device file refer to

2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what does the linux device file refer to". In the operation of the actual case, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In linux, the device file is the interface of the device driver, allowing the application to interact with the device using its device driver through standard input / output system calls. There are two kinds of commonly used device files in linux system: 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.

Linux device File (Device file)

Overview of device Fil

In a Unix-like operating system, a device file or special file is the interface to a device driver and appears in the file system as if it were a normal file. There are also special files in MS-DOS,OS / 2 and Microsoft Windows. These special files allow applications to interact with devices using their device drivers through standard input / output system calls. The use of standard system calls simplifies many programming tasks and enables a consistent user-space I / O mechanism regardless of the characteristics and functions of the device.

Device files usually 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. In addition, device files are useful for accessing system resources that are independent of any actual device, such as data receivers and random number generators.

Figure 1 relationship between applications, block files, drivers, and hardware

There are two kinds of device files commonly used in Unix-like operating systems, which are called 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. Unlike named pipes, they can be called device-specific files, but named pipes are not connected to the device, but they are not ordinary files.

In some Unix-like systems, most device files are managed as part of the virtual file system traditionally installed in / dev and may be associated with a control daemon that monitors hardware additions and deletions at run time and makes changes to the device file system accordingly, if the kernel does not automatically change the device file system The control daemon may call scripts in the system or user space to handle special device requirements.

The FreeBSD and DragonFly BSD implementations have named the virtual device file system devfs and the associated daemon devd. Linux is mainly implemented in user space called udev, but there are many variations. Darwin and operating systems such as macOS, which is based on it, have pure kernel-based device file systems.

In Unix systems that support chroot process isolation, such as Solaris Containers, each chroot environment usually requires its own / dev;-mount points that will be visible on the host operating system on each node in the global file system tree. By restricting the filling of device nodes into the chroot instance of / dev, hardware isolation can be enforced by the chroot environment (the program cannot be inserted into the hardware, it can neither see nor access the name-this is a stronger form of access control than the Unix file system).

The device node corresponds to the resources that have been allocated by the operating system kernel. Unix identifies these resources by a primary number and a secondary number, which are stored as part of the node structure. The allocation of these numbers occurs uniquely on different operating systems and different computer platforms. Typically, the primary number identifies the device driver, and the secondary number identifies the specific device controlled by the driver (which may exceed many devices): in this case, the system may pass the secondary number to the driver. However, in the case of dynamically allocating numbers, this may not be the case (for example, on FreeBSD 5 and later).

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, their names are quite counterintuitive for historical reasons, so it is often incorrect to explain the differences between the two.

Block file classification

Character device (Character devices)

Character special files or character devices provide unbuffered and direct access to hardware devices. They do not necessarily allow the program to read or write a single character at a time; it depends on the device in question. For example, the character device of a hard disk usually requires that all read and write operations be aligned to the block boundary and is absolutely not allowed to read a single byte.

Character devices are sometimes referred to as raw devices to avoid confusion about the fact that character devices around a block-based hardware usually require programs to read and write aligned blocks.

Block device (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 programmers to read or write blocks of any size, including individual characters / bytes, and any alignment. The disadvantage is that because the block device is buffered, the programmer does not know how long it will take before the write data is transferred from the kernel buffer to the actual device, or in what order will two separate writes actually reach the physical device? In addition, if the same hardware exposes characters and block devices, there is a risk of data corruption because the client uses the character device without knowing the changes made in the block device's buffer.

Pseudo device (Pseudo-devices)

Device nodes on Unix-like systems do not necessarily correspond to physical devices. The nodes that lack this correspondence form a set of pseudo devices. They provide a variety of functions handled by the operating system. Some of the most commonly used (character-based) pseudo devices include:

/ dev / null-accept and discard all inputs; no output (always return file end instruction on read) / dev / zero-accept and discard all inputs Generates a continuous NUL (zero) byte stream / dev / full-produces a continuous NUL (zero) byte stream on read and returns "disk full" messages / dev / random and / dev / urandom on write-they produce a pseudorandom stream of variable length.

Node creation

The node is created by the mknod system call. The command line program used to create nodes is also known as mknod. The usual file system calls (rename, unlink) and commands (mv,rm) can move or delete nodes. When you pass the option-R or-a when copying a device node, the cp-l command creates a new device node with the same attributes as the original.

Some versions of Unix contain a script called makedev or MAKEDEV to create all the necessary devices in the directory / dev. It makes sense only on systems where devices are statically assigned major numbers (for example, by hard-coding them in their kernel modules).

Block file naming rules

The following prefix is used for the names of some devices in the / dev hierarchy to identify the type of device:

Lp: line printer (compare lp) pt: pseudo terminal (virtual terminal) tty: terminal some additional prefixes have been widely used in some operating systems: fb: frame buffer fd: (platform) floppy disk, although this acronym is also often used to refer to file descriptor HD: ("classic") IDE driver (formerly used for ATA hard drives) ATAPI CD drive, etc.) hda: master device on the first ATA channel (usually identified by major number 3 and minor number 0) hdb: slave device on the first ATA channel hdc: master device on the second ATA channel hdc1: first primary partition on this disk (example) hdc5: first logical drive in the extended partition (example) hdd: slave device on the second ATA channel parport,pp: parallel port SCSI driver It is also used by libATA (modern PATA / SATA driver), USB,IEEE 1394, etc. Sd: mass storage driver sda: first registered device sda4: last partition on this disk (example) sda6: second logical drive in extended partition (example) sdb,sdc etc.: second, third registered device ses: chassis driver sg: generic SCSI layer sr: "ROM" driver (data-oriented CD-ROM drive Scd is just a secondary alias) st: tape driver tty: terminal ttyS: (platform) serial port driver ttyUSB:USB serial converter, modem, etc.

The list of prefix specifications used in Linux can be found in the Linux device list, which is the official registry of the assigned device number of the Linux operating system and the / dev directory node.

For most devices, the prefix is followed by a number that uniquely identifies a particular device. For hard drives, use letters to identify the device, and then use numbers to identify the partition. Therefore, the file system can find the area on the disk through / dev/sda3; associate / dev/pts/14 with a networked terminal session.

Commands for manipulating device files

Mknod

This is the end of the content of "what is the linux device file"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report