Introduction and application of common special equipment for Linux dev (loop,null,zero,full,random) _linux shell

Source: Internet
Author: User
Tags error code generator memory usage stdin


Linux is a file system, and all hardware, such as software, will be represented under the appropriate directory. For the dev directory, we know that the file below it represents the Linux device. In Windows, devices are well understood, like hard disks, and disk refers to real hardware. And under Linux on the file system, there are the files associated with these devices. Access to them can be put to the actual hardware, think or Linux flexible. Become a file, the operation should be more simple. You do not need to invoke the previous COM,PRT interface. Read the file directly, write the file can send to the device read or write operation. According to the way of storing data, we can divide the equipment into the following types: Character equipment, block equipment, pseudo equipment.



First, equipment classification



Character-type devices



A character device is a device that transmits 1 characters at a time with the system. These device nodes typically provide streaming communications services for devices such as faxes, virtual terminals and serial modems, keyboards, and so on, which typically do not support random access data. When a character device is implemented, it mostly does not use a buffer. 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 data stream in exactly the same order as the input. They are in order, first return C, and finally S.



Block device



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-ROMs, and memory areas.



Block devices typically support random access and addressing, and use caching. The operating system allocates a cache for the input output to store a piece of data. When a program sends a request to a 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 (pass the data away), and the system empties the cache. It differs from character devices in that it supports random storage. The character type is a stream form, stored one at a while.



Pseudo device



In Unix-like operating systems, device nodes do not necessarily correspond to physical devices. A device that does not have this correspondence is a pseudo device. The operating system uses the many functions they provide. Some of the most commonly used pseudo devices include: null,zero,full,loop,random,urandom



Second, special equipment and use



It says here that special devices, in addition to the hard disk motherboard, have 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 Equipment



Remember the last time you said Linux redirection? Can look at: Linux shell data Redirection (input redirection and output redirection) detailed analysis. They correspond to several special file descriptors, FD0,FD1,FD2 (Stdin,stdout,stderr)



Such as:


[Chengmo@centos5 shell]$ cat>teststdin</dev/stdin
test
#ctrl +d
#cat从/dev/stdin Get the data, and then the standard output, Entered to Teststdin file
[chengmo@centos5 shell]$ cat Teststdin 
test
[chengmo@centos5 shell]$ cat> Teststdin
test
#ctrl +d
#Do not specify input, the default input device is/dev/stdinn


/dev/stdin refers to the keyboard device.


[Chengmo@centos5 shell]$ cat test.sh >/dev/stdout |grep ' echo '
echo ' very good! ';
echo "good!";
echo "pass!";
echo "No pass!"
The #/dev/stdout point is the standard output, so the data redirected to it is eventually sent to the screen (FD1)
[chengmo@centos5 shell]$ cat test.sh ' echo ' C8/>echo "very good!";
echo "good!";
echo "pass!";
echo "No pass!";
[Chengmo@centos5 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, which can only pass standard output


/dev/null Equipment



It's a black hole device, it discards everything written into the data, the empty device is often used to discard unwanted output streams. Remember when using Windows, there is a similar device: NUL, like this function. Any data written to the device will be discarded. Reading from this inside the data returned is empty. Send some unused content to this device frequently, discarding unwanted data.



Such as:


[Chengmo@centos5 shell]$ cat/dev/null
[chengmo@centos5 shell]$ cat test.sh >/dev/null
#Read that the device is empty, the data written to this device is discarded


/dev/zero Equipment



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



Such as:

[Chengmo@centos5 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/
The file is a block of 1024 bytes, a total of 1024 pieces (just 1m), with / dev / the zero file content fills it. The output is created to: testzero file
[chengmo@centos5 Shell] $dd if=/dev/zero of=/dev/disk partition
#It's kind of like a smash file tool inside windows. However, it fills the entire partition with \ 0x00. This data is not recoverable
[Chengmo@centos5 Shell] $cat/dev/zero>testinputzero
#This command cannot be used casually. / dev / zero device a special effect is that if you read it, a dead loop will output infinite \ X00, so you will create a file that is populated with \ X00. If you do not limit the user's disk quotes. It will complete the entire disk space

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






/dev/full Equipment



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



Such as:


[Chengmo@centos5 shell]$ Echo ' Chengmo ' >/dev/full
-bash:echo:write error: No space on device
[chengmo@centos5 Shell] $ echo $?
1
#Command execution return 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 pseudo random number generator. It allows programs to access background noise from device drivers or other sources. Used as a random number generator. Specific reference: Linux shell Implementation of random number of multiple methods (DATE,RANDOM,UUID)






/dev/fd



Record user-opened file descriptors



[Chengmo@centos5 shell]$ ls/dev/fd/
0 1 2 3



Detailed reference:



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






/dev/tcp[udp]/host/port



Reading this class form device will create a TCP[UPD] connection that connects to the host host port. Open a socket communication interface.



Detailed use can refer to:



Linux shell script implements TCP/UPD protocol communication (redirect application)






/dev/loop



In a Unix-like operating system, the loop device can mount the loop file for use as a block device.



Such as:



[Chengmo@centos5 Shell] $mount-O loop example.img/home/chengmo/img



#Mount the IMG image file to / home / Chengmo / img directory, with this device, we do not need to go through the virtual drive to read the virtual disk format files



Said a lot of Linux special devices, other like CPU, memory, disk, network, keyboard, terminal equipment. Similar to what we see in Windows. What is the problem, welcome to exchange!


Related Article

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.