兩個特殊設備文件dev/zero dev/null

/dev/null:

In Unix-like operating systems, /dev/null or the null device is a special file that discards all data written to it, and provides no data to any process that reads from it (it returns EOF). In Unix programmer jargon, it may also be called the bit bucket or black hole.

The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection.

This entity is a common inspiration for technical jargon expressions and metaphors by Unix programmers, e.g. "please send complaints to /dev/null" or "my mail got archived in /dev/null", being jocular ways of saying, respectively: "don't bother to send any complaints" and "my mail got deleted". A famous advertisement for the Titanium PowerBook G4 read [The Titanium Powerbook G4] Sends other UNIX boxes to /dev/null.

The null device is also a favorite subject of technical jokes, such as warning users that the system's /dev/null is already 98% full. The April Fool's, 1995 issue of the German magazine c't reported on an enhanced /dev/null chip that would efficiently dispose of the incoming data by converting it to flicker on an internal glowing LED.

/dev/null is a special file, not a directory (folder), so one cannot move files into it with the Unix mv command. See rm for the proper way to delete files in Unix.

The equivalent device in CP/M (and later DOS and Windows) is called NUL:, and on some versions of DOS just NUL (for example, one may hide output by directing it to NUL, e.g. PAUSE>NUL, which waits for the user to press any key without printing anything to the screen). Under classic Amiga operating systems, the device's name is NIL:. In Windows NT and its successors, it is named /Device/Null internally, though, the DOS NUL is a symbolic link to it. Similarly, in OpenVMS the device is named NL:.

   這個就和路由裏面的ip route 192.168.2.0 255.255.255.0 null 相通的。路由裏面是把來自特定IP的數據包拋棄。因此這種路由也叫做路由黑洞。有時候爲了避免路由環路,會用到這種配置。


 linux中,在shell腳本編程時,有時不希望命令運行結果或錯誤信息輸出到標準輸出,可以將其重定向到日誌文件

commond 2>&1 ,如果不希望錯誤信息寫入文件便可以將其輸出到dev/null.

/dev/zero:

In Unix-like operating systems, /dev/zero is a special file that provides as many null characters (ASCII NULL, 0x00; not ASCII character "digit zero", "0", 0x30) as are read from it. One of the typical uses is to provide a character stream for overwriting information. Another might be to generate a clean file of a certain size. Using mmap to map /dev/zero to RAM is the BSD way of implementing shared memory.

# Initialise partition (important note: trying out this command will eradicate 
# any files that were on the partition, make sure you have a backup of any important data.)
dd if=/dev/zero of=/dev/hda7

# Create a large empty file called 'foobar'
dd if=/dev/zero of=foobar count=1000 bs=1000

Like /dev/null, /dev/zero acts as a source and sink for data. All writes to /dev/zero succeed with no other effects (the same as for /dev/null, although /dev/null is the more commonly used data sink); all reads on /dev/zero return as many NULs as characters requested.

 

像/dev/null一樣, /dev/zero也是一個僞文件, 但它實際上產生連續不斷的null的流(二進制的零流,而不是ASCII型的). 寫入它的輸出會丟失不見, 而從/dev/zero讀出一連串的null也比較困難, 雖然這也能通過od或一個十六進制編輯器來做到. /dev/zero主要的用處是用來創建一個指定長度用於初始化的空文件,就像臨時交換文件.

關於 /dev/zero 的另一個應用是爲特定的目的而用零去填充一個指定大小的文件, 如掛載一個文件系統到環回設備(loopback device)或"安全地" 刪除一個文件.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章