|
|
As mentioned above, within the UNIX system name space, files and devices look similar. For example, the commands cat /dev/tty01 and cat myfile both produce a stream of output. However, there is a difference between these two commands; their apparent equivalence is the result of some intricate work on the part of the operating system.
A device file is a special type of file. Rather than pointing to an inode which points to some data blocks, a device file points to an inode that contains some associated information: a major device number (which defines the type of device it is connected to), and a minor device number (which identifies a particular device of that type). When you carry out a file operation on a device file, the system uses the major number to determine which device driver to use to read data from or write data to the device. (The minor number is used internally by the device driver.)
Device files are created with mknod and cannot be manipulated like ordinary files, although you can rename them or create links to them.
Device files are typically kept in the directory /dev. They include identifiers that can be used to read from and write to kernel memory (/dev/kmem), hard disk drives (in ``raw'' or ``block'' mode, for example /dev/rhd00 for raw access to drive unit 0), and all the terminals, floppy disk drives, tape drives, and other components of the computer. Block devices write through the cache, providing fast, high level access. Raw device files bypass the buffer cache but are more flexible. Raw devices are therefore sometimes used by special applications like databases, which maintain their own high performance cache for the hardware they use for data storage.
This has some useful applications. You can send messages to a terminal by redirecting the output of a cat command to that terminal's device file. Alternatively, you can mount a filesystem on a subdirectory of your root filesystem, transparently adding another disk drive to the system. All you need do is create an empty subdirectory, then issue the mount command with the device file that refers to the additional filesystem as one of the parameters. The new filesystem is then invisibly attached to the root filesystem at the mount point you created.
As noted above, links between files are simply filenames that share the same inode. Inodes are only unique within a given filesystem. It is therefore impossible for a normal link to cross a filesystem boundary. However, a symbolic link can be used instead. A symbolic link is created using the -s flag to ln; instead of pointing to the inode of the file, the symbolic link points to a short file containing a reference to the filesystem and inode of the linked file.
While device files may seem somewhat obscure, they are one of the most important features of the operating system because they allow you to extend it. Your system administrator can add components to the computer, then create a device file through which they can be accessed. This is one of the ways in which the UNIX system is uniquely scalable.