Your browser is unsupported

We recommend using the latest version of IE11, Edge, Chrome, Firefox or Safari.

8: The /proc file system

In Linux, the /proc directory holds a number of files that don't have a representation on disk. These "virtual" files describe the state and configuration of the system, its processes, and more. Indeed, even the /proc directory contents don't exist on disk. Instead, the contents of the directory is dynamically generated every time it is read.

In this homework, we introduce the /proc file system to xv6.

Virtual directory listing Heading link

Create a directory /proc using mkdir. Modify xv6 so that when ls /proc is executed, it shows a list of “virtual” files – files that don’t actually exist on disk. There should be one directory per running process, and a file called meminfo.

To help you with this part, the hw8 template in the git repository includes some changes to the file system code. Specifically, struct inode now contains a pointer to a struct inode_functions, which in turn holds functions for reading and writing file contents, and for populating inode data.

To get started, change the i_func pointer of the inode for “/proc”, so that when we try to read “/proc”, your functions are called. To provide a directory listing, see how “ls.c” reads it, and write a procfs_readi function to match. The function namei() returns a struct inode* given a path. To populate the struct (read in inode contents from disk), use ilock().

Make sure “ls /proc” displays the appropriate names listed above, and sensible types and sizes. For this, you’ll need to implement procfs_ipopulate. Watch out for iget() here: use a different device number for “proc” files to avoid reusing the wrong inode.

meminfo and cpuinfo files Heading link

Update xv6 so that meminfo shows the total amount of memory and the amount of memory currently free. cpuinfo shows how many cores are available.

process directories and files Heading link

For each running process, the corresponding directory (named by pid) should contain several files, including name, and parent.

automatically create /proc folder and then mount proc Heading link

When you’re testing, it’s fine to start by manually creating the proc folder, rebooting and having main() do the mounting. However, during grading, the /proc folder won’t exist, and you’ll lose points if your program doesn’t work right on the first run.

So, have init.c create the /proc folder using mkdir(). But on the first run, you still don’t have a /proc when main() runs. So, for full points, create a sys_mount() system call, and call that from init.c. Of course, once you have that, it’s a hop, skip and a jump to the bonus points below:

Bonus Points: Flexible user-space mounting Heading link

Add a system call mount(), and an executable mount that allows the user to mount any named file system (proc being the only one supported at the moment), on any directory.

FYI, we’ll be testing this on a folder name other than /proc.

More Bonus Points: process memory file Heading link

In addition to the parent and name files in each process folder, provide a memory file. This file should be of size proc->sz. The contents of this file should be identical to the contents of the process’ virtual memory. If a byte between address zero and proc->sz is inaccessible (page not present, or insufficient permissions), just pretend it was a zero: the file system has no way to express permissions on a per-byte basis.

turn-in Heading link

Use this link to create your turn-in repository, then push your solution to the hw8 branch.