Tuesday 21 August 2012

how do you create /dev/null device and Use ?

Unix-like operating systems/dev/null or the null device is a special file that discards all data written to it .

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.
/dev/null is a special file, not a directory,

#man mknod
mknod - make block or character special files
        -Z, --context=CONTEXT
              set security context (quoted string)

       -m, --mode=MODE
              set permission mode (as in chmod), not a=rw - umask

       --help display this help and exit

Both MAJOR and MINOR must be specified when TYPE is b,  c,  or  u,  and
 they  must be omitted when TYPE is p.  If MAJOR or MINOR begins with 0x
 or 0X, it is interpreted as hexadecimal; otherwise, if it  begins  with 0, as octal; otherwise, as decimal.  TYPE may be:

       b      create a block (buffered) special file

       c, u   create a character (unbuffered) special file

       p      create a FIFO

I want to create a device file without using mknod command. However, I want to create a device file using udev framework in Linux Kernel.

How to create /dev/null 

null, zero - data sink
  DESCRIPTION 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.

How to create /dev/null 

Does anyone know how to create /dev/null

"man null"
/bin/rm /dev/null
#mknod -m 666 /dev/null c 1 3
#chown root:mem /dev/null

[root@new-host dev]# ls -ld /dev/null /dev/zero
crw-rw-rw- 1 root root 1, 3 Aug 21 09:57 /dev/null
crw-rw-rw- 1 root root 1, 5 Aug 21 09:57 /dev/zero
[root@new-host dev]#

Linux 

$ dir /dev/null /dev/zero
crw-rw-rw-    1 root     root       1,   3 Oct 22 20:04 /dev/null
crw-rw-rw-    1 root     root       1,   5 May  5  1998 /dev/zero

Solaris 

$ dir /dev/null /dev/zero
lrwxrwxrwx   1 root     root   1998 /dev/null -> ../devices/pseudo/mm@0:null
lrwxrwxrwx   1 root     root   1998 /dev/zero -> ../devices/pseudo/mm@0:zero
$ dir /devices/pseudo/mm@0:null /devices/pseudo/mm@0:zero
crw-rw-rw-   1 root     sys   17:39 /devices/pseudo/mm@0:null
crw-rw-rw-   1 root     sys    1998 /devices/pseudo/mm@0:zero

your 'cat /dev/null > file' example would just create a file with no data in it what so ever, its size would be zero.



No you can't send a file to /dev/null. You could display a files contents by doing:
cat filename
by that doesn't use up the file. It's still there. So
cat filename > /dev/null
just wastes time.

To delete a file, do
rm filename


Maybe a couple more examples will make it clear.

echo hello
echo hello > garbage.txt
echo hello > /dev/tty
echo hello > /dev/null

The first one will display hello for you to see, and the second one will send the word hello into a file. I think that you got that much.

The third one is explicitly doing for the shell did automatically. /dev/tty is not your ordinary file, it is a "special file". Instead of being stored in a file on disk, a special little program called a driver is going to get that "hello" string. The tty driver will figure out how to make the characters appear on your screen. And then it will tell the OS, ok...that "hello" string went to the device.

The fourth one is also going to a special file and again a driver will get the string "hello". But this driver is just going to tell the OS, ok...that "hello" string went to the device. But there is no device and the null driver did not try to do anything. It just tosses the characters away.

You can read from /dev/tty. And the driver will wait for you to type something and read those characters and send them on.

You can read from /dev/null too, but the driver will always claim that there is no data.

==============================
$cat > om
abc
xyz
uuu
ddd
eee
weww

$ls -ld om
-rwxr-xr-x 1 sankar sankar 124 Dec  9  2010 om

Q:how to reduce om file size 0/empty ?

$ cat /dev/null >om

-rwxr-xr-x 1 sankar sankar 0 Aug 21 15:37 om

1 comment:

  1. I have one written one pseudo block driver code, can one API with read and write operation, now how to read and write into pseudo device through my driver code and APIs.

    ReplyDelete