Introduction
Project 1 will cover the following:
Part 1 – Implementing the uniq command
Part 2 – Implementing the head command
This project is a total of 600 points.
As we are creating simplified versions of actual Unix commands, please test out the actual Unix commands that you are unfamiliar with before you begin to develop your own.
Assignment Notes
General Notes
Please read the assignment instructions carefully, including what files to include/submit.Don’t assume any limitations unless they are explicitly stated.Developing optimized solutions is good, but I won’t deduct any points if your code is not optimizedPlease ensure the functionalities requested in these assignments are included; you can add extra but cannot remove any.When in doubt regarding what needs to be done, ask. Another plausible option is to test it in the real UNIX or Ubuntu operating system. Unix is your friend; use it wisely. Does your code behave the same way? If not, debug. Remember to use man “command name” for reference.Test your solutions and make sure they work. A.ach your screenshots showing your steps, your console, and other details.
File Submission Instructions:
The file should be submitted in zip format, with 2 folders (uniq and head)
Each folder should consist of the following documents:
Screenshots showing successful execution of the command “make clean”.Screenshot showing successful execution of the command “make qemu” or “make qemu-nox”.Output of all conditions provided in your rubric.A detailed Readme file that explains your logic (code section), and steps followed to run your code
(Attach a screenshot and then explain the steps). If README files are missing, then we will deduct 25 points for each command (25 for uniq and 25 points for head).
All c scripts and modified Makefile.
NOTE: Make sure your code compiles using the Makefile! You will automatically lose 50% of the points if your code does not compile or does not add the command to xv6. The remaining points will be up to the discretion of the person grading the assignment based on whether what exists seems to properly implement the expected functionality.
Academic Dishonesty: We will be checking your code against other submissions in the class for logical redundancy. If you copy someone else’s code and submit it with minor changes, we will know. These cheat detectors are quite hard to fool, so please don’t try. We trust you all to submit your own work only; please don’t let us down. If you do, we will pursue the strongest consequences available to us.
Getting Help: You are not alone! If you find yourself stuck on something, contact the course TAs/Instructor for help. Office hours are there for your support; please use them. If you can’t make our office hours, let us know and we can talk over email or schedule a one-on-one meeting. We want these projects to be rewarding and instructional, not frustrating and demoralizing. But, we don’t know when or how to help unless you ask.
Course Effort: Start your assignment early, this not only depicts effort put into this class but allows for ample time for TA access and help. If there are questions/useful points to make note of, please use the course discussion page to discuss and ask relevant questions.
Part 1 – Implementing uniq on xv6 as user program and also as system call – 300 points
The uniq command in Linux is a command-line utility that reports or filters out the repeated lines in a file. In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and also deletes the duplicate lines. Uniq filters out the adjacent matching lines from the input file (that is required as an argument) and writes the filtered data to the output file. For instance, look at a simple example from a file called OS611example.txt. The cat command will print out all the contents from the file, and uniq removes duplicates from the adjacent line.
I understand the Operating system.I understand the Operating system.I understand the Operating system.I love to work on OS.I love to work on OS.Thanks xv6.
Now, when I run the uniq command on the example.txt I get the following $uniq OS611 example.txt
I understand the Operating system.I love to work on OS.Thanks xv6.
Part 1 Task-1 – User space –
create uniq.c that performs the above task by replicating the functionality of uniq. This program should display/print the following message ” Uniq command is getting executed in user mode, ” followed by content. In the above case, you will get the following output
$uniq OS611example.txt
“Uniq command is getting executed in user mode.”
I understand the Operating system.I love to work on OS.Thanks xv6.
Part 1 Task-2 – Kernel space –
Create a system call named uniq. This should display/print the following message “Uniq command is getting executed in kernel mode”, then followed by content.
Rubric for Part-1
We assume all commands are working in user and kernel mode.
Program is working in user mode but not kernel mode. (-100)If uniq does not compile in any mode (user or kernel mode). (-200)If nothing works, then based on logic, some points will be provided (range 0 -100).If any debug statements (printf) are getting printed alongside output. (-15)If the program does not terminate successfully (-100)cat “filename” — uniq does not work (-25)uniq -c “filename” does not work (-15)uniq -i “filename” does not work (-25)uniq -d “filename” does not work (-15)
Part 2 – Implementing head on xv6 as user program and also as system call – 300 points
It is complementary to the Tail command. .e head command, as the name implies, prints the top N number of data of the given input. By default, it prints the first 14 lines of the specified files. If more than one file name is provided, then data from each file is preceded by its file name. If no filename is provided, the head should read from standard input. You should also be able to invoke it without a file and have it read from standard input. (Important: If the first argument does not start with -, the number of lines to be printed should default to 14).
Like uniq, you will also implement head command in user and kernel mode.
Part 2 Task-1 – User space –
create head.c that performs the above task by replicating the functionality of the head. This program should display/print the following message: “Head command is getting executed in user mode, ” followed by content.
Part 2 Task-2 – Kernel space –
create a system call named head. This should display/print the following message: “Head command is getting executed in kernel mode”, then followed by content.
Rubric for Part-2
The program is working in user mode but not kernel mode. (-100)If the head program does not compile in any mode (user or kernel mode). (-200)If nothing works, then based on logic, some points will be provided (range 0 -100).If any debug statements (printf) are ge.ing printed alongside output. (-15)If the program does not terminate successfully (-100)head “filename” does not work. Assuming it works but doesn’t respect the default settings = 14 lines. (-50)head -n “some number” “filename” does not work (-45)A SMALL REQ*UEST FROM MY SIDE I AM USING XV6 OPERATING SYSTEM ON WINDOWS USING WSL(WINDOWS SUB SYSTEM FOR LINUX) AND UBUNTU LTS(VERSION 22)YOU PLEASE MAKE SURE YOU DELIEVER THE CODE WITH WHOLE XV6 REPOSITORYAND UPLOAD A EXECUTION VIDEO ANDNOTE: PLEASE MAKE SURE ALL THE REQUIREMENTS ARE SATISFIED WITH SCREENSHOTS AND README FILES.THANK YOU IF YOU HAVE ANY PROBLEMS REGARDING ANYTHING PLEASE FEEL FREE TO REACH OUT TO MEPLEASE TRY TO MAKE A EXECUTION VIDEO OF OUTPUT USING WINDOWS ENVIRONMENT IT WILL HELP ME.IF YOU NEED ANY ADDITIONAL RESOURCES PLEASE LET ME KNOW Xv6 – HW1 Additional Info
Making Example System Calls
High Level Steps
•
Write your program
• Include the program in the XV6 source code
• A way of uploading the program to XV6
• Compile and run XV6 with QEMU
• Run the program inside XV6
Definition of a System Call in the Kernel Land
SYSCALL([NAME]) in usys.S:
• SYSCALL(mkdir)
• This line translates to the following assembly:
• .globl mkdir;
• mkdir:
•
movl $SYS_mkdir, %eax; //putting the system call number in eax
//calling the system call handler in interrupt mode
int $T_SYSCALL;
We have to define this constant
Definition of a System Call in the Kernel Land
• $T_SYSCALL is a pointer to “syscall” function in syscall.c:
syscalls[SYS_mkdir]
Definition of a System Call in the Kernel Land
• To make the call to syscalls[SYS_mkdir] working, we need two things:
•
define SYS_mkdir
in syscall.h
•
put the pointer to our system call in syscalls[SYS_mkdir] array element
in syscall.c
Definition of a System Call in the Kernel Land
•
•
•
•
So now syscalls[SYS_mkdir] points to sys_mkdir
We need to define sys_mkdir function in the kernel land
We put the function definition in “sysproc.c” file
Since we define sys_mkdir outside syscall.c, we need this line:
in syscall.c
Definition of a System Call in the Kernel Land
• sys_mkdir skeleton in the kernel land (sysproc.c):
Definition of a System Call in the Kernel Land
• What if we want to put the function definition in “proc.c” file because we
can access many variables and kernel functions in proc.c? [you need to put
your code there for your 3rd and 4th assignment ;-)]
• We define a function in proc.c and call it in the sysproc.c definition
Defining a “hello” System Call in the Kernel
Since hello is in a different file we need to mention this in “defs.h” header file
Defining a “hello” System Call in the Kernel
• Finally…
• We need to mention that “hello” is a library function
in user.h
Installing the xv6 kernel on Mac (Intel/M1/M2)
This document outlines the steps to install the MIT-version of the xv6 kernel on MacOS, particularly on the M1/M2 SoC. The following steps are also tested on MacBook with x86 (Intel chip).
1
xv6
1.1 Update the virtual/physical Linux OS
Log in to your (virtual or physical) Linux operating system (e.g., Ubuntu). Update and upgrade
packages.
1
2
> sudo apt update
> sudo apt upgrade -y
Update and upgrade packages (sudo not required by
default for OS running on Docker)
Listing 1.
1.2
Installing tools
Install the following tools.
1
2
3
4
5
6
7
8
9
10
11
> sudo apt install \
git \
wget \
curl \
vim \
qemu \
build – essential \
gdb – multiarch \
qemu -system -misc \
gcc -riscv64 -linux -gnu \ # or gcc -x86 -64- linux -gnu
binutils -riscv64 -linux -gnu # or binutils -x86 -64- linux -gnu
Listing 2. Installing essential tools
Installation of the last few tools may fail if attempting to install on incompatible architecture.
Figure out the appropriate version for your hardware architecture from Ubuntu Packages.
1.3
Download the appropriate xv6 version
MIT CS AI Lab’s public version of the xv6 kernel seems to work on x86 and x86-64 architectures
(i.e., 32- and 64-bit Intel-based x86 architecutres). The RISCV version seems to work particularly
for ARM-based architectures (Apple M1/M1 SoC, for example).
1
2
3
# for ARM architectures ( Apple M1/M2)
> git clone https :// github .com/mit -pdos/xv6 -riscv
> cd xv6 – riscv
4
1
# for Intel – based x86 architectures
> git clone https :// github .com/mit -pdos/xv6 – public
> cd xv6 – public
5
6
7
Listing 3. Download xv6
If needed, make the following changes in Makefile.
QEMU = qemu -system – riscv64 // or qemu -system – x86_64
1
Listing 4. Configure Makefile
1.4
Build the kernel
> make
> make qemu # or make qemu -nox
> ls # lists some commands upon successful installation
1
2
3
Listing 5. Build and open the kernel
Note
1. To compile your custom program files, include the filename in the variable list UPROGS in
Makefile
2. To include non-code files (like text files), include the filename in the fs.img recipe in
Makefile similar to README
3. xv6-riscv is organised slightly differently from xv6-public. For xv6-riscv, including header
files is nested into the folders kernel or user. For example:
1
2
3
# include ” kernel /types.h”
# include ” kernel /stat.h”
# include “user/user.h”
4
5
…
Listing 6. Including header files for xv6-riscv
It is recommended that you place your custom program files in the user folder.
Also, some minor syntax variations exist (e.g., , printf, exit, etc.) which become apparent upon compilation
2
Docker for OS virtualisation
Docker is an OS-virtualisation platform. If you wish to avoid installing other third-party virtual
machine clients, you may choose to use Docker instead. Docker is lightweight and works on UNIXbased operating systems, MacOS, and (I think) Windows, too. The following is purely CLI-based.
2.1 Installing Docker
Install Docker with Homebrew.
1
> brew install docker
Listing 7. Install Docker with Homebrew
2
After installing Docker and each time you wish to use it, make sure to start it (or set it to automatically restart every time the system is booted). Docker may be started either from the GUI or
through the CLI.
1
> open -a docker
Listing 8. Start Docker
2.2 Downloading Ubuntu
Download a copy (a.k.a. image) of Ubuntu from Docker Hub.
1
> docker pull ubuntu
Listing 9. Download Ubuntu from Docker Hub
Refer to Docker commands or cheat sheets to know about more Docker commands.
2.3 Running Ubuntu
An instance (a.k.a. container) of the Ubuntu image may be started with the following command:
1
> docker run -it -d –name ubuntu /bin/bash
Listing 10. Start an instance of Ubuntu. may be
any string to name the instance (e.g., OS6611Ubuntu)
2.4
Logging into the Ubuntu instance
The steps so far need be done only once or if setting up for the first time. To log in to the Ubuntu
instance each time, run the following command:
1
> docker start -i
Listing 11. Log in to Ubuntu. is the name of the
instance from the previous step
Upon successfully logging in, you should be placed in the root folder of the Ubuntu instance. To
verify, run any of the following commands:
1
2
3
> uname -a # OS/user/CPU architecture versions
> cat /etc/os – release # OS details
> lscpu # CPU details
Listing 12. Verifying login
If appropriate details are printed, the OS is installed, started, and logged into successfully.
3
8/26/23, 8:53 PM
Project 0: Preparing to work in xv6
Project 0: Preparing to work in xv6
Start Assignment
Due Thursday by 11:59pm
Points 0
Submitting a file upload
Available Aug 23 at 4pm – Aug 31 at 11:59pm
Introduction
NOTE: Late submissions will NOT be accepted for this project.
Project 0 will cover the following:
Instructions on how to install xv6.
Understanding xv6 basics: using it and understanding some of the codebase.
Selecting project groups.
Files to Edit and Submit: Two screenshots of the installation process and a file named
cop6611_project0.txt with your answers to xv6 basics and planned project 1 group members. The first
screenshot is of your terminal after successfully compiling the kernel (the first make call in the installation
instructions). The second screenshot is of your terminal after starting up xv6 and running the ls
command.
Academic Dishonesty: We will be checking your code against other submissions in the class for logical
redundancy. If you copy someone else’s code and submit it with minor changes, we will know. These
cheat detectors are quite hard to fool, so please don’t try. We trust you all to submit your own work
only; please don’t let us down. If you do, we will pursue the strongest consequences available to us.
Getting Help: You are not alone! If you find yourself stuck on something, contact the course staff for
help. Office hours are there for your support; please use them. If you can’t make our office hours, let us
know and we can talk over email or schedule a one-on-one meeting. We want these projects to be
rewarding and instructional, not frustrating and demoralizing. But, we don’t know when or how to help
unless you ask.
Additional Resource Files: xv6-Mac-1.pdf
(https://usflearn.instructure.com/courses/1836624/files/158392116?wrap=1)
(https://usflearn.instructure.com/courses/1836624/files/158392116/download?download_frd=1) and a
Windows implementation video provided below:
https://usflearn.instructure.com/courses/1836624/assignments/14703967
1/4
8/26/23, 8:53 PM
Project 0: Preparing to work in xv6
0:00 / 7:54
xv6 Installation
Here I lay out instructions for installing and running xv6 for Linux and Windows. The installation process
for MacOS is listed on Canvas under files –> Uploaded Media –> xv6-mac.pdf. If you spend more than
an hour trying to install xv6 with any method, I highly recommend installing VirtualBox and using a Linux
image (e.g., Ubuntu) instead.
First, I’ll cover the instructions for installing xv6 on Linux (specifically, Ubuntu) since it is the simplest.
Ubuntu Installation of xv6
First, we’ll install qemu , a hardware emulator so that we can run xv6 without exiting our current OS.
$ sudo apt-get update
$ sudo apt-get install qemu-system-x86
NOTE: In older resources, you’ll find a simpler command sudo apt install qemu . The qemu packages
have since been broken up to be system-specific to reduce download sizes.
Next, we’ll install xv6 directly from the github repository (Note: MIT CS AI Lab’s public version of the xv6
kernel seems to work on x86 and x86-64 architectures (i.e., 32- and 64-bit Intel-based x86
architecutres). The RISCV version seems to work particularly
for ARM-based architectures (Apple M1/M1 SoC).
$ sudo apt-get install git
$ git clone https://github.com/mit-pdos/xv6-public.git
$ cd xv6-public
$ make
(https://github.com/mit-pdos/xv6-public.git)
At this point, you can check whether xv6 successfully compiled the kernel by seeing if an executable of
the name kernel exists.
$ ls kernel
https://usflearn.instructure.com/courses/1836624/assignments/14703967
2/4
8/26/23, 8:53 PM
Project 0: Preparing to work in xv6
kernel
Take your first screenshot here.
Now we can compile and run the OS with the hardware emulator.
$ make qemu
At this point, you should see xv6 start up in a new window with some basic info and a basic shell. You
can try out some predefined shell programs, e.g., cat, echo, grep, etc. You can exit xv6 with Ctl+A, X.
You can also start it without the new window with
$ make qemu-nox
This means, to start xv6 without x-forwarding. If you don’t have the necessary display support, either
because you are running on Windows, or because you are ssh’d into a machine without x-forwarding,
this is the option that you will have to use.
Now start up xv6 using either method, run the ls command and take your second screenshot.
The following options allow you to start up xv6 with the GNU Project Debugger.
$ make qemu-gdb
$ make qemu-nox-gdb
At this point, you’ll probably want to start tracking this codebase under your own private github repository
so that you can use git to track your changes.
Windows Installation of xv6
On Windows, you will need to install the Windows Subsystem for Linux (WSL). You can simply look up
Ubuntu on the Microsoft Store to download it for free. Once this is installed, create an account and follow
the same steps as the Ubuntu installation. You will need to use make qemu-nox since WSL doesn’t handle
windowing appropriately for this.
xv6 Basics
To familiarize yourself with xv6, read through some of the existing xv6 code and explore the basic shell
that xv6 currently provides. Then answer the following questions in a file called cop6611_project0.txt .
1. How many files does the command stressfs create?
2. What does xv6 print if you run the command wc on itself? That is, the command wc wc .
3. Where are system call numbers defined and how many are there currently?
https://usflearn.instructure.com/courses/1836624/assignments/14703967
3/4
8/26/23, 8:53 PM
Project 0: Preparing to work in xv6
4. In the implementation of the fork system call, what is the local variable name for the pointer to the
process control block of the child processes? What about the PCB for the parent process?
Though not needed for this assignment, the xv6 book
(https://usflearn.instructure.com/courses/1836624/files/158249558?wrap=1)
(https://usflearn.instructure.com/courses/1836624/files/158249558/download?download_frd=1) will be a
valuable resource throughout the semester. NOTE: We are NOT using the updated RISC-V version of
xv6. Please be sure you are referring to the correct resources!
Project 1 groups
Add a list of your collaborating members for Project 1 at the end of cop6611_project0.txt .
Submission
Once you’ve completed all of the above steps, submit the two screenshots and the file
cop6611_project0.txt on Canvas.
https://usflearn.instructure.com/courses/1836624/assignments/14703967
4/4