Английская Википедия:Device file

Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску

Шаблон:Short description Шаблон:Distinguish Шаблон:Use dmy dates

In Unix-like operating systems, a device file or special file is an interface to a device driver that appears in a file system as if it were an ordinary file. There are also special files in DOS, OS/2, and Windows. These special files allow an application program to interact with a device by using its device driver via standard input/output system calls. Using standard system calls simplifies many programming tasks, and leads to consistent user-space I/O mechanisms regardless of device features and functions.

Overview

Device files usually provide simple interfaces to standard devices (such as printers and serial ports), but can also be used to access specific unique resources on those devices, such as disk partitions. Additionally, device files are useful for accessing system resources that have no connection with any actual device, such as data sinks and random number generators.

There are two general kinds of device files in Unix-like operating systems, known as character special files and block special files. The difference between them lies in how much data is read and written by the operating system and hardware. These together can be called device special files in contrast to named pipes, which are not connected to a device but are not ordinary files either.

MS-DOS borrowed the concept of special files from Unix but renamed them devices.[1] Because early versions of MS-DOS did not support a directory hierarchy, devices were distinguished from regular files by making their names reserved words, for example: the infamous CON. These were chosen for a degree of compatibility with CP/M and are still present in modern Windows for backwards compatibility.

In some Unix-like systems, most device files are managed as part of a virtual file system traditionally mounted at /dev, possibly associated with a controlling daemon, which monitors hardware addition and removal at run time, making corresponding changes to the device file system if that's not automatically done by the kernel, and possibly invoking scripts in system or user space to handle special device needs. The FreeBSD, DragonFly BSD and Darwin have a dedicated file system devfs; device nodes are managed automatically by this file system, in kernel space. Linux used to have a similar devfs implementation, but it was abandoned later, and then removed since version 2.6.17;[2] Linux now primarily uses a user space implementation known as udev, but there are many variants.

In Unix systems which support chroot process isolation, such as Solaris Containers, typically each chroot environment needs its own /dev; these mount points will be visible on the host OS at various nodes in the global file system tree. By restricting the device nodes populated into chroot instances of /dev, hardware isolation can be enforced by the chroot environment (a program can not meddle with hardware that it can neither see nor name—an even stronger form of access control than Unix file system permissions).

MS-DOS managed hardware device contention (see terminate-and-stay-resident program) by making each device file exclusive open. An application attempting to access a device already in use would discover itself unable to open the device file node. A variety of device driver semantics are implemented in Unix and Linux concerning concurrent access.[3]

Unix and Unix-like systems

Файл:Simplified Structure of the Linux Kernel.svg
A simplified structure of the Linux kernel. File systems are implemented as part of the I/O subsystem.

Device nodes correspond to resources that an operating system's kernel has already allocated. Unix identifies those resources by a major number and a minor number,[4] both stored as part of the structure of a node. The assignment of these numbers occurs uniquely in different operating systems and on different computer platforms. Generally, the major number identifies the device driver and the minor number identifies a particular device (possibly out of many) that the driver controls:[5] in this case, the system may pass the minor number to a driver. However, in the presence of dynamic number allocation, this may not be the case (e.g. on FreeBSD 5 and up).

As with other special file types, the computer system accesses device nodes using standard system calls and treats them like regular computer files. Two standard types of device files exist; unfortunately their names are rather counter-intuitive for historical reasons, and explanations of the difference between the two are often incorrect as a result.

Character devices

Character special files or character devices provide unbuffered, direct access to the hardware device. They do not necessarily allow programs to read or write single characters at a time; that is up to the device in question. The character device for a hard disk, for example, will normally require that all reads and writes be aligned to block boundaries and most certainly will not allow reading a single byte.

Character devices are sometimes known as raw devices to avoid the confusion surrounding the fact that a character device for a piece of block-based hardware will typically require programs to read and write aligned blocks.

Шаблон:AnchorBlock devices

Block special files or block devices provide buffered access to hardware devices, and provide some abstraction from their specifics.[6] Unlike character devices, block devices will always allow the programmer to read or write a block of any size (including single characters/bytes) and any alignment. The downside is that because block devices are buffered, the programmer does not know how long it will take before written data is passed from the kernel's buffers to the actual device, or indeed in what order two separate writes will arrive at the physical device. Additionally, if the same hardware exposes both character and block devices, there is a risk of data corruption due to clients using the character device being unaware of changes made in the buffers of the block device.

Most systems create both block and character devices to represent hardware like hard disks. FreeBSD and Linux notably do not; the former has removed support for block devices,[7] while the latter creates only block devices. In Linux, to get a character device for a disk, one must use the "raw" driver, though one can get the same effect as opening a character device by opening the block device with the Linux-specific Шаблон:Mono flag.

Pseudo-devices

Device nodes on Unix-like systems do not necessarily have to correspond to physical devices. Nodes that lack this correspondence form the group of pseudo-devices. They provide various functions handled by the operating system. Some of the most commonly used (character-based) pseudo-devices include:

Additionally, BSD-specific pseudo-devices with an Шаблон:Mono interface may also include:

