About/dev/null and/dev/zero file details, as well as the solution of mis-deletion/dev/null and/dev/zero and the method of disk IO test using/dev/zero

Source: Internet
Author: User
Tags vps

1, Introduction--From the wiki interpretation/dev/null:

In a Unix-like system, a/dev/null, or empty device, is a special device file that discards everything written to it (but reports a successful write operation) and reads it immediately to get an EOF.
In programmer jargon, especially in Unix jargon,/dev/null is called a bit bucket or black hole. Empty devices are often used to discard unwanted output streams, or as empty files for input streams. These operations are usually done by redirection.

/dev/zero:

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. BSD is the use of mmap to map/dev/zero to virtual address space to achieve shared memory. You can use Mmap to map/dev/zero to a virtual memory space, which is equivalent to using a section of anonymous memory (not associated with any file). (citation:/dev/zero-Wikipedia)

The following is an interpretation of the individual from the heavenly Star, the master skipped:

/dev/null nickname is the bottomless pit, you can output any data to it, it take all, and will not hold!
/dev/zero is an input device that you can use to initialize files.
/dev/null------It is an empty device, also known as a bit bucket (bits bucket). Any output written to it will be discarded. If you do not want the message to display or write to the file in standard output, you can redirect the message to the bin.
/dev/zero------The device provides an exhaustive 0, you can use any number you need-the device provides much more. He can be used to write a string 0 to a device or file.

2,/dev/null of the daily use of/dev/null as a "black hole." It is equivalent to a write-only file, and all the content written to it is lost forever, and trying to read from it will not read anything. However,/dev/null is very useful for command lines and scripts.

We all know that cat $filename will output the file contents of filename (output to standard output)
and using cat $filename >/dev/null
will not get any information as we redirect the file information that was supposed to be displayed through the standard output to/dev/null, so what would you get?
The same effect is achieved with cat $filename 1>/dev/null, since 1 of the default redirection is the standard output. If you are familiar with Shell scripting or redirection, you should think of 2, which is the standard error output.
When we use cat $filename, if the file corresponding to filename does not exist, the system will definitely error: "Cat:filename: There is no file or directory."

What if we don't want to see the error output? We can prohibit standard error: Cat $badname 2>/dev/null

We can get a deeper understanding of/dev/null by following this test:

$cat Test.txt
Just for Test
$cat Test.txt >/dev/null
$cat Test.txt 1>/dev/null
$cat Test2.txt
Cat:test2.txt: No file or directory
$cat Test2.txt >/dev/null
Cat:test2.txt: No file or directory
$cat Test2.txt 2>/dev/null
$

Sometimes I don't want to see any output, I just want to see if this command is working properly, then we can suppress both standard output and standard error output:

Cat $filename 2>/dev/null >/dev/null

So:

* If "$filename" does not exist, there will be no error message,
* if "$filename" exists, the contents of the file will not be printed to standard output.
* Therefore, the above code does not output any information at all, and is useful when you want to test the exit code of a command without any output.

Next, we use the echo $? Check the exit code for the previous command: 0 for the command to execute normally, 1-255 for error.
Of course, using cat $filename &>/dev/null can also achieve the same effect as cat $filename 2>/dev/null >/dev/null.

$cat Test2.txt 2>/dev/null
$cat test.txt 2>/dev/null >/dev/null
$echo $?
0
$cat test2.txt 2>/dev/null >/dev/null
$echo $?
1
$cat Test.txt &>/dev/null
$echo $?
0

Sometimes, we need to delete the contents of some files without deleting the file itself: (This method can be used to delete the log file, in my Debian notebook I give/var disk space is too small, sometimes need to manually use this operation to empty the log)

# Cat/dev/null >/var/log/messages
#: >/var/log/messages has the same effect, but does not produce new processes. (because: Built-in)

In the following example, use/dev/null to remove cookies and no longer use cookies
If [-F ~/.netscape/cookies] # is deleted if it exists, you can add a soft link after deleting it
Then
Rm-f ~/.netscape/cookies
Fi

Ln-s/dev/null ~/.netscape/cookies

Among them, the directory of cookies can be changed, for example, my own computer Firefox cookie directory is: ~/.mozilla/firefox/nah4b6di.default/cookies*

