Operating Systems 202.1.3031: Spring 2022/2023 Assignment 1
Operating Systems 202.1.3031: Spring 2022/2023 Assignment 1
202.1.3031
Spring 2022/2023 Assignment 1
Applications
System calls
Kernel
Contents
1 Introduction 2
2 Submission Instructions 3
7 Scheduling Policies 9
1
Operating Systems 2022/2023 Assignment 1
1 Introduction
In the following sections we will perform a few tasks to get you familiar with
the xv6 OS and operating system development in general.
2
Operating Systems 2022/2023 Assignment 1
2 Submission Instructions
• Make sure your code compiles without errors and warnings and runs
properly!
• We recommend that you comment your code and explain your choices,
if needed. This would also be helpful for the discussion with the graders.
• You should submit your code as a single .tar.gz or .zip file.
• We advise you to work with git and commit your changes regularly. This
will make it easier for you to track your progress, collaborate and main-
tain a working version of the code to compare to when things go wrong.
• Theoretical questions in this assignment are not for submission, but
you are advised to understand the answers to these questions for the
grading sessions.
• Submission is allowed only in pairs and only via Moodle. Email sub-
missions will not be accepted.
• Before submitting, run the following command in your xv6 directory:
$ make clean
This will remove all compiled files as well as the obj directory.
• Help with the assignment and git will be given during office hours. Please
email us in advance.
3
Operating Systems 2022/2023 Assignment 1
Start by downloading the xv6 source code from the class GitHub.
Tasks will appear in this assigment in orange boxes like this one:
4
Operating Systems 2022/2023 Assignment 1
Let’s get started by writing a simple program that prints "Hello World" to
the screen! We will need to write user space programs to debug and test
our kernel code.
We will be writing a user space-only program, meaning that it does not run in
the kernel. However, note that our program is intended to run on xv6 inside
the QEMU emulator, not on our host operating system (e.g., Linux, macOS,
Windows, etc). It’s important to understand that the host system can’t run
or debug our program directly!
In order to build a new user space program, you’ll need take a look at an
existing user space program. For example, examine echo.c — see how it
works, and how it gets built by the xv6 makefile.
Hint: find echo.c and "_echo" in Makefile.
The following steps will guide you through the process of writing a
Hello World program:
1. Create a new file called helloworld.c in the user directory of xv6.
2. Add helloworld.c to the Makefile in the top-level directory.
3. Write C code that prints "Hello World xv6" to the screen.
4. Compile your program by running the following command:
$ make qemu
This essentially builds the entire xv6 system, including your pro-
gram. If your program isn’t built, check the Makefile to make sure
you added it correctly.
5. Run your program by running the following command:
$ . / helloworld
5
Operating Systems 2022/2023 Assignment 1
The goal of this part of the assignment is to get you started with system calls.
The objective is to implement a simple system call that outputs the size of
the running process’ memory in bytes and a user space program to test it.
To accomplish this, you will need to modify the xv6 kernel code and add a
new system call, called memsize(). The (user space) signature of this new
system call should be:
i n t memsize ( void ) ;
This system call should return the size of the running process’ memory in
bytes. Look at the list of system calls, and find one that performs a similar
function, getting data from the current running process. The size of a given
process can be obtained from the PCB.
6
Operating Systems 2022/2023 Assignment 1
Questions:
1. How much memory does the process use before and after the allo-
cation?
2. What is the difference between the memory size before and after
the release?
3. Try to explain the difference before and after release. What could
cause this difference? (Hint: look at the implementation of malloc()
and free()).
The goal of this task is to add an ”exit message” to exit(). This exit message
will be saved in the PCB and can be retrieved by the parent process using
the wait() system call, which we will modify as well. Then, we will write a
user space program called goodbye.c that immediately calls exit() with the
message ”Goodbye World xv6”. Finally, to show that we can retrieve the exit
message, we will modify the shell to print the exit message when a child
process of the shell terminates.
7
Operating Systems 2022/2023 Assignment 1
8
Operating Systems 2022/2023 Assignment 1
7 Scheduling Policies
Questions:
In the following tasks, we will replace the current scheduling policy by testing
two new policies and measuring the impact of these policies on the perfor-
mance of the system.
9
Operating Systems 2022/2023 Assignment 1
which can be used by a process to change its own priority. The priority of a
new process is 5, the lowest priority is 10 and the highest priority is 1 (thus,
lower values represent higher priorities).
Each time a process finishes its time quantum (that is, each time it exhausts
it, but remains runnable), add the process’ priority to its accumulator field.
Each time a new process is created, or a process shifts from blocked to
runnable, set its accumulator to the minimum value of the accumulators of
all the runnable/running processes. If it is the only runnable process in the
system, set its accumulator to 0. This gives high priority to new processes
or to processes returning from I/O. In case of a tie between two or more
processes with the same accumulator value, run the first in order within
the array. Note that the accumulator fields of processes with high priorities
(small values) grow more slowly, so the scheduler will favor them.
1. Add the long long accumulator and int ps_priority fields to the
PCB.
2. Update these fields when the appropriate events occur (see
trap.c, proc.c).
3. Implement the set_ps_priority() system call.
4. Modify the scheduler and other places in the OS as needed.
For submission: Modified OS files that implement priority scheduling
and the set_ps_priority() system call.
10
Operating Systems 2022/2023 Assignment 1
run time
vruntime = decay factor ×
run time + sleep time + runnable time
In case of a tie, the first process in the array that tied will be selected.
The decay factor1 is defined using the concept of a process priority. Three
priority levels should be supported:
The default priority for a new process is normal. Calling fork() will copy the
priority of the parent process to the child process.
11
Operating Systems 2022/2023 Assignment 1
Questions:
1. Is run time and sleep time of a process the same thing? If not,
what is the difference between them?
2. Is the run time and runnable time of a process the same thing? If
not, what is the difference between them?
3. Run the test a few times. Did you notice any difference in the
run times, sleep times, and runnable times of the three child pro-
cesses? Explain your results.
4. Do the three priority levels actually matter for which process gets
more CPU time? Does it make sense to give a higher priority to a
process that we want to run more often?
5. Does this policy make sense for a real-life operating system?
Explain your answer.
12
For submission: cfs.c and modified OS files that implement CFS with
Priority Decay and the set_cfs_priority() and get_cfs_stats() sys-
tem calls.
Operating Systems 2022/2023 Assignment 1
A global variable that keeps the scheduling type should be declared at the
end of defs.h:
i n t s c he d _p o li c y ;
13