Sinsing interpretation of/dev/null and/dev/zero

Source: Internet
Author: User

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

When we use cat $filename, if the corresponding file does not exist, then the system will generate an error message, we can suppress the output of this error message, we can use the cat $badname 2>/dev/null to do.

In the Unix-like operating system,/dev/zero is also a special file, when we read it, it will provide an infinite number of null characters, can be understood as Null,ascii null,0x00, one of the typical usage is that it provides a stream of characters to cover the information, Another common use is to produce a blank file of a specific size. In BSD is through mmap the/dev/zero map to the virtual address space to achieve shared memory, you can use Mmap to map/dev/zero to a virtual memory space, the effect of this operation is equivalent to using a section of anonymous memory, that is, no nuclear file related.

/dev/zero is also a pseudo-file, but it actually produces a continuous stream of NULL, which is a binary 0 stream, not an ASCII type. 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.

If we accidentally deleted/dev/zero this file, it is worth noting that the file is a system special file, it is lost and can not be copied from the system disk or other systems directly, can only be repaired by reconstruction, our repair process is usually as follows:

(1) Mknod/dev/zero C 1 5

(2) chmod 666/dev/zero

We usually use Mknod to create special files that can only be run by the root user or by a system member. Let's talk about this Mknod command, which creates a directory entry and a corresponding index node for a special file. The first parameter is the name of a name item device. Select a descriptive device name. The Mknod command has another form, and they have different flags, both of which are as follows:

(1) Mknod name {B|c} major minor

(2) Mknod name {p}

First, the first form, where b means that the special file is a block-oriented device, such as a disk and floppy disks. C means that the special file is a character-oriented device, such as a keyboard. The last two parameters of the first form are the number of specified primary devices that help the operating system find the device driver code and the number of specified secondary devices, that is, the unit drive or line number, which are either decimal or octal. The primary and secondary number of a device is assigned by the device's configuration method, which is stored in the CUDVDR class in the ODM, and it is important to define the primary and secondary numbers in this object class to ensure consistency across the system device definition.

Then there is the second form, which uses the P flag to create the FIFO, which is named pipe.

For example, when we created the/dev/zero, the primary and secondary equipment numbers are 1 and 5, and the/dev/null when the main equipment number is 1 and 3, and/dev/random the major and minor device numbers are 1 and 8, in general, the master device number is used to partition the type of equipment, The secondary device number is to make a unique partition, indicating different attributes. We can use Ls-ll/dev/zero to see these values, and in the red Hat version, you can not add the-l parameter, but you must add it under Solaris.

Typically a dedicated file does not occupy space on the disk, it simply provides communication to the operating system, not the data storage service. In general, private files point to a single hardware. A block file is typically similar to a disk device because a block number is assigned to the data where it can be accessed, which means that a block cache is also set. All other devices are character files.

Sinsing interpretation of/dev/null and/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: 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.