3, the daily use of/dev/zero
Like/dev/null,/dev/zero is also a pseudo-file, but it actually produces a continuous stream of NULL (binary 0 streams, not ASCII). The output that is written to it is lost, and the primary use of/dev/zero is to create an empty file of the specified length for initialization, like a temporary swap file.

For example, in my previous blog ("Try to install the new version of Chrome OS Vanilla & after the USB flash drive encountered problem resolution"), mentioned I use DD made USB drive system, and my USB stick 16G, and after the production of a good, The system disk accounts for only 2.5G, while the rest of the space (nearly 12G) is not used. I can only use DD If=/dev/zero of=/dev/sdb bs=4m to re-give my entire USB flash drive to zero.

Script Instance 1. Create a swap temporary file with/dev/zero
#!/bin/bash
# Create a swap file with parameters for the number of blocks created (default without parameters), 1024B (1K)


The $UID of the Root_uid=0 # Root user is 0.
E_WRONG_USER=65 # not Root?


File=/swap
blocksize=1024
Minblocks=40
Success=0

# This script must be run with root, if not root to make a prompt and exit
If ["$UID"-ne "$ROOT _uid"]
Then
Echo echo "You must is root to run this script."; Echo
Exit $E _wrong_user
Fi


blocks=${1:-$MINBLOCKS} # If the command line is not specified, it is set to the default of 40 blocks.
# The above sentence is equivalent to:
# ----------------------------------------
# if [-N "$"]
# Then
# blocks=$1
# Else
# blocks= $MINBLOCKS
# fi
# -----------------------------------------

If ["$blocks"-lt $MINBLOCKS]
Then
blocks= $MINBLOCKS # At least 40 blocks long, if the parameters are smaller than 40, the number of blocks will still be set to 40
Fi

echo "Creating swap file of size $blocks blocks (KB)."
DD If=/dev/zero of= $FILE bs= $BLOCKSIZE count= $blocks # writes zeros to the file.

Mkswap $FILE $blocks # to build this file as a swap file (or swap partition).
Swapon $FILE # Activates the swap file.

echo "Swap file created and activated."
Exit $SUCCESS

The effect of the operation we can see:

[Email protected] zhaiqutianshangxing/tmp]$ vim testswap.sh
[Email protected] zhaiqutianshangxing/tmp]$ chmod +x testswap.sh
[[email protected] zhaiqutianshangxing/tmp]$ sudo./testswap.sh
[sudo] password for zhaiqutianshangxing:
[Email protected] zhaiqutianshangxing/tmp]$./testswap.sh

You must is root to run this script.

[[email protected] zhaiqutianshangxing/tmp]$ sudo./testswap.sh
[sudo] password for zhaiqutianshangxing:
Creating swap file of size blocks (KB).
Recorded 40+0 read-in
Recorded the writing of 40+0.
40960 kilobytes (KB) copied, 0.000904021 sec, 45.3 mb/sec
Setting swap space version 1, size = KiB
No label, uuid=3e59eddf-098f-454d-9507-aba55f434a8c
Swap file created and activated.

Another application for/dev/zero is to use 0 to populate a file of a specified size for a specific purpose, such as mounting a file system to a loopback device (loopback) or "safely" deleting a file.
Script Instance 2. Create RAMDisk
#!/bin/bash
# ramdisk.sh
# "RAMDisk" is a section of system RAM memory that can be manipulated as a file system.
# Pros: Very fast access (including Read and write).
# Cons: Volatile, loss of data when the computer restarts or shuts down.
#Reduces the RAM available to the system.
#
# So what does RAMDisk do?
# Save a large data set in RAMDisk, such as a table or dictionary, which speeds up the data query, because finding in memory is much faster than finding it on disk.

E_NON_ROOT_USER=70 # must be run with ROOT.
Rootuser_name=root

Mountpt=/mnt/ramdisk
size=2000 # 2K Blocks (can be modified as appropriate)
blocksize=1024 # 1 K (in the size of a byte) per block
DEVICE=/DEV/RAM0 # First RAM device

