Linux/dev Introduction and application of common special equipment [loop,null,zero,full,random]

Source: Internet
Author: User
Tags stdin

Linux is a file-based system, and all hardware, such as software, will have a corresponding file representation under the directory. For Dev This directory, we know the file below it, which represents the Linux device. In Windows systems, devices are well understood, like hard disks, which refer to real hardware. Under Linux for file systems, there are files associated with these devices. Access to them can be put into the actual hardware, think or Linux flexible. It's much easier to do it. You do not need to call the previous COM,PRT interface. Directly read the file, write the file can be sent to the device read or write operations. According to the way of reading and writing storage data, we can divide the device into the following types: Character device, block device, pseudo-device.

First, equipment classification

    • Character type device

A character device is a device that transmits 1 characters at a time to the system. These device nodes typically provide streaming communication services for devices such as faxes, virtual terminals, and serial modems, keyboards, which generally do not support random access to data. Most of the character devices do not use buffers when they are implemented. The system reads/writes each character directly from the device. For example, the device provided by the keyboard is a data stream, and when you type the string "Cnblogs", the keyboard driver returns the seven-character stream in exactly the same order as the input. They are sequential, return C first, and finally S.

    • Block Devices

A block device is a device that moves data in a block way between systems. These device nodes typically represent addressable devices such as hard disks, CD-ROM, and memory areas.

Block devices typically support random access and addressing, and use buffers. The operating system allocates a cache of input and output to store a piece of data. When a program sends a request to the device to read or write data, the system stores each character in the data in the appropriate cache. When the cache is filled, the appropriate action is taken (passing the data away), and the system empties the cache. It differs from the character device in that it supports random storage. The character type is a stream form that is stored one after the other.

    • Pseudo-Device

In Unix-like operating systems, device nodes do not necessarily correspond to physical devices. Devices that do not have this correspondence are pseudo-devices . The operating system uses many of the features they provide. Some of the pseudo-devices that are often used include: Null,zero,full,loop,random,urandom

Second, special equipment and use

Here is a special device in addition to the hard disk motherboard, but it has a special role in the Linux shell command, so take them out alone. 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 the last time you said, Linux redirection? Can look at: A 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)

Such as:

?

1 2 3 4 5 6 7 8 9 10 11 [[email protected] shell]$ cat >teststdin< /dev/stdin test #ctrl+D #cat从/dev/stdin获得数据,然后将标准输出,输入的到teststdin文件 [[email protected] shell]$ cat  teststdin  test   [[email protected] shell]$ cat >teststdin test #ctrl+D #不指定输入,默认输入设备就是/dev/stdinn

/dev/stdin means a keyboard device.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [[email protected] shell]$ cat test .sh > /dev/stdout | grep ‘echo‘ echo "very good!" ; echo "good!" ; echo "pass!" ; echo "no pass!" #/dev/stdout指向就是标准输出,因此重定向给它的数据,最终发送到屏幕上(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 指是错误输出,默认也是输出到屏幕上面,但是它的内容不能通过管道传递给grep,管道只能传递标准输出

/dev/null Equipment

is a black hole device that discards everything written to it, and an empty device is often used to discard unwanted output streams. Remember when using Windows, there was a similar device: NUL, as with this function. Any data written to the device will be discarded. Reading the data from this inside returns is empty. Send some unused content to this device frequently, discarding unwanted data.

Such as:

?

1 2 [[email protected] shell]$ cat /dev/null [[email protected] shell]$ cat test .sh > /dev/null

?

1 #读该设备为空,写入该设备数据都丢弃了

/dev/zero Equipment

In the Unix-like operating system,/dev/zero is a special file that, when you read it, provides unlimited null characters (null, ASCII NUL, 0x00). One typical usage is to overwrite information with a stream of characters it provides, and another common use is to produce a blank file of a specific size.

Such as:

?

1 2 3 4 5 6 7 8 9 10 11 12 [[email protected] shell]$ dd if = /dev/zero of=testzero count=1024 bs=1024 1024+0 Records in 1024+0 Records ou T 1048576 bytes (1.0 MB) copied, 0.0107194 seconds, 97.8 MB /s #创建一个大小为1M文件, a block of the file is 1024 bytes, altogether 1024 blocks (just 1M), filled with/dev/zero file content. The output is created to: Testzero file         [[email protected] shell]$ dd if = /dev/zero of= /dev/ Disk Partitions The #这个命令一定不要随便用, a bit like the Smash file tool inside Windows. However, it fills the entire partition with \0x00. This data is not recoverable.     [[email protected] shell]$ cat /dev/zero >testinputzero #这个命令也不能随便使用咯,/dev/zero device A special effect is that if you read it, a dead loop will output an infinite \x00, so you will create a X00 the populated file. If you do not limit the user's disk quotas. It will drain the entire disk space.

In the Linux resource quota limit, if there is no current user's disk space utilization, or memory usage. An ordinary user can fill the entire disk with one of the above methods. You can also pass the while (true) {fork ...} Class program that starts an infinite thread and consumes the entire system memory.

/dev/full Equipment

In Unix-like systems,/dev/full (Sheung Moon Device) is a special device file that always returns no space (error code of ENOSPC) to the device when writing to it, and reads like/dev/zero, returning an infinite number of NULL characters (NULL, ASCII NUL, 0x00). This device is often used to test the behavior of a program when it encounters a disk with no remaining space errors.

Such as:

?

1 2 3) 4 5 [[email protected] shell]$ echo ‘chengmo‘ > /dev/full  - bash : echo : write error: 设备上没有空间 [[email protected] shell]$ echo $? 1 #命令执行返回错误

?

1

/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 as a pseudo-random number generator. It allows programs to access background noise from device drivers or other sources. Commonly used as a random number generator. Specific reference: Linux shell Implementation of random number of methods (DATE,RANDOM,UUID)

/dev/fd

Record user-opened file descriptors

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

Detailed reference:

Linux shell data Redirection (input redirection and output redirection) detailed analysis file descriptor description.

/dev/tcp[udp]/host/port

Reading this type of device, a tcp[upd] connection will be created that connects to the host port. Open a socket communication interface.

For detailed use, refer to:

Linux shell script implements TCP/UPD protocol communication (redirected app)

/dev/loop

In Unix-like operating systems, loop devices can be used to mount loop files as block devices.

Such as:

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

#将img镜像文件挂载到/home/chengmo/img directory, with this device, we do not need to be able to read the virtual disk format files through the virtual optical drive.

Said a lot of Linux special devices, other like CPU, memory, disk, network, keyboard, terminal equipment. It's pretty much like our windows. What's the problem, welcome to exchange!


Linux/dev Introduction and application of common special equipment [loop,null,zero,full,random]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.