CT104H – Operating System
CAN THO UNIVERSITY
COLLEGE OF INFORMATION AND COMMUNICATION TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY
OPERATING SYSTEMS (CT104H)
PROJECT
Declaration of own work
I, TRÁT LÂM TRƯỜNG - B2111965, certify that this assignment is my own work, is not copied from any
other person's work.
Virtual machine: Ubuntu VM
Submission:
- A report describes clearly how did you solve problems
- All programs (in both kernel-level codes and user-level codes for testing) with comments (DON’T
ZIP the files)
PART 1 (2.0 points): BUILD THE LINUX KERNEL
*** Note: At the first step, it will be better if you install an old version of OS kernel. Then, the process
of building a new Linux kernel will be clear
You need to be a root user (root user)
$su -
A. GET THE LINUX KERNEL CODE
1. Download and install development tools on your system.
$sudo apt-get install -y gcc libncurses5-dev make wget
Lecturer: Lam Nhut Khang
1
CT104H – Operating System
$sudo apt-get install -y gcc libssl-dev
$sudo apt-get install bison
Lecturer: Lam Nhut Khang
2
CT104H – Operating System
$sudo apt-get install flex
2. Obtain the version of your current kernel, type:
# uname -r
you will have something like: 5.15.0-67-generic (Ubuntu)
3. Visit http://kernel.org and download the source code of your current running kernel.
# wget http://www.kernel.org/pub/linux/kernel/v5.x/linux-5.15.tar.gz
# tar xvzf linux-5.15.tar.gz
Download a new kernel from https://www.kernel.org/ (e.g., 5.19) and extract the source:
Lecturer: Lam Nhut Khang
3
CT104H – Operating System
B. CONFIGURE YOUR NEW KERNEL
1. Make sure you are at ~/linux-5.19 such that the “linux-5.19” is the top directory of the kernel source.
#cd ~/linux-5.19/
2. Generate the configuration file
# make menuconfig
Lecturer: Lam Nhut Khang
4
CT104H – Operating System
Do not change anything. Press ESC to save and exit the configuration menu. The configuration file will be
generated.
C. COMPILE THE KERNEL
1. At ~/linux-5.19, create a compressed kernel image
# make –j4 (Allocate 4 CPUs for VM UbuntuB2111965)
Lecturer: Lam Nhut Khang
5
CT104H – Operating System
Getting an fatal error while running make -j4:
Lecturer: Lam Nhut Khang
6
CT104H – Operating System
Troubleshooting: Run command: # sudo apt-get install libelf-dev to install libelf-dev package.
Run command # make -j4 again:
Getting the error No rule to make target: 'debian/canonical-certs.pem', needed by 'certs/x509_certificate_list'.
Stop. when running command #make -j4
Lecturer: Lam Nhut Khang
7
CT104H – Operating System
The fix: use nano .config, Ctrl+W to find the lines with 'debian/canonical-certs.pem' and delete them, then
use Ctrl+O to save and Ctrl+E to exit.
Run the command # make -j4 again
Lecturer: Lam Nhut Khang
8
CT104H – Operating System
Getting the 'Failed to generate BTF for vmlinux' error
Fix: Use command # sudo apt install dwarves to install dwarves package.
Run the command # make -j4 again
Lecturer: Lam Nhut Khang
9
CT104H – Operating System
Error: /bin/sh: 1: zstd: not found
Use command # sudo apt install zstd to install zstd package.
Lecturer: Lam Nhut Khang
10
CT104H – Operating System
Run the command # make -j4 again
2. To compile kernel modules:
Lecturer: Lam Nhut Khang
11
CT104H – Operating System
Run # make and # make bzImage commands before running # make_modules to avoid unpredictable errors.
D. INSTALL THE KERNEL
1. Install kernel modules:
# make modules_install
2. Install the kernel
# make install
Lecturer: Lam Nhut Khang
12
CT104H – Operating System
E. MODIFY GRUB CONFIGURATION FILE (GRUB CONFIGURATION FILE)
Make the following changes:
GRUB_DEFAULT=0
GRUB_TIMEOUT=25
Use the command # gedit /etc/default/grub to change the configuration file
Lecturer: Lam Nhut Khang
13
CT104H – Operating System
Use the command # update-grub to update the modifications
Use # vim /etc/default/grub to check for changes
Running vim command, I encountered an error:
Fix: Use apt install vim
F. REBOOT VM
1. Reboot to the new kernel
# reboot
2. After boot, check if you have the new kernel:
# uname -r
You will see something like: 5.19.0
PART 2 (1.5 points): ADD A NEW SYSTEM CALL INTO THE LINUX KERNEL
Lecturer: Lam Nhut Khang
14
CT104H – Operating System
We add a simple system call helloworld to the Linux kernel. The system call prints out a “Hello! My name
is XXX” message to the syslog (XXX is your student name and your student ID). You need to implement the
system call in the kernel and write a program at the user-level to test your created system call.
I – PREPARATION:
1. # sudo -i
2. Update my OS: # apt update && apt upgrade
3. Download essential packages:
$sudo apt-get install -y gcc libncurses5-dev make wget
$sudo apt-get install -y gcc libssl-dev
$sudo apt-get install bison
$sudo apt-get install flex
$sudo apt-get install libelf-dev
$sudo apt-get install dwarves
$sudo apt-get install zstd
$sudo apt-get install dkms
$sudo apt-get install vim
4. Clean up installed packages: # sudo apt clean && sudo apt autoremove
5. See current kernel version: # uname -r
You will see something like: 5.19.0-69-generic
6. Download kernel source code with higher version than current kernel:
# wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.19.tar.gz
7. Extract the source code of the downloaded kernel: # tar xvzf linux-5.19.tar.gz
8. Reboot my computer.
II – CREATION:
1. Move to directore linux-5.19: # cd ~/linux-5.19/
2. Create a directory named hello and change the directory to hello:
Lecturer: Lam Nhut Khang
15
CT104H – Operating System
# mkdir hello
# cd hello
3. Create a new file hello.c and write the following code in the editor:
# nano hello.c
Save (Ctrl+O) and Exit (Ctrl+X) .
4. Create Makefile in directory hello:
Write the code as shown below
# nano Makefile
obj-y := hello.o
5. Add hello directory to Makefile
Go back to ~/linux-5.19/ and open Makefile
Find to line
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/
core-$(CONFIG_BLOCK) += block/
Then add the hello directory at the end
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/
core-$(CONFIG_BLOCK) += block/ hello/
Lecturer: Lam Nhut Khang
16
CT104H – Operating System
Save and exit the editor.
6. Add the system call (sys_hello()) to the kernel’s system call table.
Open the table with the following command:
cd arch/x86/entry/syscalls/
nano syscall_64.tbl
Add code:
451 common hello sys_hello
Lecturer: Lam Nhut Khang
17
CT104H – Operating System
Save and exit the editor.
7. At ~/linux-5.19/ add a system call (sys_hello()) to the system call header file.
# cd include/linux/
# nano syscalls.h
8. Scroll to the bottom of the page and add a line of code above the #endif line:
asmlinkage long sys_hello(void);
III – INSTALLATION
Create configuration file. Make sure the current path is at ~/linux-5.19/
# make menuconfig
Lecturer: Lam Nhut Khang
18
CT104H – Operating System
# make -j4
Lecturer: Lam Nhut Khang
19
CT104H – Operating System
# make modules
Lecturer: Lam Nhut Khang
20
CT104H – Operating System
# make modules_install
# make install
# update-grub
Lecturer: Lam Nhut Khang
21
CT104H – Operating System
Reboot my computer
IV - RESULT
1. Return to root directory
$ cd ~
$ nano report.c
2. Write some code in this file like this:
3. Compile and run the program:
$ gcc -o report report.c (Switch to machine language)
$ ./report
Lecturer: Lam Nhut Khang
22
CT104H – Operating System
4. Run the command $ dmesg and check the last line of the output:
PART 3 (1.5 points): PRINT OUT THE CALLING PROCESS’S INFORMATION
Implement a print_self system call such that this system call will identify the calling process at the user-level
and print out the Process id, running state, and program name.
HINT: https://linuxgazette.net/133/saha.html
I – PREPARATION:
1. # sudo -i
2. Update my OS: # apt update && apt upgrade
3. Download essential packages:
$sudo apt-get install -y gcc libncurses5-dev make wget
$sudo apt-get install -y gcc libssl-dev
$sudo apt-get install bison
$sudo apt-get install flex
$sudo apt-get install libelf-dev
$sudo apt-get install dwarves
$sudo apt-get install zstd
$sudo apt-get install dkms
$sudo apt-get install vim
4. Clean up installed packages: # sudo apt clean && sudo apt autoremove
5. See current kernel version: # uname -r
You will see something like: 5.19.0-69-generic
6. Download kernel source code with higher version than current kernel:
# wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.19.tar.gz
7. Extract the source code of the downloaded kernel: # tar xvzf linux-5.19.tar.gz
Lecturer: Lam Nhut Khang
23
CT104H – Operating System
II
II– CREATION:
8. Move to directore linux-5.19: # cd ~/linux-5.19/
9. Create a directory named print_self and change the directory to print_self:
# mkdir print_self
# cd print_self
10. Create a new file print_self.c and write the following code in the editor:
# nano print_self.c
Save (Ctrl+O) and Exit (Ctrl+X) .
9. Create Makefile in directory print_self:
Write the code as shown below
# nano Makefile
obj-y := print_self.o
Lecturer: Lam Nhut Khang
24
CT104H – Operating System
10. Add print_self directory to Makefile
Go back to ~/linux-5.19/ and open Makefile
Find to line
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/
core-$(CONFIG_BLOCK) += block/
Then add the print_self directory at the end
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/
core-$(CONFIG_BLOCK) += block/ print_self/
Save and exit the editor.
11. Add the system call (sys_hello()) to the kernel’s system call table.
Open the table with the following command:
cd arch/x86/entry/syscalls/
nano syscall_64.tbl
Add code:
451 common print_self sys_print_self
Lecturer: Lam Nhut Khang
25
CT104H – Operating System
Save and exit the editor.
12. At ~/linux-5.19/ add a system call (sys_print_self()) to the system call header file.
# cd include/linux/
# nano syscalls.h
13. Scroll to the bottom of the page and add a line of code above the #endif line:
asmlinkage long sys_print_self(void);
Lecturer: Lam Nhut Khang
26
CT104H – Operating System
III – INSTALLATION
Create configuration file. Make sure the current path is at ~/linux-5.19/
# make menuconfig
Lecturer: Lam Nhut Khang
27
CT104H – Operating System
# make -j4
Lecturer: Lam Nhut Khang
28
CT104H – Operating System
# make modules
Lecturer: Lam Nhut Khang
29
CT104H – Operating System
# make modules_install
# make install
# update-grub
Lecturer: Lam Nhut Khang
30
CT104H – Operating System
Reboot my computer
IV - RESULT
1. See current kernel version: # uname -r
$ gedit Part5.c
Write some code in this file like this:
Save and exit.
$ gcc -o Part5 Part5.c (Switch to machine language)
./Part5
Lecturer: Lam Nhut Khang
31
CT104H – Operating System
2. Run the command $ dmesg and check the last line of the output:
PART 4 (3.0 points): CPU SCHEDULING
Write a program called IDcpuscheduling.c (ID is your student ID) to implement the CPU scheduling
algorithms: FCFS, SJF-preemptive and Round Robin. The program IDcpuscheduling.c will take the
parameters of n processes. Each scheduler will create a Gantt chart showing the states of the processes in a
string format, where R denotes the running state and W denotes the waiting state. Finally, the program will
calculate the average values of the waiting time, response time, and turnaround time of each scheduler.
Problem description: the program will take the number of processes n. Then, the program will allow users
to input (n x 2)+1 parameters:
q x1 y1 x2 y2 …. xi yi …xn yn
where q is the quantum value of the Round Robin algorithm, xi and yi are the arrival time and burst time of
the process i.
Output format: For each scheduler, the program will have the following lines:
- A line “*************** XXX SCHEDULER *****************” to separate schedulers (XXX
is the name of the scheduler).
- Each process will have a line of states. For example: RRRRRWWRRRR
- The last line of each scheduler presents the average values of the waiting time (AVGW), respond time
(AVGR) and turnaround time (AVGT).
Lecturer: Lam Nhut Khang
32
CT104H – Operating System
Example: the number of processes provided by a user is n=3. Then, the user provides parameters of
processes as 1 0 2 1 3 2 2. The output of the program will be as following:
************* FCFS SCHEDULER *******************
RR------
-WRRR--
--WWWRR
AVGW= 1.33 AVGR=1.33 AVGT=3.66
************** SJF PREEMPTIVE SCHEDULER ******************
………..
1. Create directory TruongB2111965_1: mkdir TruongB2111965_1
2. Move to directory TruongB2111965_1: cd TruongB2111965_1
3. Compile and run the program:
nano B2111965cpuscheduling.c
Write code like this:
Lecturer: Lam Nhut Khang
33
CT104H – Operating System
Lecturer: Lam Nhut Khang
34
CT104H – Operating System
Lecturer: Lam Nhut Khang
35
CT104H – Operating System
Lecturer: Lam Nhut Khang
36
CT104H – Operating System
Lecturer: Lam Nhut Khang
37
CT104H – Operating System
4. gcc -o B2111965cpuscheduling B2111965cpuscheduling.c
./B2111965cpuscheduling
PART 5 (2.0 point): PROCESS
Write a program in C to create a parent process P1. The parent process P1 forks a child (named P2) and waits
for the child to complete the program multiplicationtable. The child process prints its pid, executes the
multiplicationtable program and then exits. The multiplicationtable gets an argument arg and prints the arg
times table. When P2 is finished, P1 prints the information of P2 including pid and the exit state, and then
exits.
Lecturer: Lam Nhut Khang
38
CT104H – Operating System
1. Create directory TruongB2111965_1: mkdir TruongB2111965
2. Move to directory TruongB2111965_1: cd TruongB2111965
3. gedit TruongB2111965.c
Write code like this:
4. Compile and run the program:
gcc -o TruongB2111965 TruongB2111965.c
./TruongB2111965
----------------------THE END ----------------------
Lecturer: Lam Nhut Khang
39
CT104H – Operating System
Lecturer: Lam Nhut Khang
40