17.7.4. Character devices

Character devices can have arbitrary File operations associated to them:

./character_device.sh
echo $?

Outcome: the test passes:

0

Sources:

Unlike procfs entires, character device files are created with userland mknod or mknodat syscalls:

mknod </dev/path_to_dev> c <major> <minor>

Intuitively, for physical devices like keyboards, the major number maps to which driver, and the minor number maps to which device it is.

A single driver can drive multiple compatible devices.

The major and minor numbers can be observed with:

ls -l /dev/urandom

Output:

crw-rw-rw-    1 root     root        1,   9 Jun 29 05:45 /dev/urandom

which means:

  • c (first letter): this is a character device. Would be b for a block device.

  • 1, 9: the major number is 1, and the minor 9

To avoid device number conflicts when registering the driver we:

  • ask the kernel to allocate a free major number for us with: register_chrdev(0

  • find ouf which number was assigned by grepping /proc/devices for the kernel module name