Project 1

$24.99 $18.99

Write modules that implement the following requirements Accepts three parameters: operand1type int, operand2type int array, operatortype charp (string). Create a proc file /proc/<你的学号>/calc. If operatoris add, then operand2each element of is added operand1to get the result array; if oeratoris mul, then operand2each element of is multiplied operand1to get the result array. When reading a proc file, output an array of results, each element separated by…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:
Tags:

Description

5/5 – (2 votes)
  1. Write modules that implement the following requirements

    • Accepts three parameters: operand1type int, operand2type int array, operatortype charp (string).

    • Create a proc file /proc/<你的学号>/calc.

    • If operatoris add, then operand2each element of is added operand1to get the result array; if oeratoris mul, then operand2each element of is multiplied operand1to 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 operand1recalculated .

    • 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
  1. 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
      -e -ww -o pid:5,state,cmd
      of .

Experiment Tips

  1. 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_userthe function to copy the user cache to the kernel space; if you want to write to the user cache, you must use copy_to_userto copy the contents of the kernel space. These two functions are defined linux/uaccess.hin .

  2. The macros used to pass module parameters are defined linux/moduleparam.hin . 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.

  3. 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.hand linux/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.

  4. Don’t forget to delete the created proc files and folders when the module exits.

  5. The proc files needed to realize the simple ps program are /proc/<PID>/cmdlineand /proc/PID/stat. For some processes, the cmdline file is empty, and the content in /proc/<PID>/commthe file .

  6. You can start from the template code.

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

Project 1
$24.99 $18.99