Processes & Syscalls
Understand how Process Abstraction and System Calls work in xv6
Throughout this course you are expected to read through the xv6 kernel source, alongside the xv6 book, which explains the kernel’s design. This will make it WAY easier for you to work on your mini projects.
This homework covers two topics: how xv6 represents a process, and how a user program asks the kernel to do something on its behalf through a system call.
Process abstraction was covered in this lecture. Syscalls were covered in this tutorial.
Part 1: Process abstraction
Read the following files:
kernel/proc.hkernel/proc.c
Chapter 2 of the book (Operating system organization) will help, especially section 2.5 (Process overview).
Question
Suppose an xv6 kernel has used up all of the struct proc entries in the struct proc proc[NPROC] table (i.e. none of them have state == UNUSED). What happens if one of the processes calls fork()? What happens if one of the processes calls exit() and its parent never calls wait()? Does that free up a slot in the table?
Part 2: System calls
Read the following files:
user/user.huser/usys.plkernel/syscall.ckernel/syscall.hkernel/sysproc.c
Chapter 2.2 (User mode, supervisor mode, and system calls) and sections 4.3-4.4 of the book will help.
DIY: add a system call
Every process in xv6 has a parent (p->parent), and it uses fork() to birth children. Add a new system call, headcount(), that returns the number of living children the calling process currently has: processes in the table whose parent field points to you, and whose state isn’t UNUSED or ZOMBIE.
You will need to touch:
kernel/syscall.h, to give it a numberkernel/syscall.c, to register it in thesyscallsarraykernel/sysproc.c, to implementsys_headcount()kernel/defs.h&kernel/proc.c, to actually implementheadcount()user/usys.planduser/user.h, to create the user-space stub
Write a small user program that forks off a few children, then calls headcount() and prints the result. It should print however many children you actually forked.
Note: There are some parts of the reading assignment which you may not understand right now for example the call to
kvmmapinkernel/proc.c, these are functions to manage memory and page table. Which will be done in more detail further in the course. If you still need a reference, head over to the section 3.3 in the book which will explain it in brief.
AnswersPart 1Note: The solution to the DIY section is given as a git patch, to apply it to your own copy of the xv6 source, run
git apply patch.diff.