Home > System Tutorial > LINUX > The difference between block and string devices in Linux and how to use them

The difference between block and string devices in Linux and how to use them

WBOY
Release: 2024-02-11 12:48:30
forward
928 people have browsed it

As a Linux user, have you ever encountered a situation where you need to use block devices or character devices? Although block devices and character devices are similar in appearance and functionality, they are very different in how they are implemented and used. Today, we will take you to have an in-depth understanding of block and string devices in Linux, explore their differences and how to use them correctly.

The difference between block and string devices in Linux and how to use them

Devices in the system that can access fixed-size data pieces (chunks) randomly (not in order) are called block devices, and these data pieces are called blocks. The most common block device is the hard disk, but there are many other block devices such as floppy disk drives, CD-ROM drives, and flash memory. Note that they are all used in a way that mounts the file system - this is how block devices are generally accessed.

Another basic device type is the character device. Character devices are accessed in an orderly manner according to the character stream. Serial ports and keyboards are all character devices. If a hardware device is accessed as a character stream, it should be classified as a character device; conversely, if a device is accessed randomly (out of order), then it is a block device.

The fundamental difference between these two types of devices is whether they can be accessed randomly—in other words, whether access to the device can jump from one location to another at will. For example, a device like a keyboard provides a data stream. When you type the string "fox", the keyboard driver will return the data stream consisting of three characters in exactly the same order as the input. It makes no sense to ask the keyboard driver to read the string out of order, or to read other characters. So the keyboard is a typical character device, and what it provides is the character stream input by the user from the keyboard. Reading from the keyboard results in a stream of characters, first "f", then "o", then "x", and finally the end of file (EOF). When no one is typing on the keyboard, the character stream is empty. The situation with hard disk devices is different. The driver of the hard disk device may request to read the contents of any block on the disk, and then turn to read the contents of other blocks. The positions of the read blocks on the disk do not have to be consecutive, so the hard disk can be randomly accessed. Instead of being accessed as a stream, it's obviously a block device.

Kernel management of block devices is much more detailed than management of character devices. The issues that need to be considered and the work completed are much more complex than those of character devices. This is because a character device only needs to control one location—the current location—while a block device must be able to access locations that can be moved back and forth between different sections of the medium. So in fact, the kernel does not need to provide a dedicated subsystem to manage character devices, but the management of block devices must have a dedicated subsystem that provides services. Not only is the complexity of block devices much higher than that of character devices, the more important reason is that block devices have very high requirements on execution performance; every additional use of the hard disk will improve the performance of the entire system, and its effect Much greater than doubling the keyboard throughput speed. Additionally, as we will see, the complexity of block devices leaves a lot of room for such optimizations.

Naked device

To put it simply, block devices can be randomly accessed, but character devices cannot be randomly accessed. How to explain the naked device?

Can't raw devices, such as raw disk devices, be read randomly? Then use raw equipment to create a 2G data file in the database. In order to access the last data block, Oracle has to read all the previous data blocks. Obviously it is not consistent with the facts. If it is explained this way, the operating system cannot be random. Reading does not mean that the database cannot also read randomly.

Block devices are read through the system cache, not directly from the physical disk. Character devices can be read directly from the physical disk without going through the system cache. (Such as keyboard, directly interrupt accordingly)

Through the introduction of this article, we understand that there are big differences in the implementation and use of block devices and string devices. In short, the block device is oriented to the input and output of block data, while the string device is oriented to the input and output of character stream type. At the same time, in actual applications, we can choose the appropriate device type according to our own needs, and use the corresponding commands to create, mount, format and other operations. In general, mastering the use of block and string devices is of great significance to improving the performance optimization and management level of Linux systems.

The above is the detailed content of The difference between block and string devices in Linux and how to use them. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template