代做2025/25 COMP2860 Weeks 1–6: Operating Systems Laboratory Manual代做Java程序
- 首页 >> Algorithm 算法2025/25 COMP2860 Weeks 1–6: Operating Systems Laboratory Manual
IMPORTANT: Portfolio for this part of the module includes 3 × MCQ on Gradescope and one C programming exercise described in this lab manual
Chapter 1
Introduction
Welcome to 2025/26 COMP2860 Weeks 1–6: Operating Systems. This manual provides all of the practical activities for the first six weeks of the module. Most of the exercises are formative assessment, which do not contribute to your portfolio. The weekly exercises are designed to help prepare for the main part, summative assessment, which is required to pass the portfolio of COMP2860. You cannot complete the summative assessment without undertaking weekly formative assessment exercises because they act as preparation. Lecture material is assessed by three sets of Multiple Choice Questions (MCQs) on Gradecope.
The coursework specification and MCQs are released on week 1 and you should aim to complete them as soon as you can—the coursework is not easy and leaving it until the end, where other assignments will accumulate, is not a good studying practice.
The coursework is designed to be a highly independent piece of work, requiring you to search for information and learn the required new skills yourself during the lab by using text books, linux man pages, and the web; the laboratory manual provides many pointers to help you along this journey but it does not provide a complete step-by-step guide for achieving everything—getting used to searching for information on how to solve problems and answer your own questions will be an important part. If you are stuck, staff in the laboratory can point you in the right direction to help you progress and they have the model solutions which they can discuss with you after the dedicated assignment weeks. We will also be reviewing the solutions during the lectures.
Use of large-language models to generate solutions and submitting them as your own creative work is not acceptable and will lead to zero marks and may result in the academic malpractice violation.
We will be using Bash scripting, C programming, some git version control, and a small operating system designed for learning, called xv6. For Bash scripting and git we recom- mend using various materials online, as well as the man command in the terminal. For C programming we recommend The C Programming Language (2nd Edition) by Kernighan and Ritchie, 1988. Table 1.1 provides the timeline of this part of COMP2860.
1.1 Schedule and deadlines
Table 1.1 summarises all the activities in the first six weeks of COMP2860. Please note the following deadlines for your portfolio.
• 40 Multiple-Choice Question (MCQ) set 1, on Gradescope, deadline 9th October 2pm
• MCQ set 2, on Gradescope, deadline 23rd October 2pm
• MCQ set 3, on Gradescope, deadline 6th November 2pm
• Coursework specified in Chapter 7 of this lab manual. Submission on Gradescope. Deadline 13th November 2pm. Note that in order to be able to effectively do this coursework you a required to do weekly exercises in outlined in this lab manual—they do not contribute to your portfolio, but skipping these will make it very hard to pass.
Please note that all portfolio assignments in Semester 1 have to be submitted by 11th of December, 2pm.
1.2 Acknowledgements
The exercises were created by utilizing previous years’ COMP2211 Operating Systems mod- ule material: we thank Martin Callaghan and Sam Wilson.
Table 1.1: COMP2860 Operating Systems weekly plan. Each lecture and lab is 2 hours in length. Assessment column shows recommended weeks for your submission—do not delay until the end of the term.
Chapter 2
Setting up your Linux environment
First, go to https://classroom.github.com/a/u-zd6hcv to accept the assignment, which will clone a repository containing the code that you will be working on in the first six weeks of this module. The repository will be named comp2860-semester-1-week-1-
The next step is to determine a good way(s) for you to work on a Linux environment for this part of the module. There are at least four options:
1. Use a Linux virtual machine, Windows Subsystem for Linux (WSL) or a machine with Linux installed.
2. Use the machines in the room 2.05 on Level 2 of the William Henry Bragg building.
3. Connect to https://feng-linux.leeds.ac.uk.
4. Use GitHub codespaces.
Option 1 would require you to support your own infrastructure. We and IT cannot support the installation of xv6 and the correct cross-compilation tools on various types of personal devices and operating systems—it is not feasible to anticipate all potential problems and investigate solutions.
Option 2 is a good option when you are in 2.05. The lab machines are based on a Linux distribution and contain all the required software for working with xv6.
Option 3: if you have access to a computer that contains only Windows, then one way is to use https://feng-linux.leeds.ac.uk on your browser which allows you to remotely log in into the University’s Linux environment. Use your standard University username and password. In some cases you will not be able to log in because your account is not set up yet—for example, if you are an exchange student. If you are having problems logging in, please file an IT ticket at https://it.leeds.ac.uk/it/ by logging in and choosing “Something broken?”.
Options 1-3 require you to clone the repository that was created as part of you accepting the GitHub classroom assignment.
Option 4: another option is to access the Linux environment through GitHub codespaces. Go back to the base of your new repository, press “Code”, “Codespaces” and “+ (Create codespace on main)”. It will take about 2 minutes to set up, but this should allow you to access a Linux environment that has xv6 and the cross-compilation setup ready. It is worth to note that the storage on GitHub codespaces is not permanent, so any changes you make to your files have to be committed regularly and pushed to your repository (ideally, the same should be done with options 1-3 as well). If you leave your codespaces active and not use it for a while it will stop automatically and eventually disappear, not recording any changes to your files. You can push your files from within the codespace to the repository using the usual git add, git commit -m, and git push terminal commands—look them up online. The commands will be covered in your level 2 personal tutorial on Week 1.
Chapter 3
Week 1: Command line and C programming
The purpose of this lab is to re-introduce you to the Linux command line and the Unix shell scripting language, and also recap the key concepts of programming in C, which you have covered last year but may have forgot some of the detail over the summer. If you feel confident with these skills then proceed to the next chapter.
There are many Unix shells that act as command-line interpreters, with many similar but also different features: csh, sh, bash, zsh and many others—look into the history online.
Let us check the shell active on your terminal. Open up the terminal on your Linux machine (or GitHub codespaces) and run
$ echo "$SHELL"
or
$ echo "$0"
which should show which shell you are running (most likely bin/bash)
The following set of exercises equip you (or provide a refresh) with foundational Unix shell skills that will be useful in this module. Ideally you should undertake all of the tasks inside the shell—if you find yourself steering away from the shell and doing anything with the mouse, ask yourself, and the lab demonstrators, how you could achieve that task by using the shell.
Please type the commands into the terminal directly. Copying-pasting them from this PDF document might not always work.
3.1 Linux command line
First, get hold of the sample data we will be using today. Use a terminal on a Linux machine to run the following commands when you are inside the assignment repository (on GitHub codespaces or on a machine where you have cloned the assignment repository). Type them in rather than copy-paste if you encounter problems.
$ ls
coursework_templates README. md shell_training xv6 -riscv
This command lists the contents of the folder in which the assignment repostiry has been cloned. It tells us that it contains three directories, cousework_template, shell_training, xv6-risc, and one file README. md. We will not need to use the file README. md—it is a file automatically created by GitHub.
Explore the contents of shell_training/ by using the cd and ls commands, and create a new directory work inside shell_training:
$ cd shell_training $ ls
data IOM -animals $ mkdir work
$ ls
data IOM-animals work
Here the first command goes inside the directory shell_training that is in your current directory (the directory where the repository of the assignment). The second command lists its current contents, the third creates a new directory called work and the last lists the new contents showing that work has appeared inside shell_directory.
Finally, come back to the previous directory
$ cd . .
$ ls
coursework_templates README. md shell_training xv6 -riscv
Navigating the directory tree: run pwd to determine where you are. Run cd to go to the home folder or cd
Practice going back and forth through the directories with cd, ls and pwd. These com- mands cannot break your environment or damage the directories and can be used at will to develop an understanding of the file structure at hand.c
3.1.1 Exercise 1: creating and moving files
Create the following directory tree in your shell_training/work or another preferred work directory, using mkdir:
temp
temp/input_data/ temp/results/
temp/program/
• Create a file shell_training/work/input. txt with a text editor, such as emacs, nano or vim, and populate it with some text. You may do it inside the terminal rather than in the Graphical User Interface (GUI) of your chosen text editor. If you have not used emacs, nano or vim before, find a tutorial online and spend some time on understanding how to create and save files with those text editors, and ask the lab demonstrators to help you. Then, in one command, move the file to temp/input_data and rename it to control01 . txt (actions of moving and renaming should be performed in the same command). If you do not know what command can be used for moving files, investigate this on the internet or use any of your preferred textbook on bash scripting.
• Create this directory tree in one line only: temp/experiment/results/report. If you do not know how, investigate the options you can provide to mkdir.
• Delete the temp/ directory and all of its contents with one single command. Be careful not to delete shell_training or shell_training/work.
Write down the commands you have discovered, for your reference.
Folder or directory: you may notice we that we use a word directory instead of a term folder, which you may have met more often before. If curious, investigate what the difference between these two terms is and find out why do people use folder quite often.
3.1.2 Exercise 2: manipulating standard inputPlease meet four new commands:
• The cat command concatenates and prints files.
• The sort command will sort lines alphabetically or numerically.
• The cut command splits lines of text based on a given character e.g. cut -d ’,’ -f 2 will split lines around the comma and give you the second part.
• The > command redirects the standard output to a file instead of the terminal display.
Use man command to get more information on other commands. For example, man sort will provide detailed information on sort. Exit man with q.
Go back to the directory shell_training/. Use cat, cut, sort, and redirection com- mands to cut out the English names and print out the Latin names alphabetical order, from IOM-animals/birds . txt. Save the output to a new file work/sorted-birds-latin . txt and then display its contents in the terminal. This should be achieved by running three separated commands and perhaps using a temporary file in between to store intermediate results. The first line of the output in the terminal should be “accipiter nisus”.
3.1.3 Exercise 3: sorting
List all the animals in the files in IOM-animals/ alphabetically and print out the 50th item in that list in the terminal. To refer to all the files you can use a wildcard, IOM-animals/* . txt which matches all files inside IOM-animals that end with . txt. The commands head and tail can be used in conjunction with sort for achieving this. The output should be "coot, fulica atra".
3.1.4 Exercise 4: counting lines
shell-training/data/ contains 300 data files, each of which should contain 100 values. One of these files is missing some data. Use a series of commands connected by pipes to identify the file with missing data (hint wc -w will tell you the number of values in a file, sort -n will sort numerically). This should be done with a single line command. If you have not pipes before, investigate them online and ask lab demonstrators to check your understanding.
3.1.5 Exercise 5: using sed and grep
Study the following Linux commands: sed and grep.
• Use the sed command to delete the first character and last character in each line of IOM-animals/amphibians. txt and display the output in the terminal (do not modify the original file).
• Use the grep and wc commands to find how many lines of a file contain a word ,bat, in IOM-animals/mammals . txt. The filename and the word to look for are provided as inputs to grep.
3.1.6 Exercise 6: comparing bash and C run times
Write a shell script program and a C program to display “HELLO WORLD”. Compare the running time of the programs using time command. What do you observe?
3.1.7 Exercise 7: creating aliases
This exercise is for those who are working on the linux machines in Bragg 2.05 or feng-linux.
As a reminder, you can get various software packages on the School’s linux machines by using a command module. Start by having a look at what modules are available by running
$ module avail
and load one with
$ module load
Create an alias command called, for example, load-my-modules that runs a command or a script to load all the required modules in your Linux environment. Set it up so that this command is run automatically every time you log in. If you don’t need any modules yet, start by loading the modules git and gcc and come back to update your alias in a few weeks when you start needing to load more modules.
Completion: most shells support automatic completion of partially written commands. Type load-m and press TAB button to complete the command automatically to load-my-modules.
3.2 C programming
3.2.1 Exercise 8: terminating strings
Write a C program that does the following:
1. Continuously waits for the standard input terminated by an end-of-file (EOF) character (pressing Ctrl+D twice). If Enter key is pressed, a new line character ‘\n’ is added, and the program should continue waiting for EOF instead of starting processing the standard input at the occurrence of ‘\n’ .
2. Once EOF is met, the program should take the data and split the character stream into separate strings. The splitting should be performed by adding a null character ‘\0’ at the end of each string inside the original data (not making copies or moving data between different memory locations).
3. Each string should be saved into an array of pointers by storing the address of the first character.
4. Once the standard input is split up and the array is produced, use printf() in a loop to print each string on a new line.
5. Then continue waiting for the next standard input stream terminated by an EOF.
6. Do not use large-language models to produce the whole program. If you wish, ask the large-language models to help you understand different concepts in C that would allow you to write this program yourself.
Here is some starting code, which you will find in the assignment repository, under C_training/c_tutorial . c:
1 #include
2
3 int main(void) {
4 // Define an array of 100 characters.
5 char buffer[100];
6 // Define a pointer to a chracter and point it
7 // to the first character in the buffer.
8 char* current_char = buffer;
9 // Define an array of 20 points to characters and set
10 // them to NULL .
11 char* words[20] = {NULL};
12 // Define some counters and flags.
13 int i = 0;
14 int prev_char_not_ordinary = 1;
15
16 // Read a new character from stdin until EOF is detected.
17 while ((*current_char = getchar()) != EOF) {
18 // If the current character is a space, a new line,
19 // or a tab, coming after an ordinary character
20 // terminate the string by placing ’\0’ in the current
21 // location of buffer.
22
23 // PLACE YOUR CODE HERE
24
25 // If the current character is an ordinary character
26 // after a special character save the pointer to it in
27 // the words[] array .
28 else if (prev_char_not_ordinary) {
29 // PLACE YOUR CODE HERE
30 }
31 // Move the pointer in buff by one place.
32 // PLACE YOUR CODE HERE
33 }
34 *current_char = ’\0’;
35 printf("\n");
36
37 // Print out all the detected words in the stdin.
38 for (int i = 0; i < 20; i++)
39 if (words[i] != NULL)
40 printf("%s \n", words[i]);
41 return 0;
42 }
Let,s consider some of the lines present in this starting code:
• Line 5 declares an array of 100 characters where the input from the standard input will be stored in.
• Line 8 declared a pointer to a char type and assigns to it the pointer to the first character in the buffer array.
• Line 11 declares an array of pointers. Each of the pointers in words will be pointing to different characters in buffer.
• Line 14 declares a flag that will be useful to detect first characters of each string in the buffer. It is set to 1 at start, indicating that the previous character was not an ordinary character (a letter, a number, or a symbol).
• Line 17 reads in a character from the standard input into the location of memory pointed to by currect_char pointer. Notice the asterisk symbol at the front, which is used to dereference the pointer to get to the contents in memory that the pointer is pointing to.
• Line 34 replaces the EOF character in buffer by a ‘\0, character to terminate the last string in the buffer.
• Line 38 prints out the strings in words array. Each entry in that array is a pointer that is pointing to a memory location containing the first letter of the string. C follows that pointer character by character until a ‘\0, is found marking the termination of the string, so it known how many characters to print out.
Fill in the rest of the code in the marked locations in order to terminate each string in the buffer by a ‘\0, and to store pointers to first characters of each string in words array of pointers. The template is missing approximately 10 lines of code compared with the solution that the lab demonstrators can discuss with you.
Compile and run the code:
$ gcc c_tutorial.c -o c_tutorial
% ./c_tutorial
word1 word2 word3
word4 word5
^D
word1
word2
word3
word4
word5
This type of parsing of the standard input to identify distinct strings and store them in an array will be an essential part of the portfolio exercise where user commands will need to be read in from the standard input and ran.
3.3 Reading and thinking activities
3.3.1 Exercise 9: the history of Unix
Introducing Unix: https://www.youtube.com/watch?v=tc4ROCJYbm0. This is a 27.5 minute video that sets the scene for the work we’ll be doing this semester. We will be studying xv6 (a simplified version of Unix, version 6) designed by MIT faculty. This video will help you understand some of the challenges—despite being some decades old it is all still very relevant.
Also see diagram at https://en.wikipedia.org/wiki/History_of_Unix#/media/File: Unix_history-simple.svg.
3.3.2 Exercise 10: xv6 book
Download the xv6 book
https://pdos.csail.mit.edu/6.828/2025/xv6/book-riscv-rev5.pdf
and read Chapter 1. The book is very technical and most rewarding after multiple reads. The best way is to read the chapters and mark tricky parts for yourself rather than get stuck on parts trying to understand them. After working with xv6 on some later exercises, come back to the early chapters again and check whether the material is easier to understand.
The material in this book is not examinable but it will indirectly help you in under- standing the examinable material we will be covering in the lectures; each is encouraged to make a personal plan on how much time to spend on understanding the book and the source code of the xv6. A lot of the material in it will help with the laboratory exercises and the summative assessment.
