3

From https://unix.stackexchange.com/a/472920/674

A "character device" and "block device" are abstractions, usually used in Unix-style systems in classifying various devices.

Is a device being a block or character device determined by hardware (either device or device controller), device driver, file system, OS kernel I/O subsystem, and/or something else?

I thought they are concepts of purely hardware (either device or device controller), independent of device driver, file system, and OS kernel I/O subsystem.

From Are there implications between block/character device and random/sequential access?

the only real visible difference is that block devices use the buffer cache and character devices don't. (And some block device drivers may offer the option of bypassing the buffer cache on a per-file basis).

Is the buffer cache used by block devices provided by device driver, file systems, or by hardware (either the device or device controller)? If by the hardware, then does it mean that a device being a block or character device is determined purely by hardware (either device or device controller)?

Thanks.

5

No, it is determined by the OS.

FreeBSD Architecture Handbook dated 2018-09-23 11:38:04.

9.4 Block Devices (Are Gone)

Other UNIX® systems may support a second type of disk device known as block devices. Block devices are disk devices for which the kernel provides caching [...] FreeBSD dropped support for cached disk devices as part of the modernization of the disk I/O infrastructure.

FreeBSD still has "raw" devices, which are character devices that allow reading and writing disk blocks.

Do not be confused by the names. Block devices provide buffering - which allows you to read and write in units of bytes. Raw devices are a type of character device - but they require you to read and write in units of blocks. The block size depends on the specific disk hardware.

"Character devices" were probably originally named after teletype terminals, which physically work one character at a time. Block devices were a specific case where read() and write() interacted with the buffer cache, instead of directly with the device driver. Character devices became the general case, used for every other device file. The interface is flexible, because as well as device-specific read() and write(), it can support arbitrary commands with ioctl().

https://en.wikipedia.org/wiki/Device_file#Block_devices

Most systems create both block and character devices to represent hardware like hard disks. FreeBSD and Linux notably do not; the former has removed support for block devices,[6] while the latter creates only block devices. In Linux, to get a character device for a disk one must use the "raw" driver, though one can get the same effect as opening a character device by opening the block device with the Linux-specific O_DIRECT flag.

Block special files or block devices provide buffered access to hardware devices, and provide some abstraction from their specifics.[5] Unlike character devices, block devices will always allow the programmer to read or write a block of any size (including single characters/bytes) and any alignment. The downside is that because block devices are buffered, the programmer does not know how long it will take before written data is passed from the kernel's buffers to the actual device, or indeed in what order two separate writes will arrive at the physical device. Additionally, if the same hardware exposes both character and block devices, there is a risk of data corruption due to clients using the character device being unaware of changes made in the buffers of the block device.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.