Device Files
Linux exposes devices as files, and the /dev directory contains a number of special files that represent devices. These are not actual files as we know them, but they appear as files - for example, /dev/sda represents the first SATA drive in the system. If you wanted to partition it, you could start a partition editor and tell it to edit /dev/sda.
This directory also contains pseudo-devices, which are virtual devices that don't actually correspond to hardware. For example, /dev/random produces random numbers. /dev/null is a special device that produces no output and automatically discards all input - when you pipe the output of a command to /dev/null, you discard it.
Contains references to all the CPU peripheral hardware, which are represented as files with special properties.
USER - The current logged in user.
HOME - The home directory of the current user.
EDITOR - The default file editor to be used. This is the editor that will be used when you type edit in your terminal.
SHELL - The path of the current user's shell, such as bash or zsh.
LOGNAME - The name of the current user.
PATH - A list of directories to be searched when executing commands. When you run a command the system will search those directories in this order and use the first found executable.
LANG - The current locales settings.
TERM - The current terminal emulation.
MAIL - Location of where the current user's mail is stored.
One of the unique things about Unix as an operating system is that regards everything as a file. Files can be divided into three categories; ordinary or plain files, directories, and special or device files.
Directories in Unix are properly known as directory files. They contain information such as owner, permissions, and size for a set of files.
Ordinary or plain files in Unix are not all text files. They may also contain ASCII text, binary data, and program input or output. Executable binaries (programs) are also files, as are commands. When a user enters a command, the associated file is retrieved and executed. This is an important feature and contributes to the flexibility of Unix.
Special files are also known as device files. In Unix all physical devices are accessed via device files; they are what programs use to communicate with hardware. Files hold information on location, type, and access mode for a specific device. There are two types of device files; character and block, as well as two modes of access.
Block device files are used to access block device I/O. Block devices do buffered I/O, meaning that the the data is collected in a buffer until a full block can be transfered.
Character device files are associated with character or raw device access. They are used for unbuffered data transfers to and from a device. Rather than transferring data in blocks the data is transfered character by character. One transfer can consist of multiple characters.
4. So what about a device that could be accessed in character or block mode? How many device files would it have?
- One.
- Two.
- There are no such devices.
Some devices, such as disk partitions, may be accessed in block or character mode. Because each device file corresponds to a single access mode, physical devices that have more than one access mode will have more than one device file.
Device files are found in the /dev directory. Each device is assigned a major and minor device number. The major device number identifies the type of device, i.e. all SCSI devices would have the same number as would all the keyboards. The minor device number identifies a specific device, i.e. the keyboard attached to this workstation.
Device files are created using the mknod command. The form for this command is:
mknod device-name type major minor
- device-name is the name of the device file
- type is either "c" for character or "b" for block
- major is the major device number
- minor is the minor device number
The major and minor device numbers are indexed to device switches. There are two types of device switches; cdevsw for character devices and bdevsw for block devices. These switches are kernel structures that hold the names of all the control routines for a device and tell the kernel which driver module to execute. Device switches are actually tables that look something like this:
- 0 keyboard
- 1 SCSIbus
- 2 tty
- 3 disk
Using the ls command in the /dev directory will show entries that look like:
brw-r----- 1 root sys 1, 0 Aug 31 16:01 /dev/sd1aThe "b" before the permissions indicates that this is a block device file. When a user enters /dev/sd1a the kernel sees the file opening, realizes that it's major device number 1, and calls up the SCSIbus function to handle it.
Terms used: SCSI, tty, ASCII, I/O.