Username= ' Id-nu '
If ["$username"! = "$ROOTUSER _name"]
Then
echo "must is root to run" "' BaseName $ '" "."
Exit $E _non_root_user
Fi

if [!-D "$MOUNTPT"] # test whether the mount point already exists,
Then #+ if this script has been running several times, it won't be built again.
mkdir $MOUNTPT #+ Because the front was already established.
Fi

DD If=/dev/zero of= $DEVICE count= $SIZE bs= $BLOCKSIZE # Fills the contents of the RAM device with 0.
# Why do you need to do this?
MKE2FS $DEVICE # Create a ext2 file system on a RAM device.
Mount $DEVICE $MOUNTPT # mount device.
chmod 777 $MOUNTPT # makes it possible for ordinary users to access this ramdisk, but only by root unloading download it.

echo "" "$MOUNTPT" "now available for use."
# now RAMDisk can be used to access files even for ordinary users.
Note that RAMDisk is volatile, so when the computer system restarts or shuts down, the contents of the RAMDisk disappear.
#
# after rebooting, run this script to build up a ramdisk again.
# reload/mnt/ramdisk only and no other steps will work correctly.


# If improved, this script can be placed in/etc/rc.d/rc.local so that the system can automatically set up a ramdisk when it starts. This is appropriate for the database server with high speed requirements.
Exit 0
It works as follows:


[Email protected] zhaiqutianshangxing/tmp]$ vim ramdisk.sh
[Email protected] zhaiqutianshangxing/tmp]$ chmod +x ramdisk.sh
[Email protected] zhaiqutianshangxing/tmp]$./ramdisk.sh
Must is root to run ramdisk.sh.
[[email protected] zhaiqutianshangxing/tmp]$ sudo./ramdisk.sh
Recorded 2000+0 read-in
Recorded the writing of 2000+0.
2048000 bytes (2.0 MB) replicated, 0.0113732 seconds, $/sec.
MKE2FS 1.42.8 (20-jun-2013)
Discarding device blocks: complete
File System label =
OS Type:linux
Block size =1024 (log=0)
Chunked size =1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
16384 inodes, 65536 blocks
3276 blocks (5.00%) reserved for the Super user
First block of data =1
Maximum filesystem blocks=67108864
8 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345


Allocating group tables: complete
Writing Inode table: complete
Writing Superblocks and FileSystem accounting information: Complete
/mnt/ramdisk now available to use.
Finally, it is worth mentioning that the elf binaries take advantage of the/dev/zero.


Test Disk IO today to execute the command, suddenly found that the/dev/zero file strange lost, the prompt cannot find the file, the error is as follows:
Pick up the Sky stars, a love of Internet art people! Email:[email protected]
[[email protected] zhaiqutianshangxing]# dd If=/dev/zero of=test bs=64k count=4k Oflag=dsync
DD: Opening "/dev/zero": No file or directory
The resolution of/dev/zero file by mistake Delete the following two commands to repair the rebuild file: [[email protected] zhaiqutianshangxing]# Mknod/dev/zero C 1 5
[Email protected] zhaiqutianshangxing]# chmod 666/dev/zero
After the repair is completed with the command test is available, found that the disk test can be used normally
[[email protected] sq808sq]# dd If=/dev/zero of=test bs=64k count=4k Oflag=dsync
Recorded 4096+0 read-in
Recorded the writing of 4096+0.
268435456 bytes (268 MB) Replicated, 105.086 sec, 2.6 mb/sec
At this point, the repair and rebuild of the/dev/zero file has been completed(this file can not be copied directly from the system disk or other system after the system special file is lost, but only by rebuilding and repairing, the/dev/null file below)
The resolution of the/dev/null file by mistake is as follows: [[email protected] zhaiqutianshangxing]# mknod/dev/null C 1 3
[Email protected] zhaiqutianshangxing]# chmod 666/dev/null
Then execute the command to view the/dev/null file as shown below and have successfully repaired the/dev/null file
[Email protected] zhaiqutianshangxing]# Ls-la/dev/null
crw-rw-rw-. 1 root root 1, 3 December 13:13/dev/null


Test disk IO with/dev/zero:

