The difference between a Linux character device and a block device

Source: Internet
Author: User

Devices in the system that can randomly (and do not need to sequentially) access fixed-size data slices (chunks) are called block devices, which are called blocks. The most common block devices are hard disks, and in addition, there are many other block devices, such as floppy drives, CD-ROM drives, flash memory, and so on. Note that they are all used in a way that installs the file system-this is also the general way to access the block device.

Another basic device type is the character device. Character devices are ordered to be accessed in a character stream, such as a serial and keyboard. If a hardware device is accessed in a character stream, it should be attributed to a character device, and conversely, if a device is randomly (unordered) accessed, it belongs to the block device.

The fundamental difference between these two types of devices is whether they can be randomly accessed--in other words, whether or not to jump from one location to another at random when accessing the device. For example, the keyboard provides a data stream, and when you type the string "Fox", the keyboard driver returns the three-character stream in exactly the same order as the input. It makes no sense to have the keyboard driver scramble the sequence to read a string, or read other characters. So the keyboard is a typical character device that provides a stream of characters that the user enters from the keyboard. Reading a keyboard results in a stream of characters, first "F", then "O", and finally "X", ultimately the end of the file (EOF). When no one knocks on the keyboard, the character stream is empty. The hard disk device is not the same. The drive of the hard disk device may require reading the contents of any block on the disk, and then go to read the contents of the other block, while the block being read is not necessarily contiguous on the disk, so the hard disk can be randomly accessed instead of being accessed in a stream, apparently it is a block device.

Kernel management block devices are much more nuanced than managing character devices, and the issues to consider and the work done are much more complex than character devices. This is because the character device only needs to control one location-the current position-and the location where the block device accesses must be able to move around the different intervals of the media. So in fact the kernel does not have to provide a specialized subsystem to manage character devices, but the management of block devices must have a dedicated subsystem to provide services. Not only because the complexity of the block device is much higher than the character device, the more important reason is that the block device has a high performance requirement, and the performance of the whole system is improved by the use of every single part of the hard disk, the effect is much larger than the keyboard throughput speed. In addition, we will see that the complexity of block devices will leave a lot of room for this optimization.

Simply put, the block device can be randomly accessed, and the character device is not random access, the bare device how to explain it?

are bare devices, such as disk bare devices, not readable at random? That in the database with a bare device to build a 2g data file, in order to access the last block of data, Oracle will also have to read all the previous blocks of data, obviously does not conform to the facts, if this explanation, the operating system can not read randomly, does not mean that the database can not be randomly read.

Block devices are read through the system cache, not directly to the physical disk. Character devices can be read directly from a physical disk without being cached by the system. (such as keyboard, directly corresponding interrupt)

The difference between a Linux character device and a block device

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.