Concept: in a Linux system, all devices are mapped to "device files" for processing, device files, and applications can operate on hardware devices as if they were normal files.
First, the type of equipment
Tidy from: ( pretty good, suggest have time to read the original text )
Chapter I introduction to device drivers 1.3. Classification of equipment and modules
"Linux device driven character device driver"
In general, the Linux device can be divided into 3 basic device types: Character devices, block devices, network equipment
A character device or a block device corresponds to a device file in the/dev directory.
The Linux User program uses the driver to manipulate character devices and block devices through device files (or device nodes).
-
-
Character device
-
Refers to a device that can read and write only one byte at a byte, not a random reading of a data in the device memory, and the data needs to be read sequentially .
The character device is a stream-oriented device, and the common character devices are mouse, keyboard, serial port, console and led device.
This console (/dev/console) and the serial port (/DEV/TTYS0) are examples of character devices because they show the abstraction of the stream well.
Character devices are accessed through file system nodes, such as/dev/tty1 and/dev/lp0.
The only difference between a character device and a normal file is that you can often move around in normal files, but most of the character devices are just data channels, and you can only access them sequentially. However, there are character devices that look like data areas, and you can move them around. For example, frame grabber often does so, and applications can use MMAP or Lseek to access the entire requested image.
Character device file (Type C):
Note:
careful person, may find that the device file is no file size, instead of two numbers: the main device number + this device number (see below)
-
-
Block devices
-
-
refers to a device that can read a certain length of data from anywhere on the device. A block device includes a hard disk, disk, USB flash drive, and SD card, such as
like a character device, a block device is located in the/dev File system nodes of the directory to be accessed.
a block device, such as a disk, should be a file system.
linux, instead, allows an application to read and write a block device like a character device-- It allows any number of bytes to be transferred at one time. As a result, the difference between a block and a character device is only in the kernel and in the way it manages the data internally, and therefore differs on the kernel/driver software interface. Like a character device, each block device is accessed through a file system node, and the difference between them is transparent to the user. Block drive is completely different from the kernel's interface than the character driver.
block device file (Type B):
-
- network interface
-
-
Generally, an interface is a hardware device, but it can also be a purely software device, such as a loopback interface.
A network interface is responsible for sending and receiving data packets, driven by the kernel network subsystem, It is not necessary to know how a single transaction is mapped to the actual sent message.
Many network connections (especially those that use TCP) are stream-oriented, However, network devices are often designed to handle the sending and receiving of messages.
since it is not a stream-oriented device, a network interface is not as easily mapped to a node of the file system as/dev/tty1.
unix provides access to interfaces is still by assigning a name to them (for example, eth0), but the name does not have a corresponding entry in the file system.
 
Note:The Linux file type also includes: Normal file (-), socket file (S), link file (L), Visible: "Linux file type full parse ", there is a D, Representative directory
Second, the main equipment number and the secondary device number
View main device number and secondary device number: Ls-al/dev
View the main device number of the currently loaded device driver: cat/proc/devices
The first column is the primary device number, and the second is the device name
Each character device and block device must have a primary and secondary device number, the same device with the same main device number (using the same driver)
Whether the difference is similar equipment, can better manage and optimize the equipment, mainly reflected in:
The system may contain several devices of the same type, managed by the same device driver (does not require a duplicate load drive to the kernel)
The same device can be combined for easy insertion into the kernel's data structure for management.
Third, other
Reference: "TTY, Pty, PTS and other equipment naming rules"
Here are some small experiments in the MAC system:
Tty
1. Open two terminals, switch to root
Step1: Terminal 1:
#通过 TTY gets the corresponding file address of the current terminal:
root# TTY
/dev/ttys001
#持续监控 ttys001 File contents
root# tail-f/dev/ttys001
Step2: Terminal 2:
root# echo ' hello/dev/ttys0001 ' >/dev/ttys001
Step3: Terminal 1 outputs a bit of content:
hello/dev/ttys0001
2. SSH to a Linux system, switch to root
Step1: Terminal 1:
#通过 TTY gets the corresponding file address of the current terminal:
root# TTY
/dev/pts/0
#持续监控 ttys001 File contents
root# tail-f/dev/pts/0
Step2: Terminal 2:
echo ' hello/dev/pts/0 ' >/dev/pts/0
Step3: Terminal 1 outputs a bit of content:
hello/dev/pts/0
Classification of Linux devices and modules