You are on page 1of 41

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. This project made by me: Lê Anh Quân (ID: B2105684), 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.

Lecturer: Lam Nhut Khang


CT104H – Operating System

$sudo apt-get install -y gcc libncurses5-dev make wget

$sudo apt-get install -y gcc libssl-dev

Lecturer: Lam Nhut Khang


CT104H – Operating System

$sudo apt-get install bison

$sudo apt-get install flex

Lecturer: Lam Nhut Khang


CT104H – Operating System

2. Obtain the version of your current kernel, type:


# uname –r
you will have something like: 5.19.0-38-generic (Ubuntu)

3. Visit http://kernel.org and download the source code of your current running kernel. Then, download a new kernel
from https://www.kernel.org/ (e.g., 5.19.17) and extract the source:

# wget https://www.kernel.org/pub/linux/kernel/v5.x/linux-5.19.17.tar.xz

Lecturer: Lam Nhut Khang


CT104H – Operating System
# sudo tar -xvf linux-5.19.17.tar.xz -C/usr/src/

tar – tar used to store and extract files from a tape or disk archive
-x – extract files from an archive
-v – requested additional information about the process using the -verbose option when extracting archives
-f – to work with a specific archive file or device archive followed by the name of the archive
-C – to extract files to a particular directory followed by the directory path (in this case /usr/src/)

B. CONFIGURE YOUR NEW KERNEL


1. Make sure you are at ~/linux-5.19.17 such that the “linux-5.19.17” is the top directory of the kernel source.
Now we’ll change the directory to where the files are extracted
# cd /usr/src/linux-5.19.17/
2. Generate the configuration file
# make menuconfig
Do not change anything. Press ESC to save and exit the configuration menu. The configuration file will be generated
-When you run make menuconfig, you are presented with a menu-based interface that allows you to customize the
kernel configuration options according to your requirements. However, in this specific case, the instruction asks you
not to change anything in the configuration menu and simply exit the menu by pressing the ESC key. This is because
the configuration file is already preconfigured with the required options for building the kernel, and modifying these
options without proper knowledge can lead to errors or issues while compiling the kernel.

-After exiting the configuration menu, the generated configuration file is stored in the .config file in the kernel source
directory. This file contains all the configuration options selected or enabled by the user, as well as default options that
are selected by the kernel build system. This file is used by the kernel build system to compile the kernel with the
desired configuration options.

Lecturer: Lam Nhut Khang


CT104H – Operating System

-When I run make menuconfig it will show that error is “Your display is too small to run Menuconfig!”
=> Increase the size of terminal window by dragging the corners until it is large enough to display the entire menu
configuration interface. Change from 1792x1344(4:3) to 1920x1440(4:3) to fix it.

Lecturer: Lam Nhut Khang


CT104H – Operating System

Lecturer: Lam Nhut Khang


CT104H – Operating System

C. COMPILE THE KERNEL


1. At ~/linux-5.19.17, create a compressed kernel image
# make –j6
-The error <gelf.h>: No such file or directory usually occurs when the GNU ELF library headers are missing or not
properly installed on Linux system.
=> To fix this error you can install the required development package using this command.
# sudo apt-get install libelf-dev

-In your kernel configuration file you will find this line:

Lecturer: Lam Nhut Khang


CT104H – Operating System

CONFIG_SYSTEM_TRUSTED_KEYS="debian/canonical-certs.pem"
Change it to this:

CONFIG_SYSTEM_TRUSTED_KEYS=""

-Another key has been added to the default Canonical kernel configuration:

CONFIG_SYSTEM_REVOCATION_KEYS="debian/canonical-revoked-certs.pem"
So, it also needs to change to this:
CONFIG_SYSTEM_REVOCATION_KEYS=""
- It is necessary to make these changes because they ensure that the kernel compilation process runs smoothly without
encountering any errors or issues. The first change specifies not to include extra trusted X.509 keys into the kernel,
which can cause issues while verifying kernel modules. The second change specifies not to include any system
revocation keys.

Lecturer: Lam Nhut Khang


CT104H – Operating System