Node creation

Nodes are created by the Шаблон:Mono system call. The command-line program for creating nodes is also called Шаблон:Mono. Nodes can be moved or deleted by the usual filesystem system calls (Шаблон:Mono, Шаблон:Mono) and commands (Шаблон:Mono, Шаблон:Mono).

Some Unix versions include a script named makedev or MAKEDEV to create all necessary devices in the directory Шаблон:Mono. It only makes sense on systems whose devices are statically assigned major numbers (e.g., by means of hardcoding it in their kernel module).

While some other Unix systems such as FreeBSD, used kernel-based device node management via devfs only, and not supporting manual node creation. Шаблон:Mono system call and Шаблон:Mono command exist to keep compatibility with POSIX, but manually created device nodes outside devfs will not function at all.[9]

Naming conventions Шаблон:Anchor

The following prefixes are used for the names of some devices in the Шаблон:Mono hierarchy, to identify the type of device:

Some additional prefixes have come into common use in some operating systems:

The canonical list of the prefixes used in Linux can be found in the Linux Device List, the official registry of allocated device numbers and Шаблон:Mono directory nodes for the Linux operating system.[10]

For most devices, this prefix is followed by a number uniquely identifying the particular device. For hard drives, a letter is used to identify devices and is followed by a number to identify partitions. Thus a file system may "know" an area on a disk as Шаблон:Mono, for example, or "see" a networked terminal session as associated with Шаблон:Mono.

On disks using the typical PC master boot record, the device numbers of primary and the optional extended partition are numbered 1 through 4, while the indexes of any logical partitions are 5 and onwards, regardless of the layout of the former partitions (their parent extended partition does not need to be the fourth partition on the disk, nor do all four primary partitions have to exist).

Device names are usually not portable between different Unix-like system variants, for example, on some BSD systems, the IDE devices are named Шаблон:Mono, Шаблон:Mono, etc.

Шаблон:Anchordevfs

devfs is a specific implementation of a device file system on Unix-like operating systems, used for presenting device files. The underlying mechanism of implementation may vary, depending on the OS.

Maintaining these special files on a physically-implemented file system such as a hard drive is inconvenient, and as it needs kernel assistance anyway, the idea arose of a special-purpose logical file system that is not physically stored.

Defining when devices are ready to appear is not trivial. The devfs approach is for the device driver to request creation and deletion of devfs entries related to the devices it enables and disables.

Шаблон:AnchorPC DOS, TOS, OS/2, and Windows

A device file is a reserved keyword used in PC DOS, TOS, OS/2, and Windows systems to allow access to certain ports and devices.

MS-DOS borrowed the concept of special files from Unix but renamed them devices.[1] Because early versions of MS-DOS did not support a directory hierarchy, devices were distinguished from regular files by making their names reserved words. This means that certain file names were reserved for devices, and should not be used to name new files or directories.[11] The reserved names themselves were chosen to be compatible with "special files" handling of PIP command in CP/M. There were two kinds of devices in DOS: Block Devices (used for disk drives) and Character Devices (generally all other devices, including COM and PRN devices).[12]

DOS uses device files for accessing printers and ports. Most versions of Windows also contain this support, which can cause confusion when trying to make files and folders of certain names, as they cannot have these names.[13] Versions 2.x of MS-DOS provide the AVAILDEV CONFIG.SYS parameter that, if set to FALSE, makes these special names only active if prefixed with \DEV\, thus allowing ordinary files to be created with these names.[14]

GEMDOS, the DOS-like part of Atari TOS, supported similar device names to DOS, but unlike DOS it required a trailing ":" character (on DOS, this is optional) to identify them as devices as opposed to normal filenames (thus "CON:" would work on both DOS and TOS, but "CON" would name an ordinary file on TOS but the console device on DOS). In MiNT and MagiC, a special UNIX-like unified filesystem view accessed via the "U:" drive letter also placed device files in "U:\DEV".

