Xv6 Chapter1
Operating system interfaces
Preface
Let us go to the world of xv6.
— 2026/11/11
Operating System
An operating system is designed to manages and abstracts the low-level hardware.
To keep each process isolated, the kernel use hardware protection mechanisms provided by CPU.When more permissions are needed(the function-like calling is known as system call),the user program invokes a system call,the hardware raises the privilege level
Process and memory
An xv6 process consists of user-space memory.
All C library are not allowed to use.Because it depends on the x86/AMD assembly while the xv6 operating system is based on risc-v.
Luckily,most of useful functions are provided.We can check all functions ,which is able to be called freely,in user/user.h
All right.Let us gain an inspection on some of functions provided
forkfunction signature
int fork()forkis used to create a child process which possess a exact copy of the calling process’s memory.forkcopy everything of calling process into the new process’s memory.Just like we know,
forkreturns in both original and new processes.In the original process,it returns the new process’s PID,while in the new process,it returns zero.If receiving a negative number,it means that fork failed.exitfunction signature
int exit(int status)exitsystem call causes the calling process to stop and release resource such as memory and file descriptorWhen calling
exit,a status argument is reuquired.Convetionally,0 indicates success and 1 stands for failurewaitfunction signature
int wait(int* status)Unlike
waitin Linux,waitin xv6 is similar towaitpid(-1,&status,0)in Linux.it returns the PID of an exited child process and copies the exit status of the child to the address passed to wait.
if none of the caller’s children has exited,
waitwaits for one to do so.If the caller has no children,waitreturns -1 immediately.Supposing the caller have no interest in the status of a child,passing 0(also NULL) is allowed.
execfunction signature
int exec(char *file, char *argv[])Just like the
execvein Linux,execsystem call replaces the calling process’s memory with a new memory image loaded from a file stored in the file system.Xv6 operating system uses ELF format as executable program.Considering
execsucceeds,it does not return.If it returns,we can ensureexechas failedWhen calling
exec,two arguments are passed:the path of the executable file and an array of string arguments(the path included).
I/O and File descriptors
file descriptor is a small integer representing a kernel-managed object that a process may read from or write to.
To clarify it,a file descriptor is obtained while opening a file,directory,device or by creating a pipe.
The xv6 kernel uses descriptor as an index into a per-process table.Every process has a private space of file descriptor starting at zero.
By convention,a process reads file descriptor 0(standard input),writes output to file descriptor 1(standard output),writes error messages to file desciptor 2(standard error).
readandwritefunction signature
int read(int fd, char *buf, int n) int write(int fd, char *buf, int n)readis used to read n bytes at most from the file descriptor and store them into buf.NOTICE:
readalways not ensure n bytes are readreadreturns the number of byte read as a matter of fact.So,when calling
read,we always need to use while,if continuing callingread,readreads data from the current file offset.(the offest is stored bystruct file)In xv6,
readis not only used to read file but directory.When reading the directory,the files(including directory) are abstracted as a array ofdirent.struct dirent { ushort inum; char name[DIRSIZ] __attribute__((nonstring)); };Each
readjust reads one singledirent,so it is a good choice to usewhileto read all files in current directory.So,what is file descriptor in fact?
We can regard a file descriptor as an index of the files kept in kernel.
In fact, the profile of file is in
struct file,including reference count, flags ,file offset and so on.Check it inlinux/fs.hopenandclosefunction signature
int open(char *file, int flags) int close(int fd)The second parameter of
openis flags,which includesO_RDOLNY,O_WRONLY,O_RDWR,O_CREATE,O_TRUNC.They are defined infcntl.hand obvious.The kernel will choose a smallest usable number as file descriptor.
closesystem call is used to close the file descriptor instead of the file.In a word,We just change the reference count of file.When the count of reference is zero,thestruct fileis released.dupfunction signature
int dup(int fd)dupis the short for “duplicate”,which works as duplicating the file descriptor pointing the same file and increasing the count of reference.When calling
fork,everything is almost copied to the new process,and thestruct fileincluded.
Pipes
pipe is small kernel buffer exposed to processes as a pair of file descriptors,one for reading and one for writing.Pipes provide a way for interprocess communication.
function signature
int pipe(int pipefd[2]);pipe system call initialize a two-element array,one for reading and another for writing.
Let us have a look an example provided by Linux documentation.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int pipefd[2];
char buf;
pid_t cpid;
if (argc != 2) {
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Child reads from pipe */
close(pipefd[1]); /* Close unused write end */
while (read(pipefd[0], &buf, 1) > 0)
write(STDOUT_FILENO, &buf, 1);
write(STDOUT_FILENO, "\n", 1);
close(pipefd[0]);
_exit(EXIT_SUCCESS);
} else { /* Parent writes argv[1] to pipe */
close(pipefd[0]); /* Close unused read end */
write(pipefd[1], argv[1], strlen(argv[1]));
close(pipefd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}
}How does it work?
We call pipe to initialize two file descriptor,one for writing and another for reading.And when forking, the pair of file descriptors are copied.For safety,it is a must to close the useless end(1 indicates writing end ,0 indicates reading end).
When we call write on write-end,the data is written into pipe blocked.
When we call read on read-end,the data is read into buffer.However, if there is no data in the stream,read will wait for either data to be written or for all file descriptors referring to the write end to be closed.In the latter case,read will return 0,just as if the EOF had been reached.
In the xv6 shell | is regarded as pipelines because it works just like a pipe and pass the output current instruction to next instruction as the input.