-The warning message indicates that the stack frame size of a particular function is larger than the recommended limit
of 1024 bytes. In this case, the function in question has a stack frame size of 1136 bytes, which exceeds this limit, and
hence the warning is generated. When you have made the changes with some commands I have mentioned above, it
will run normal and no longer have the error.

Lecturer: Lam Nhut Khang


CT104H – Operating System

-Compiling the kernel: The command make -j6 compiles the kernel with 6 threads using the make command. The -j6
option specifies that we want to use 6 threads to speed up the compilation process.

2. To compile kernel modules:


-Compiling kernel modules: The command make modules compiles kernel modules using the make command.

Lecturer: Lam Nhut Khang


CT104H – Operating System
# make modules

D. INSTALL THE KERNEL


1. Install kernel modules
-Installing kernel modules: The command make modules_install installs the compiled kernel modules using the make
command..
# make modules_install

2. Install the kernel

Lecturer: Lam Nhut Khang


CT104H – Operating System
-Installing the kernel: The command make install installs the compiled kernel using the make command. This installs
the kernel image, system map, and config files into the /boot directory, updates the bootloader configuration to include
the new kernel, and updates the initial ramdisk to include the new kernel modules
# make install

E. MODIFY GRUB CONFIGURATION FILE (GRUB CONFIGURATION FILE)


Change the grub configuration file
# vim /etc/default/grub
-Modifying the GRUB configuration file and changing the GRUB_DEFAULT and GRUB_TIMEOUT parameters is
important to customize the boot process of your Linux system according to your preferences.

Lecturer: Lam Nhut Khang


CT104H – Operating System

Make the following changes:


GRUB_DEFAULT=0
GRUB_TIMEOUT=25

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 the kernel version like so : 5.19.17

Lecturer: Lam Nhut Khang


CT104H – Operating System
PART 2 (1.5 points): ADD A NEW SYSTEM CALL INTO THE LINUX KERNEL

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. 

These are very good articles to start: 


- https://www.kernel.org/doc/html/v4.10/process/adding-syscalls.html
- https://brennan.io/2016/11/14/kernel-dev-ep3/ 
- https://medium.com/@ssreehari/implementing-a-system-call-in-linux-kernel-4-7-1-6f98250a8c38
- https://medium.com/anubhav-shrimal/adding-a-hello-world-system-call-to-linux-kernel-dad32875872
- https://linux.die.net/man/2/syscalls

-Go to the /usr/src/linux-5.19.17/ directory


# cd /usr/src/linux-5.19.17/
-Create a directory named hello/ and change the directory to hello/:
# mkdir hello
# cd hello
-Create a file hello.c using gedit editor
# gedit hello.c

-Create a “Makefile” in the hello directory:


# gedit Makefile
-And add the following line to it:
obj-y := hello.o
=>The purpose of this is to guarantee that the hello.c file is both compiled and incorporated into the kernel source
code.

Lecturer: Lam Nhut Khang


CT104H – Operating System

-Go back to the /usr/src/linux-5.19.17/ and open “Makefile”


# gedit Makefile
-When you search “core-y” in the document, the second instance of your search will yield this line and you will
add ‘hello/’ to the end of this line.
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ hello/
core-y += block/
 This is to inform the compiler that the source files for our new system call(sys_hello()) are located in the
hello directory

-
-Run the following commands in linux-5.19.17/ directory and you will get a file like the following
# cd arch/x86/entry/syscalls/
# gedit syscall_64.tbl

Lecturer: Lam Nhut Khang


CT104H – Operating System
-In the previous line, the number entry was 547, so I have written 548 here. Additionally, I have written 64 as it is
64-bit (It depending on your system configuration). Remember to note the system call number for reference. You
should write like this and save+exit after all.

-Go back to the /usr/src/linux-5.19.17/ and type the following commands:


# cd include/linux/
# gedit syscalls.h
-Add this line to the end of the document before #endif statement. Remember to save and exit
asmlinkage long sys_hello(void);
 This line snippet defines the function prototype for our system call. The “asmlinkage” keyword is used to
indicate that all parameters for the function will be available on the stack.

-Before starting to compile you need to install a few packages.


