xv6, line by line
user/sync.c

user/sync.c

C · 10 lines · annotated 100% · user program / library · upstream

About this file

sync waits until every file system change made so far is safely on disk. It is a single system call, sync(), whose kernel side is sys_sync in kernel/log.c.

Why is there anything to wait for? xv6 makes every file system system call part of a transaction in the write-ahead log. Several calls running at the same time share one transaction, and it is written to disk (commit) only when the last of them finishes (end_op). So when your write returns, its changes may still be waiting for some other process’s operation to end. sync waits for the commit that includes them: if any operation is in progress or a commit is running, sys_sync sleeps until the commit counter ncommit goes up by one; if nothing is in progress, everything is already on disk and it returns at once.

This is different from Unix, where sync asks the kernel to write out dirty buffers that would otherwise be written later. xv6 never holds committed changes back: commit writes the log, then installs the blocks at their home locations, before it returns.

Read before: kernel/log.c (the commit path). Read next: user/logstress.c, which exercises the log.

5int
6main(int argc, char *argv[])
7{
8 sync();
9 exit(0);