Mehdi Akiki
Published on

Understanding Character and Block Devices in Linux

Authors
  • Mehdi Akiki avatar
    Name
    Mehdi Akiki
    Twitter

Reference · Through the layers

In Linux, hardware devices are represented as special files within the /dev directory. These device files fall into two main categories: character devices and block devices. Understanding their differences is essential for working effectively with low-level hardware interactions and system administration.

What Are Character Devices?

Character devices handle data transfer one byte (or character) at a time. They are best suited for devices requiring sequential access and immediate processing of data.

Key Characteristics:

  • Sequential Access: Data is read or written in a linear fashion.
  • No Buffering: Transfers occur without caching or intermediate storage.
  • System Calls: Use read() and write() for interaction.

Examples:

  • /dev/tty: Terminal devices (e.g., console input and output).
  • /dev/input: Input devices like keyboards and mice.
  • /dev/random: Random number generator.
  • /dev/pts: Pseudo-terminal devices for SSH sessions.

Use Case:

Character devices are ideal for peripherals like serial ports, keyboards, and audio devices, where data is processed in real-time.


What Are Block Devices?

Block devices handle data transfer in fixed-size blocks (usually 512 bytes or multiples). They are optimized for devices requiring random access and buffered I/O.

Key Characteristics:

  • Random Access: Specific blocks of data can be accessed directly.
  • Buffered I/O: Data is cached to improve performance.
  • System Calls: Use pread() and pwrite() for access.

Examples:

  • /dev/sda: Hard disk drives (HDDs) and solid-state drives (SSDs).
  • /dev/nvme: NVMe storage devices.
  • /dev/loop: Loopback devices for mounting files as block devices.
  • /dev/dm-*: Device mapper files for LVM volumes.

Use Case:

Block devices are ideal for storage media like disks and filesystems, where efficient data retrieval and modification are critical.


Key Differences Between Character and Block Devices

| Feature           | Character Devices         | Block Devices           |
| ----------------- | ------------------------- | ----------------------- |
| **Data Transfer** | Byte-by-byte              | Block-by-block          |
| **Caching**       | No caching                | Buffered I/O            |
| **Access**        | Sequential                | Random                  |
| **Use Cases**     | Input/output peripherals  | Storage devices         |
| **Examples**      | `/dev/tty`, `/dev/random` | `/dev/sda`, `/dev/nvme` |

Inspecting Devices in Linux

To view a list of character and block devices, you can use the /proc/devices file:

cat /proc/devices

Example Output (from my machine):

   1   │ Character devices:
   2   │   1 mem
   3   │   4 /dev/vc/0
   4   │   4 tty
   5   │   4 ttyS
   6   │   5 /dev/tty
   7   │   5 /dev/console
   8   │   5 /dev/ptmx
   9   │   7 vcs
  10   │  10 misc
  11   │  13 input
  12   │  29 fb
  13   │  81 video4linux
  14   │  90 mtd
  15   │ 116 alsa
  16   │ 128 ptm
  17   │ 136 pts
  18   │ 166 ttyACM
  19   │ 180 usb
  20   │ 188 ttyUSB
  21   │ 189 usb_device
  22   │ 202 cpu/msr
  23   │ 203 cpu/cpuid
  24   │ 216 rfcomm
  25   │ 226 drm
  26   │ 234 mei
  27   │ 235 nvme-generic
  28   │ 236 nvme
  29   │ 237 aux
  30   │ 238 cec
  31   │ 239 binder
  32   │ 240 hidraw
  33   │ 241 wwan_port
  34   │ 242 bsg
  35   │ 243 watchdog
  36   │ 244 remoteproc
  37   │ 245 ptp
  38   │ 246 pps
  39   │ 247 lirc
  40   │ 248 rtc
  41   │ 249 dma_heap
  42   │ 250 dax
  43   │ 251 dimmctl
  44   │ 252 ndctl
  45   │ 253 tpm
  46   │ 254 gpiochip
  47   │ 510 iio
  48   │ 511 media
  49   │
  50   │ Block devices:
  51   │   7 loop
  52   │   8 sd
  53   │  65 sd
  54   │  66 sd
  55   │  67 sd
  56   │  68 sd
  57   │  69 sd
  58   │  70 sd
  59   │  71 sd
  60   │ 128 sd
  61   │ 129 sd
  62   │ 130 sd
  63   │ 131 sd
  64   │ 132 sd
  65   │ 133 sd
  66   │ 134 sd
  67   │ 135 sd
  68   │ 254 device-mapper
  69   │ 259 blkext

This output shows the major device numbers assigned to various device drivers.

FAQ

1. What are character and block devices in Linux?

Character and block devices are special files in Linux that represent hardware devices. They allow the operating system to interact with hardware, abstracting the complexities of device communication.

  • Character devices transfer data byte-by-byte and are suitable for sequential data access (e.g., keyboards, serial ports).
  • Block devices transfer data in fixed-size blocks and are ideal for random access (e.g., hard drives, SSDs).

2. Where are these devices located in Linux?

Both character and block devices are represented as files in the /dev directory. For example:

  • Character devices: /dev/tty, /dev/input
  • Block devices: /dev/sda, /dev/nvme

3. How do I check the list of character and block devices on my system?

Use the following command to view registered devices:

cat /proc/devices

This will list major numbers and their corresponding device types.

4. What are examples of character devices?

Some common examples include:

  • /dev/tty: Terminal input/output devices.
  • /dev/random: Random number generator.
  • /dev/input: Input devices like keyboards and mice.