It's a feature of the cgroup
filesystem, not a feature of the mkdir
command. The mkdir
command just invokes the mkdir
system call. The mkdir
system call invokes the VFS layer in the kernel, which performs some analysis on the command and in particular determines on what filesystem the directory is to be created by analyzing the hierarchy of mount points in the directory tree. Then the VFS layer calls the code in the driver for that filesystem, which is the cgroup
filesystem driver.
What a filesystem driver does for each filesystem operation (open file, read, write, create directory, remove directory, etc.) is up to that filesystem driver. A filesystem for on-disk storage will modify the disk content to record the existence of a directory as directed. A network filesystem will send a command over the network. The proc filesystem reports information about processes and about the kernel, and the cgroup filesystem does something similar.
The behavior of the cgroup filesystem is described in the kernel documentation.
New cgroups are created using the mkdir system call or shell
command. The properties of a cgroup, such as its flags, are
modified by writing to the appropriate file in that cgroups
directory, as listed above.
When the cgroup filesystem driver receives an mkdir
command at that level of the hierarchy, it interprets that to mean “create a new cgroup” with the given name. When it receives a readdir
command (the system call that enumerates directory entries) in /cgroup/cpu0
, it lists the cgroups on that CPU. When it receives a readdir
command in /cgroup/cpu0/group0
, it reports a fixed set of files that reflect that cgroup's parameters as stored in memory. When it receives a read
or write
command on those files, it reports or modifies the corresponding parameter.
The designers of cgroup chose to have it entirely controlled via the filesystem rather than add new system calls. That makes it easy to use without having to develop much library code and many utilities.
mkdir
will at some point call filesystem code, and different filesystems can do different things when "creating directories". (I have not double-checked if this is actually how the cgroups files are created.) – Ulrich Schwarz Mar 16 '16 at 21:09