Skip to main content
p. 48

3.1 Device Files

It’s easy to manipulate most devices on a Unix system because the kernel presents many of the device I/O interfaces to user processes as files. These device files are sometimes called device nodes. Aside from programmers using regular file operations to work with devices, some devices are also accessible to standard programs like cat, so you don’t have to be a programmer to use a device. However, there is a limit to what you can do with a file interface, so not all devices or device capabilities are accessible with standard file I/O.

Linux uses the same design for device files as do other Unix flavors. Device files are in the /dev directory, and running ls /dev reveals quite a few files in /dev. So how do you work with devices?

To get started, consider this command:

$ echo blah blah > /dev/null

Like any other command with redirected output, this sends some stuff from the standard output to a file. However, the file is /dev/null, a device, so the kernel bypasses its usual file operations and uses a device driver on data written to this device. In the case of /dev/null, the kernel simply accepts the input data and throws it away.

To identify a device and view its permissions, use ls -l. Here are some examples:

$ ls -l
brw-rw---- 1 root disk 8, 1 Sep 6 08:37 sda1
crw-rw-rw- 1 root root 1, 3 Sep 6 08:37 null
prw-r--r-- 1 root root 0 Mar 3 19:17 fdata
srw-rw-rw- 1 root root 0 Dec 18 07:43 log

Note the first character of each line (the first character of the file’s mode). If this character is b, c, p, or s, the file is a device. These letters stand for block, character, pipe, and socket, respectively:

Block device

Programs access data from a block device in fixed chunks. The sda1 in the preceding example is a disk device, a type of block device. Disks can be easily split up into blocks of data. Because a block device’s total size is fixed and easy to index, processes have quick random access to any block in the device with the help of the kernel.

p. 49

Character device

Character devices work with data streams. You can only read characters from or write characters to character devices, as previously demonstrated with /dev/null. Character devices don’t have a size; when you read from or write to one, the kernel usually performs a read or write operation on it. Printers directly attached to your computer are represented by character devices. It’s important to note that during character device interaction, the kernel cannot back up and reexamine the data stream after it has passed data to a device or process.

Pipe device

Named pipes are like character devices, with another process at the other end of the I/O stream instead of a kernel driver.

Socket device

Sockets are special-purpose interfaces that are frequently used for interprocess communication. They’re often found outside of the /dev directory. Socket files represent Unix domain sockets; you’ll learn more about those in Chapter 10.

In file listings from ls -l of block and character devices, the numbers before the dates are the major and minor device numbers that the kernel uses to identify the device. Similar devices usually have the same major number, such as sda3 and sdb1 (both of which are hard disk partitions).

note

Not all devices have device files, because the block and character device I/O interfaces are not appropriate in all cases. For example, network interfaces don’t have device files. It is theoretically possible to interact with a network interface using a single character device, but because it would be difficult, the kernel offers other I/O interfaces.