


The difference between block and string devices in Linux and how to use them
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.

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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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).

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).

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.

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)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

How to back up VS Code configurations and extensions? Manually backup the settings file: Copy the key JSON files (settings.json, keybindings.json, extensions.json) to a safe location. Take advantage of VS Code synchronization: enable synchronization with your GitHub account to automatically back up all relevant settings and extensions. Use third-party tools: Back up configurations with reliable tools and provide richer features such as version control and incremental backups.