Device keyword[13] Use as input Use as output
CON Receives typed data until ^Z (Ctrl-Z) is pressed. Prints data to the console.
PRN[15] Шаблон:N/a Prints text to the printer, usually redirected to LPT1 or LST. Sometimes reconfigurable to other devices.[16][17][18]
AUX (not in OS/2[15]) Reads data from an auxiliary device, usually a serial device like COM1. Sometimes reconfigurable to other devices.[16][17][18] Sends data to an auxiliary device, usually a serial device like COM1. Sometimes reconfigurable to other devices.[16][17][18]
NUL Returns null or no data. Discards received data.
CLOCK$ (still named CLOCK in some versions of MS-DOS 2.11[19][16][17]) Шаблон:N/a Шаблон:N/a
KEYBD$ (only in multitasking MS-DOS) Шаблон:Dunno Шаблон:Dunno
KBD$ (only in OS/2[15]) Шаблон:Dunno Шаблон:Dunno
SCREEN$ (only in multitasking MS-DOS and OS/2[15]) Шаблон:Dunno Шаблон:Dunno
POINTER$ (only in OS/2[15]) Шаблон:Dunno Шаблон:Dunno
MOUSE$ (only in OS/2[15]) Шаблон:Dunno Шаблон:Dunno
$IDLE$ (only in DR-DOS (since 5.0) and Multiuser DOS (since Concurrent DOS 386) families) Шаблон:N/a Шаблон:N/a
CONFIG$ (only in MS-DOS 7.0 and higher) Шаблон:N/a Шаблон:N/a
LST (only in 86-DOS and DOS 1.x, also in Hewlett-Packard's MS-DOS 2.11 for the HP Portable Plus[16][17]) Returns no data. Sends data to the line printer. (LPT2 for Hewlett-Packard's MS-DOS 2.11[16][17])
PLT (only in Hewlett-Packard's MS-DOS 2.11 for the HP Portable Plus[16][17]) Returns no data. Sends data to the assigned plotter. The attached plotter device is reconfigurable.[16][17]
LPT1, LPT2, LPT3, and sometimes LPT4 (in DR-DOS 7.02 and higher and some versions of Multiuser DOS) Шаблон:N/a Sends data to the selected parallel port.
COM1, COM2, COM3, COM4 Reads data from the selected serial port. Sends data to the selected serial port.
82164A (only in Hewlett-Packard's MS-DOS 2.11 for the HP Portable Plus[16][17]) Redirects to COM2. Redirects to COM2.

Using shell redirection and pipes, data can be sent to or received from a device. For example, typing the following will send the file c:\data.txt to the printer:

TYPE c:\data.txt > PRN

PIPE, MAILSLOT, and MUP are other standard Windows devices.[20]

IOCS

The 8-bit operating system of Sharp pocket computers like the PC-E500, PC-E500S etc. consists of a BASIC interpreter, a DOS 2-like File Control System (FCS) implementing a rudimentary 12-bit FAT-like filesystem, and a BIOS-like Input/Output Control System (IOCS) implementing a number of standard character and block device drivers as well as special file devices including STDO:/SCRN: (display), STDI:/KYBD: (keyboard), COM: (serial I/O), STDL:/PRN: (printer), CAS: (cassette tape), E:/F:/G: (memory file), S1:/S2:/S3: (memory card), X:/Y: (floppy), SYSTM: (system), and NIL: (function).[21]

Шаблон:AnchorImplementations

Operating System Filesystem or managing software Standard mount point Author Notes
Linux 2.3.46pre5–2.6.17 devfs[22] and devfsd /dev Richard Gooch Implemented fully in the kernel, with optional daemon devfsd to handle device node events in user space.[23] Obsolete – users are encouraged to migrate to udev and/or devtmpfs.
Linux 2.5– udev on any fs, but usually tmpfs /dev Greg Kroah-Hartman, Kay Sievers and Dan Stekloff Implemented largely in user space, device information is gathered from sysfs. Device files can be stored on a conventional general-purpose file system, or in a memory file system (tmpfs).
Linux 2.6.32– devtmpfs with or without udev /dev Kay Sievers, Jan Blunck, Greg Kroah-Hartman A hybrid kernel/userspace approach of a device filesystem to provide nodes before udev runs for the first time[24]
Solaris devfs[25] /devices Sun Microsystems Introduced with dynamic loaded drivers in Solaris-2.1
FreeBSD 2.0– devfs /dev Poul-Henning Kamp Implemented fully in the kernel.
DragonFly BSD 2.3.2– devfs /dev Alex Hornung Implemented fully in the kernel.
macOS devfs /dev Apple Inc. Implemented fully in the kernel.
HP-UX B.11.31 devfs /dev HP Implemented fully in the kernel.
Plan 9 # Bell Labs Implemented in the kernel.
RISC OS DeviceFS Devices: Acorn Computers DeviceFS was started in 1991[26] and first appeared in RISC OS 3. It manages several device like special files, most commonly: Parallel, Serial, FastParallel, and USB. The SystemDevices module implements the pseudo devices such as: Vdu, Kbd, Null and Printer.
MS-DOS, PC DOS, DR-DOS FAT \DEV (and /DEV) various As implemented in the kernel, character devices appear in the virtual \DEV directory and any disk directory. Under MS-DOS/PC DOS 2.x, the CONFIG.SYS AVAILDEV=FALSE directive can be used to force devices to exist only in \DEV.
MagiC, MiNT, MultiTOS U:\DEV[27][28] Application Systems Heidelberg, Eric R. Smith, Atari Corp. The special U: drive contains a virtual DEV directory, inside which one can find device files.
Windows 9x \\devices\ Microsoft
Windows NT \Device Microsoft The \Device directory is a part of Windows NT object namespace.
Windows NT Win32 Subsystem \\.\ Microsoft The \\.\ prefix makes supporting APIs access the Win32 device namespace instead of the Win32 file namespace. The Win32 device names are symbolic links to device names under Windows NT \Device directory.

See also

Шаблон:Div col

Шаблон:Div col end

References

Шаблон:Reflist

Further reading

Шаблон:File systems Шаблон:Disk operating systems