Search

A look into the /dev/null driver

We must have used the /dev/null to send the unwanted output from the terminal , its like a sinkhole into which we can dump any data and it is lost forever. We can only write into it but never read out of the /dev/null. For example



The string goes into the /dev/null , but does not get saved in it. If /dev/null is read, it will print nothing.

Let us look under the null device and see how this is achivedin the kernel. The null device is a charachater device, which can be noticed by listing it



The first charahcter is "c" which indicates it is a charachter device. Being a charachter driver it should have a read operation and writer file operations mapped to it. These can be found in the file mem.c . The write_null and read_null functions meant to read and write from the null device are written in the file mem.c . Let us look at the write_null



As can be seen the function does not store any data, but just returns a count of data recevied. Thus any data written into the devices does not get stored in any location and is just lost completely.

Similarly if we look at the read_null function.



We can see that the above function will always return a 0, every time is read and does not really return any data to the user space. Thus reading out of the null driver never produces any output, but the read operation succeeds everytime.