Description
-
Write modules that implement the following requirements
-
Accepts three parameters:
operand1
type int,operand2
type int array,operator
type charp (string). -
Create a proc file
/proc/<
你的学号
>/calc
. -
If
operator
is add, thenoperand2
each element of is addedoperand1
to get the result array; ifoerator
is mul, thenoperand2
each element of is multipliedoperand1
to get the result array. -
When reading a proc file, output an array of results, each element separated by a comma.
-
When the user writes a number to the proc file, the number is
operand1
recalculated . -
An example is as follows:
-
# sudo insmod calc.ko operand1=2 operand2=1,2,3,4,5 operator=add
# cat /proc/<ID>/calc
3,4,5,6,7
# echo 3 > /proc/<ID>/calc
# cat /proc/<ID>/calc
4,5,6,7,8
# sudo rmmod calc
# sudo insmod calc.ko operand1=2 operand2=1,2,3,4,5 operator=mul
# cat /proc/<ID>/calc
2,4,6,8,10
# echo 3 > /proc/<ID>/calc
# cat /proc/<ID>/calc
3,6,9,12,15
# sudo rmmod calc
-
Write a program that fulfills the following requirements:
-
Get all process PIDs and related information in the system from the /proc file system.
-
Output the PID of these processes, the process state, and the command line parameters of the process in three columns.
-
PID is 5 characters wide, right-aligned, and filled with spaces; each column of information is separated by a space.
-
For the output effect, please refer to the output effect
ps
of .
-e -ww -o pid:5,state,cmd
-
Experiment Tips
-
When implementing the read and write functions of the module, if you want to read the contents of the user cache, you must first use
copy_from_user
the function to copy the user cache to the kernel space; if you want to write to the user cache, you must usecopy_to_user
to copy the contents of the kernel space. These two functions are definedlinux/uaccess.h
in . -
The macros used to pass module parameters are defined
linux/moduleparam.h
in . Note that the macros used by ordinary parameters and array parameters are different. You can read the comments in the header file to learn more about these macros. -
Although the C standard library cannot be used in kernel module programming, the Linux kernel itself implements most of the functions in the standard library. Most of the functions useful for this experiment are defined in the header files
linux/kernel.h
andlinux/kstrtox.h
. If you want to use a certain standard library function, you may wish to search the Internet whether the Linux kernel comes with this function. -
Don’t forget to delete the created proc files and folders when the module exits.
-
The proc files needed to realize the simple ps program are
/proc/<PID>/cmdline
and/proc/PID/stat
. For some processes, the cmdline file is empty, and the content in/proc/<PID>/comm
the file . -
You can start from the template code.
-
You can refer to https://sysprog21.github.io/lkmpg/#the-proc-file-system for module writing, and refer to https://man7.org/linux/man-pages/man5/proc.5.html to understand proc The role of each file in
Experiment submission
Submission channel: Canvas
Submit files: 学号
_project1.zip
, source code folder 学号
_project1_src
(all source code files and Makefile), experiment report 学号
_project1_report.pdf
.
The content of the experimental report includes but is not limited to the experimental process, screenshots of the experimental results, and experimental experience (difficulties encountered during the experiment, solutions, or tips worth sharing).