CS110: Principles of Computer Systems
Winter 2021-2022
Stanford University
Instructors: Nick Troccoli and Jerry Cain
Unix v6 Filesystem design, part 1 (files)
Unix v6 Filesystem design, part 2 (large files + directories)
Interacting with the filesystem from our programs
assign2: implement portions of a filesystem!
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
Filename
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
Last modified time
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
Size (bytes)
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
Group name
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
Owner name
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
# Hard Links
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
Type and permissions
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
Current directory
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
Parent directory
troccoli@myth54:~/assign1$ ls -al
total 42
drwx------ 5 troccoli operator 2048 Jan 10 08:41 .
drwxr-xr-x 51 troccoli operator 6144 Jan 9 15:12 ..
drwx------ 8 troccoli operator 2048 Jan 9 15:12 .git
-rw------- 1 troccoli operator 259 Jan 5 15:31 .gitignore
-rw------- 1 troccoli operator 1750 Jan 9 15:12 imdb.cc
-rw------- 1 troccoli operator 3501 Jan 5 15:31 imdb.h
-rw------- 1 troccoli operator 6439 Jan 5 15:31 imdbtest.cc
-rw------- 1 troccoli operator 1720 Jan 5 15:31 imdb-utils.h
-rw------- 1 troccoli operator 964 Jan 5 15:31 Makefile
drwx------ 2 troccoli operator 2048 Jan 5 15:31 .metadata
-rw------- 1 troccoli operator 2146 Jan 5 15:31 path.cc
-rw------- 1 troccoli operator 4122 Jan 5 15:31 path.h
-rw------- 1 troccoli operator 1829 Jan 5 15:31 search.cc
drwx------ 2 troccoli operator 2048 Jan 5 15:31 tools
Type and permissions
owner
Here, the owner has read, write, and execute permissions, the group has only read and execute permissions, and the user also has only read and execute permissions.
Filesystem represents permissions in binary (1 or 0 for each permission option):
111 101 101
we can further convert each group of 3 into one base-8 digit
base 8: 7 5 5
rwx r-x r-x
group
other
open()
A function that a program can call to open a file:
int open(const char *pathname, int flags);
Many possible flags (see man page for full list). You must include exactly one of the following flags:
O_RDONLY: read only
O_WRONLY: write only
O_RDWR: read and write
Another useful flag is O_TRUNC: if the file exists already, clear it ("truncate it").
open()
A function that a program can call to open (and potentially create) a file:
int open(const char *pathname, int flags, mode_t mode);
You can also use open to create a new file if the specified file doesn't exist. To do this, include O_CREAT as one of the flags. You must also specify a third mode parameter.
Another useful flag here is O_EXCL: the file must be created from scratch, fail if already exists
Aside: how are there multiple signatures for open in C? See here.
close()
A function that a program can call to close a file when done with it.
int close(int fd);
It's important to close files when you are done with them to preserve system resources.
You can use valgrind to check if you forgot to close any files.
// Create the file
int fd = open("myfile.txt", O_WRONLY | O_CREAT | O_EXCL, 0644);
// Close the file now that we are done with it
close(fd);
read()
A function that a program can call to read bytes from an open file.
ssize_t read(int fd, void *buf, size_t count);
Key idea: read may not read all the bytes you ask it to! The return value tells you how many were actually read.
Key idea #2: the operating system keeps track of where in a file a file descriptor is reading from. So the next time you read, it will resume where you left off.
write()
A function that a program can call to write bytes to an open file.
ssize_t write(int fd, const void *buf, size_t count);
Key idea: write may not write all the bytes you ask it to! The return value tells you how many were actually written.
Key idea #2: the operating system keeps track of where in a file a file descriptor is writing to. So the next time you write, it will write to where you left off.
Let's write an example program copy that emulates the built-in cp command. It takes in two command line arguments (file names) and copies the contents of the first file to the second.
copy-soln.c and copy-soln-full.c (with error checking)
File descriptors are just integers - for that reason, we can store and access them just like integers.
There are 3 special file descriptors provided by default to each program:
Let's build on our copy.c program to add support for copying to multiple files, and have it also output the contents of the file to the terminal.
copy-extended-soln.c and copy-extended-soln-full.c (with error checking)
process control blocks
, and they are stored in the process table
file descriptor table
read, write,
and close
)$ cat in.txt > out.txt
works)bash
shell calls make
, which itself calls g++
, each of them inserts text into the same terminal window: those three files could be stdin, stdout, and stderr for a terminalNone of these
kernel-resident
data structures
are visible to
users. Note the
filesystem itself
is a completely
different
component, and
that filesystem
inodes of open
files are loaded into vnode table entries. The yellow inode in the vnode is an in-memory replica of the yellow sliver of memory in the filesystem.
Next time: more details on system calls, and an introduction to multiprocessing