Introduction and application of Linux dev common special devices (loop, null, zero, full, random)

Time:2021-11-24

Linux is a file system. All hardware, such as software, will have corresponding file representation under the corresponding directory. For the dev directory, we know that the files under it represent Linux devices. In Windows system, devices are easy to understand. Like hard disk, disk refers to real hardware. Under Linux of the file system, there are files associated with these devices. You can access them and put them into the actual hardware. Think about it, Linux is flexible. How easy it is to become a file. There is no need to call the previous com, PRT and other interfaces. Directly read and write files, you can send read or write operations to the device. According to the way of reading and writing data storage, we can divide the equipment into the following types: character type equipment, block equipment and pseudo equipment.

1、 Equipment classification

Character type equipment

Character device refers to the device that transmits 1 character with the system at a time. These device nodes usually provide streaming communication services for devices such as fax, virtual terminal, serial modem and keyboard. It usually does not support random access data. When implementing character devices, most of them do not use buffers. The system reads / writes each character directly from the device. For example, the keyboard device provides a data stream. When you type in the string “cnblogs”, the keyboard driver will return the seven character data stream in exactly the same order as the input. They are sequential, returning C first and s last.

Block device

A block device is a device that moves data in a block manner with a system. These device nodes typically represent addressable devices such as hard disks, CD ROMs, and memory areas.

Block devices usually support random access and addressing and use buffers. The operating system allocates a cache for input and output to store a piece of data. When the program sends a request to read or write data to the device, the system stores each character in the data in the appropriate cache. When the cache is full, appropriate actions will be taken (transfer the data), and then the system will empty the cache. It differs from character devices in that it supports random storage. Character type is a stream form, which is stored one by one.

Pseudo device

In UNIX like operating systems, device nodes do not necessarily correspond to physical devices. Devices without this correspondence are pseudo devices. Operating systems use a variety of functions they provide. Some commonly used pseudo devices include: null, zero, full, loop, random, random

2、 Special equipment and use

In addition to the hard disk, motherboard and other special devices mentioned here, they only play a special role in the Linux shell commands, so they are taken out separately. These devices are:

/dev/stdin
/dev/stdout
/dev/stderr
/dev/null
/dev/zero
/dev/full
/dev/random,urandom
/dev/fd
/dev/tcp|upd
/dev/loop

1. Standard output input device

Remember what I said last time, Linux redirection? You can see the following: detailed analysis of Linux shell data redirection (input redirection and output redirection). They correspond to several special file descriptors, fd0, FD1, fd2 (stdin, stdout, stderr)

For example:

[[email protected] shell]$ cat>teststdin</dev/stdin
test
#ctrl+D
#Cat gets the data from / dev / stdin, and then inputs the standard output to the teststdin file
[[email protected] shell]$ cat teststdin 
test
 
[[email protected] shell]$ cat>teststdin
test
#ctrl+D
#If no input is specified, the default input device is / dev / stdinn

/Dev / stdin refers to the keyboard device

[[email protected] shell]$ cat test.sh >/dev/stdout |grep 'echo'
echo "very good!";
echo "good!";
echo "pass!";
echo "no pass!"
#/Dev / stdout points to the standard output, so the data redirected to it is finally sent to the screen (FD1)
     
[[email protected] shell]$ cat test.sh  |grep 'echo'           
echo "very good!";
echo "good!";
echo "pass!";
echo "no pass!";
 
     
[[email protected] shell]$ cat test.sh >/dev/stderr |grep 'echo'  
#!/bin/sh
 
scores=40;
if [[ $scores -gt 90 ]]; then
    echo "very good!";
elif [[ $scores -gt 80 ]]; then
    echo "good!";
elif [[ $scores -gt 60 ]]; then
    echo "pass!";
else
    echo "no pass!";
fi;
#/Dev / stderr refers to error output, which is also output to the screen by default, but its content cannot be passed to grep through the pipeline, and the pipeline can only deliver standard output

/Dev / null device

Is a black hole device that discards all data written into it. Empty devices are usually used to discard unwanted output streams. I remember when I used windows, there was a similar device: nul, which has the same function. Any data written to the device will be discarded. The data read from this is null. Often send some unused content to the device and discard unwanted data.

For example:

[[email protected] shell]$ cat /dev/null
[[email protected] shell]$ cat test.sh >/dev/null
#The read device is empty, and the data written to the device is discarded

/Dev / zero device

In UNIX like operating system, / dev / zero is a special file. When you read it, it will provide unlimited null characters (null, ASCII nul, 0x00). A typical usage is to overwrite information with the character stream it provides. Another common usage is to generate a blank file of a specific size.

For example:

[[email protected] shell]$ dd if=/dev/zero of=testzero count=1024 bs=1024
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB) copied, 0.0107194 seconds, 97.8 MB/s
#Create a file with a size of 1m. One block of the file is 1024 bytes, with a total of 1024 blocks (just 1m). Fill it with the contents of the / dev / zero file. Create the output to the: testzro file
 
 
[ [email protected] Shell] $DD if = / dev / zero of = / dev / disk partition
#This command must not be used casually. It is a bit like the file shredding tool in windows. However, it fills the entire partition with \ 0x00. In this way, the data can not be recovered.
 
[[email protected] shell]$cat /dev/zero>testinputzero
#This command cannot be used casually. A special effect of the / dev / zero device is that if you read it, it is an endless loop and will output infinite \ X00. In this way, you will create a file filled with \ X00. If you do not limit the disk quota for this user. It will exhaust the entire disk space.

In the Linux resource quota limit, if there is no use of disk space or memory for ordinary users. An ordinary user can use the above method to fill the whole disk in a short time. You can also use the while (true) {fork…} class program to start infinite threads and exhaust the memory of the whole system.

/Dev / full device

In UNIX like systems, / dev / full (constant full device) is a special device file. When writing to it, it always returns that the device has no remaining space (the error code is enospc). When reading, it is similar to / dev / zero, and returns infinite null characters (null, ASCII nul, 0x00). This device is usually used to test the behavior of programs when they encounter a disk free space error.

For example:

[[email protected] shell]$ echo 'chengmo' >/dev/full
-Bash: Echo: write error: there is no space on the device
[[email protected] shell]$ echo $?
1
#Command execution returned an error

/dev/random[urandom]

In UNIX like operating systems, / dev / random is a special device file that can be used as a random number generator or a pseudo-random number generator. It allows programs to access background noise from device drivers or other sources. Commonly used as random number generator. Specific reference: Linux shell implements multiple methods of random numbers (date, random, UUID)

/dev/fd

Record the file descriptor opened by the user

[[email protected] shell]$ ls /dev/fd/
0 1 2 3

Detailed reference:

Linux shell data redirection (input redirection and output redirection) detailed analysis of file descriptors.

/dev/tcp[udp]/host/port

Reading this type of device will create a TCP [UPD] connection to the host port port. Open a socket communication interface.

For details, please refer to:

Linux shell script realizes TCP / UPD protocol communication (redirection application)

/dev/loop

In UNIX like operating systems, loop devices can mount loop files as block devices.

For example:

[[email protected] shell]$mount -o loop example.img /home/chengmo/img

#Mount the IMG image file to the / home / Chengmo / img directory. With this device, we can read the files in the virtual disk format without using the virtual optical drive.

Said a lot of Linux special equipment, others like CPU, memory, disk, network, keyboard, terminal equipment. It’s similar to what we see in windows. If you have any questions, welcome to communicate!

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]