# sudo apt-get install gcc
# sudo apt-get install libncurses5-dev

Lecturer: Lam Nhut Khang


CT104H – Operating System

# sudo apt-get install bison


# sudo apt-get install flex

# sudo apt-get install libssl-dev


# sudo apt-get install libelf-dev

Lecturer: Lam Nhut Khang


CT104H – Operating System

# sudo apt-get update


# sudo apt-get upgrade

 You need to install these packages before compiling the kernel because they are required dependencies
for building the kernel. The kernel build process relies on a number of tools and libraries, including a C
compiler (gcc), libraries for handling terminal input (libncurses5-dev), a parser generator (bison), a
lexical analyzer generator (flex), libraries for handling SSL encryption (libssl-dev), and libraries for
handling object files (libelf-dev). Additionally, updating and upgrading your system ensures that you

Lecturer: Lam Nhut Khang


CT104H – Operating System
have the latest versions of all the necessary packages installed, which can help avoid compatibility issues
during the kernel build process.
-To configure your kernel use the following command in your linux-5.19.17/ directory
=> You need to follow these steps in order to configure and compile the Linux kernel for your system. The
"sudo make menuconfig" command allows you to customize the kernel configuration by selecting specific
options and settings.

-After configuring the kernel, the "sudo make -j6" command is used to compile the kernel. This command can
take a long time to complete, so you can use the "sudo make -j6" command to speed up the process by using
multiple cores on your system.

-Once the kernel has been compiled, you need to install and update it using the "sudo make modules_install
install" command

Lecturer: Lam Nhut Khang


CT104H – Operating System

-After rebooting you can verify the kernel version using the following command:
# reboot
# uname -r
-It will display the kernel version like: 5.19.17
-Go to your home directory and create a userspace.c file
# cd ~
# gedit userspace.c
-Write the following code in this file (remember the number of system call that I wrote in syscalls_64.tbl was
548).

Lecturer: Lam Nhut Khang


CT104H – Operating System

-Now compile and run the program:


# gcc userspace.c
# ./a.out
-If all the steps are done correctly you’ll get an output like below:
System call sys_hello returned 0
-To check the message of your kernel run the following command:
# sudo dmesg
This will display Hello! My name is Le Anh Quan B2105684 at the end of the kernel’s message.

Lecturer: Lam Nhut Khang


CT104H – Operating System
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

-Go to the /usr/src/linux-5.19.17/ directory


# cd /usr/src/linux-5.19.17/
-Create a directory named printself/ and change the directory to printself/:
# mkdir printself
# cd printself
-Create a file printself.c using gedit editor
# gedit printself.c

-The error message "Failed to execute child process “dbus-launch” (No such file or directory)" indicates that a program
or process attempted to execute the 'dbus-launch' command, but the command was not found on the system.
 The command may not be installed on your system. You may need to install it using your system's package
manager.
# sudo apt-get install -y dbus-x11

Lecturer: Lam Nhut Khang


CT104H – Operating System

Lecturer: Lam Nhut Khang


CT104H – Operating System
-Create a “Makefile” in the printself directory:
# gedit Makefile
-And add the following line to it:
obj-y := printself.o
=>The purpose of this is to guarantee that the printself.c file is both compiled and incorporated into the kernel
source code.

-Go back to the /usr/src/linux-5.19.17/ and open “Makefile”


# gedit Makefile
-When you search “core-y” in the document, the second instance of your search will yield this line and you will
add ‘printself/’ to the end of this line.
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ printself/
core-y += block/
 This is to inform the compiler that the source files for our new system call(sys_printself()) are located in
the printself directory

Lecturer: Lam Nhut Khang


CT104H – Operating System
-Run the following commands in linux-5.19.17/ directory and you will get a file like the following
# cd arch/x86/entry/syscalls/
# gedit syscall_64.tbl
-In the previous line, the number entry was 450, so I have written 451 here. Additionally, I have written 64 as it is
64-bit (It depending on your system configuration). Remember to note the system call number for reference. You
should write like this and save + exit after all.

-Go back to the /usr/src/linux-5.19.17/ and type the following commands:


Lecturer: Lam Nhut Khang
CT104H – Operating System
# cd include/linux/
# gedit syscalls.h
-Add this line to the end of the document before #endif statement. Remember to save and exit
asmlinkage long sys_printself(void);
 This line snippet defines the function prototype for our system call. The “asmlinkage” keyword is used to
indicate that all parameters for the function will be available on the stack.

-Before starting to compile you need to install a few packages.


# sudo apt-get install gcc
# sudo apt-get install libncurses5-dev

# sudo apt-get install bison


# sudo apt-get install flex
Lecturer: Lam Nhut Khang
CT104H – Operating System

# sudo apt-get install libssl-dev


# sudo apt-get install libelf-dev

# sudo apt-get update


# sudo apt-get upgrade

Lecturer: Lam Nhut Khang


CT104H – Operating System

 You need to install these packages before compiling the kernel because they are required dependencies
for building the kernel. The kernel build process relies on a number of tools and libraries, including a C
compiler (gcc), libraries for handling terminal input (libncurses5-dev), a parser generator (bison), a
lexical analyzer generator (flex), libraries for handling SSL encryption (libssl-dev), and libraries for
handling object files (libelf-dev). Additionally, updating and upgrading your system ensures that you
have the latest versions of all the necessary packages installed, which can help avoid compatibility issues
during the kernel build process.
-To configure your kernel use the following command in your linux-5.19.17/ directory
=> You need to follow these steps in order to configure and compile the Linux kernel for your system. The
"sudo make menuconfig" command allows you to customize the kernel configuration by selecting specific
options and settings. It is important to ensure that the file system menu includes the "ext4" option, which is a
widely used file system for Linux.

Lecturer: Lam Nhut Khang


CT104H – Operating System
-After configuring the kernel, the "sudo make -j6" command is used to compile the kernel. This command can
take a long time to complete, so you can use the "sudo make -j6" command to speed up the process by using
multiple cores on your system.

-Once the kernel has been compiled, you need to install and update it using the "sudo make modules_install
install" command

-After rebooting you can verify the kernel version using the following command:
# reboot
# uname -r
-It will display the kernel version like: 5.19.17

-Go to your home directory and create a test.c file


# cd ~
# gedit test.c

Lecturer: Lam Nhut Khang


CT104H – Operating System
-Write the following code in this file (remember the number of system call that I wrote in syscalls_64.tbl was
451).

-Now compile and run the program:


# gcc test.c
# ./a.out
-If all the steps are done correctly you’ll get an output like below:
System call sys_printself returned 1

Lecturer: Lam Nhut Khang


CT104H – Operating System

-To check the message of your kernel run the following command:
# sudo dmesg
 This will display like this at the end of the kernel’s message.
My current PID: 2009
My programme name: a.out
My running state: runnable
Cleaning up.

Lecturer: Lam Nhut Khang


CT104H – Operating System

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).

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 ****************** 
………..

-Go to the /usr/src/linux-5.19.17/ directory


# cd /usr/src/linux-5.19.17/
-Create a file B2105684cpusheduling.c using gedit editor
# gedit B2105684cpuscheduling.c

Lecturer: Lam Nhut Khang


CT104H – Operating System

Lecturer: Lam Nhut Khang


CT104H – Operating System

Lecturer: Lam Nhut Khang


CT104H – Operating System

Lecturer: Lam Nhut Khang


CT104H – Operating System

Lecturer: Lam Nhut Khang


CT104H – Operating System

-Now compile and run the program:


# gcc B2105684cpuscheduling.c
# ./a.out
-If all the steps are done correctly you’ll get an output like below:

Lecturer: Lam Nhut Khang


CT104H – Operating System

Lecturer: Lam Nhut Khang


CT104H – Operating System
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.
-Go to the home directory
# cd ~
-Create a file process.c using gedit editor
# gedit process.c

-Now compile and run the program:


# gcc process.c
# ./a.out
-If all the steps are done correctly you’ll get an output like below:

Lecturer: Lam Nhut Khang


CT104H – Operating System

----------------------THE END ----------------------

Lecturer: Lam Nhut Khang

You might also like