Issue
As I understand, the Device Files in Linux are nothing but a user interface for users to communicate with the actual device. Is there ANY OTHER use of device files?
Also does this mean that if a user does not need to communicate with the real device, we do not need device files?
Also they tend to model every real device as either a character or block or network device. Is this true?
Solution
let's start with some historic facts:
First of all, it was the start of everything. The world was created on Thursday, January 1st, 1970 at 12:00a.m., UTC, according to the Legend of the Epoch, and thus there's no (standard) way to count any time before that, because it was simply non-existent ;).
Then, it was the '70s (actually, the late '60s, but who cares?), do you remember (I don't...)? We don't know what was Ken smoking at the time, but it appears to have been some sort of exotic ink used in the top-secret Bell Labs' files he has was supposed not have access to. Just an infinitesimal fraction of time before The Creation, he said...
Let everything be a file!
And so it was. On Unix, and its uncountable decendants, (almost) everything is a file, to the point that hell (otherwise known as /dev/null
) is a file.
On the Epoch was born the Kernel. It's the ultimate monarch whose sole purpose is to allow or deny access to Sacred Data. Ah! And to kill everyone who disboeys him (if you do *(int*)0 = 0
, you'll get a death penality due to the violation of the Address Space Law: Shall not you read, write, or otherwise access any address outside your address space
).
Now, because everything is a file, devices are files, no? There's no bad on it (except that showing everyone the fact that block devices are the shame they are is a stupid idea). And, because network resources are something, they're files, no?
Now, how does the Supreme Monarch handles this? First of all, for normal files, everything works as expected. The filesystem driver gets blocks from the massive-storage-device driver, then reads the superblock, blah, blah...
But, what happens will all the others? I usually think of them as "trap" files. Whenever you read or write to them, the kernel detects this special situation, and calls appropiate fallback functions to "simulate" the effect of reading and writing from a regular file. For /dev/null
(hell), for instance, all reads will return End-of-File, and all writes get discarded. In the case of files refering to devices, the call does not go directly to the device, but to the device driver, that is, a part of the kernel responsible for handling an specific hardware piece. Thus, there are no "files to devices", but "files to device drivers with some extra information to differentiate respective devices".
This allows special files, such as /dev/random
, /dev/zero
, et al... To exist through the same interface. Though they're not "files to devices/device drivers", the user has (almost) no way to differentiate between them, so who cares?
Also does this mean that if a user does not need to communicate with the > real device, we do not need device files?
Ritchie would turn in his grave if he would hear that... Those abstractions allow things such as device drivers and utility programs (such as the one that you use to format your USB drive) to be implemented in user-space (although with root permissions, anyway). From the kernel's point of view, there's no "user" as you think of it, but processes that are part of some group, known as a "user", identified by its "user ID".
Also they tend to model every real device as either a character or block or network device. Is this true?
Although in practice it's true, a device driver is not obligated to export its corresponding devices through special files.
I hope this has led some light on you!
Answered By - 3442