Wednesday, August 6, 2008

what exactly is the subtle difference between /dev/zero and /dev/null ?

what exactly is the subtle difference between /dev/zero and /dev/null ?

the following is what i got when i gave the command man zero on the terminal

Data written on a null or zero special file is discarded.

Reads from the null special file always return end of file, whereas
reads from zero always return \0 characters.

null and zero are typically created by:

mknod -m 666 /dev/null c 1 3
mknod -m 666 /dev/zero c 1 5
chown root:root /dev/null /dev/zero


colucix

$ dd if=/dev/null of=testnull count=1 bs=512
0+0 records in
0+0 records out
0 bytes (0 B) copied, 1.9969e-05 s, 0.0 kB/s

$ dd if=/dev/zero of=testzero count=1 bs=512
1+0 records in
1+0 records out
512 bytes (512 B) copied, 6.037e-05 s, 8.5 MB/s

$ ls -l test*
-rw-r--r-- 1 colucix users 0 2008-08-06 10:58 testnull
-rw-r--r-- 1 colucix users 512 2008-08-06 10:58 testzero

$ od -c testnull
0000000

$ od -c testzero
0000000 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0001000

$ rm testnull testzero


David the H

/dev/zero is an endless source of zeros. It's there to provide a simple raw, data stream of unvarying content. /dev/null is a black hole. Anything sent into it just disappears. It's the programmers trash can.

So /dev/zero is meant as a source of (empty) data, and /dev/null is meant as a destination for (unwanted) data.


Mr C

/dev/zero returns 0's, not empty data.

/dev/null is also useful for reading - it returns EOF upon read input.


David the H

I said it returned zeros. Perhaps I got my terminology wrong, but I'd consider a stream of zeros to be pretty much empty data. I've mostly seen it used for blanking out file systems and such in any case.


Mr C

You can use whatever terminology you want. But mathematically, there is a big difference between an empty data set and a set of infinite zeros. A zero is not "empty data", it is in fact, specifically not-empty. Loose terminology confusing rather than clarifies.

mcduck

Well, you pretty much already said the difference in your post. Reading from /dev/zero provides you with zeroes, while reading from /dev/null provides no data.

So you can use /dev/zero to create empty files of certain sizes, or to overwrite disks with zeroes, while /dev/null wouldn't work for this kind of purposes.


Jean david beyer

The simple way of looking at these is to say

if you write to /dev/null, what you wrote disappears.

if you write to /dev/zero, what you wrote disappears.

if you read from /dev/zero and you get as many \0 characters as you ask for.

If you read from /dev/null, you get no characters and an end-of-file return.

Florian diesh

When you read from /dev/zero you get 0-Bytes, when you read from
/dev/null you get EOF


colucix

In the original context - as proposed by the OP - /dev/zero is a special file which provides as many zeros as you want to read. From a computer science point of view it provides a sequence of N bytes, each one representing the number zero. If you store them in a file, it will have a finite size, other than zero.

/dev/null is a special file that discards all the data sent to it and provides no data at all if you try to read from it (actually it returns EOF as already stated by Mr.C). If you try to store /dev/null in a file, you will end up with a file size equal to zero. Not a single bit is written, nor zero, nor one.
__________________
Theory is when you know all and nothing works. Practice is when all works and nobody knows why. In this case we have put together theory and practice: nothing works... and nobody knows why! (Albert Einstein)

No comments:

Post a Comment