I have found out that in Linux there are "real files" and there are "virtual files", real files are files that resides on the hard disk, while virtual files are just data represented by the kernel as files.
For example, the files in the /proc
directory are virtual files.
I want to understand how a function like read()
knows how to read a real file and how to read a virtual file. I have created the following diagram to show my understanding of this subject, please correct me if I am wrong about my understanding:
read(2)
is a system call. Once the userland process callsread()
control passes to the kernel, who of course knows perfectly well where to go for the data; the source can be a filesystem backed by persistent local storage, or a socket, or a FIFO, or a named pipe, or, yes, a virtual file.read()
issues a system call, but it is not itself a system call.read()
is a POSIX-standard function. Per the POSIX standard: "The read() function shall attempt to read nbyte bytes ... if nbyte is zero, the read() function may detect and return errors ..." As @ Paul says, it may issue a system call, butread()
itself is a function.