Why does the root directory '/' have a reference to its "parent"?
I'm currently developing a fake filesystem for a browsergame. I recently implemented ".." and "." folders so every folder has a reference to its parent. Then I checked in my terminal if "/" also has these folders. Actually I was very surprised that it has a directory ".." which obviously is a reference to itself.
Is the reason for this consistency or is there a better explanation?
Edit: I'm basically looking for the document where this is documented.
There is this POSIX definition :
3.144 Empty Directory
A directory that contains, at most, directory entries for dot and dot-dot, and has exactly one link to it (other than its own dot entry, if one exists), in dot-dot.
This definition is repeated with different phrasings in the POSIX descriptions of all file-commands.
For example for rmdir :
The definition of an empty directory is one that contains, at most, directory entries for dot and dot-dot.
This above convoluted definition for an Empty Directory is apparently behind this funny convention, and its purpose is to avoid any exceptions to the rule, not even for slash (/).
The Single UNIX Specification states:
As a special case, in the root directory, dot-dot may refer to the root directory itself.
No reason is stated in the standard. For an official "why?" answer, you may need to ask Ken Thompson directly.
File system traversal.
When you are looking at a file name on Unix systems, you're looking at a rooted tree.
When you're looking at a file, it is in a directory. You can ask that directory what it is, by going up a level (..), and checking for the inode of the directory you're in. You then repeat, and you can build up where you are. But when you reach the root, there is no up. There is only 'here'. By setting '.' and '..' to the same value, you set a unique signal that no other directory in that file system can have. It is the root.
When you mount a file system to another mount point - you have a /home filesystem on another disk, for example, you override the '..' with a reference to the mount point on the root file system. So the mounted file system root that used to have a '.' and '..' that were the same, now have different values.
Having '.' and '..' repeat the same data is an important, unique signal, true only for the top of the tree. And that tells traversal programs they can stop looking for parent nodes.
I think there is tangential documentation in the Lions Commentary To The Unix Kernel version 6. Around Page 84, where it describes how the mount point is handled.