[[email protected] zhaiqutianshangxing]# dd If=/dev/zero of=test bs=64k count=4k Oflag=dsync
Recorded 4096+0 read-in
Recorded the writing of 4096+0.
268435456 bytes (268 MB) Replicated, 104.896 sec, 2.6 mb/sec
[[email protected] zhaiqutianshangxing]# dd If=/dev/zero of=test bs=8k count=256k Conv=fdatasync
Recorded 262144+0 read-in
Recorded the writing of 262144+0.
2147483648 bytes (2.1 GB) Replicated, 28.9346 sec, 74.2 mb/sec

More detailed/dev/zero disk IO Test instructions:

The normal test may be more than one side of the test, it may take a lot of times to find the average, this test results in normal redirection is not effective after Google a bit in the following way to redirect to a file

DD If=/dev/zero of=/var/test bs=8k count=1000000 2>> Info

The result of this test is in the info file.

1. DD If=/dev/zero of=test bs=64k count=16k
This is very inaccurate, because the data is not actually written to disk at the end of the command.

2. DD If=/dev/zero of=test bs=64k count=16k Conv=fsync
This is accurate, the data has been written to disk

3. DD If=/dev/zero of=test bs=64k count=4k Oflag=dsync
This can be considered a simulated database insert operation, so it's slow

Then let's take a look at the VPS disk performance
DD If=/dev/zero of=test bs=64k count=16k
1073741824 bytes (1.1 GB) copied, 2.625317 seconds, 358 MB/s
The result of the above method seems to be fast
DD If=/dev/zero of=test bs=64k count=16k Conv=fsync
1073741824 bytes (1.1 GB) copied, 12.5891 seconds, 77.1 MB/s
The implementation is a bit slow, but this time the results are more useful.
DD If=/dev/zero of=test bs=64k count=2k Oflag=dsync
134217728 bytes (134 MB) copied, 176.151 seconds, 755 kb/s
This, is the real power of the VPS, in 84 of the VPS test but there are 20m/s

-dsync can be used as a simulation database insert operation, read a piece of data in/dev/zone immediately write to the hard disk
-fsync also writes data to disk, but is cached and then written to the hard disk

Here are some ways to test the disk read and write speed differences?
DD bs=1m count=128 If=/dev/zero of=test
DD bs=1m count=128 If=/dev/zero of=test; Sync
DD bs=1m count=128 If=/dev/zero of=test Conv=fdatasync
DD bs=1m count=128 If=/dev/zero of=test Oflag=dsync
The difference is in how the in-memory write cache is handled.
DD bs=1m count=128 If=/dev/zero of=test
Without any parameters, the default mode of DD does not include the Sync command. That is, the DD command does not allow the system to actually write the file to disk until it is finished. So the above command simply reads the 128MB data into the memory buffer (write cache). So what you get is going to be a super fast speed. Because the only thing DD gives you is the reading speed, until the DD is finished, the system begins to actually write data to the disk, but you can't see it at this speed. So if the speed is fast, don't steal it first. Oh
DD bs=1m count=128 If=/dev/zero of=test; Sync
Exactly the same as in the previous 1. A semicolon separates only two separate commands. When the sync command is ready to start to actually write data to the disk, the previous DD command has already displayed the wrong "write speed" value on the screen. So you still don't get a real write speed.
DD bs=1m count=128 If=/dev/zero of=test Conv=fdatasync
After adding this parameter, the DD command executes to the end of a real "sync" operation, so you get the time it takes to read the 128M data into memory and write to the disk, so the time that is calculated is more realistic.
DD bs=1m count=128 If=/dev/zero of=test Oflag=dsync
When this parameter is added, the DD performs a synchronous write operation each time it executes. That is, this command to read 1M each time after the 1M is written to disk, and then read the following 1M, altogether repeated 128 times. This is probably the slowest way to do this, because the write cache is basically not used.
It is recommended to use DD bs=1m count=128 if=/dev/zero of=test conv=fdatasync
Because this approach is closest to the actual operation of the computer, the data measured is the most useful.

Pick up the Sky stars, a love of Internet art people! Email:[email protected]


About/dev/null and/dev/zero file details, as well as the solution of mis-deletion/dev/null and/dev/zero and the method of disk IO test using/dev/zero

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.