Special Files: Character Mode and Block Mode Files

As already stated, such files are either created by the system or peripherals on your machine. We also mentioned that the contents of block mode character files were buffered, while character mode files were not. In order to illustrate this, insert a floppy into the drive and type the following command twice:

$ dd if=/dev/fd0 of=/dev/null

You should have observed the following: the first time the command was launched, the entire content of the floppy was read. The second time you executed the command, there was no access to the floppy drive at all. This is because the content of the floppy was buffered the first time you launched the command –– and you did not change anything on the floppy between the two instances.

But now, if you want to print a big file this way (yes it will work):

$ cat /a/big/printable/file/somewhere >/dev/lp0

The command will take as much time, whether you launch it once, twice or fifty times. This is because /dev/lp0 is a character mode file, and its contents are not buffered.

The fact that block mode files are buffered has a nice side effect: not only are reads buffered, but writes are buffered too. This allows for writes to the disks to be asynchronous: when you write a file on disk, the write operation itself is not immediate. It will only occur when the Linux kernel decides to execute the write to the hardware.

Finally, each special file has a major and minor number. On a ls -l output, they appear in place of the size, as the size for such files is irrelevant:

ls -l /dev/ide/host0/bus0/target0/lun0/disc /dev/printers/0 
brw-------  1 root root 3, 0 dic 31  1969 /dev/ide/host0/bus0/target0/lun0/disc
crw-rw----  1 lp   sys  6, 0 dic 31  1969 /dev/printers/0

Here, the major and minor of /dev/ide/host0/bus0/target0/lun0/disc are 3 and 0, whereas for /dev/printers/0, they are 6 and 0. Note that these numbers are unique per file category, which means that there can be a character mode file with major 3 and minor 0 (this file actually exists: /dev/pty/s0), and similarly, there can be a block mode file with major 6 and minor 0. These numbers exist for a simple reason: it allows the kernel to associate the correct operations to these files (that is, to the peripherals these files refer to): you don't handle a floppy drive the same way as, say, a SCSI hard drive.