Question: Project 2 Linux kernel module for task information In this project, you will write a Linux kernel module that uses the / proc file system
Project Linux kernel module for task information
In this project, you will write a Linux kernel module that uses the proc file system for displaying a task's information based on its process identifier value pid. Before beginning this project, be sure you have completed the Linux kernel module programming project in the chapter OperatingSystem Structures, which involves creating an entry in the proc file system. This project will involve writing a process identifier to the file procpid Once a pid has been written to the proc file, subsequent reads from procpid will report the command the task is running, the value of the task's pid, and the current state of the task. An example of how your kernel module will be accessed once loaded into the system is as follows:
echo procpid
cat procpid
command bash pid state
The echo command writes the characters to the procpid file. Your kernel module will read this value and store its integer equivalent as it represents a process identifier. The cat command reads from procpid where your kernel module will retrieve the three fields from the taskstruct associated with the task whose pid value is
I. Writing to the proc file system
In the kernel module project in the chapter OperatingSystem Structures, you learned how to read from the proc file system. We now cover how to write to proc Setting the field write in struct fileoperations to
write procwrite
causes the procwrite function of the figure below to be called when a write operation is made to procpid
ssizet procwritestruct file file char user usrbuf,
sizet count, lofft pos
int rv ;
char kmem;
allocate kernel memory
kmem kmalloccount GFPKERNEL;
copies user space usrbuf to kernel memory
copyfromuserkmem, usrbuf, count;
printkKERNINFO s
kmem;
return kernel memory
kfreekmem;
return count;
The kmalloc function is the kernel equivalent of the userlevel malloc function for allocating memory, except that kernel memory is being allocated. The GFPKERNEL flag indicates routine kernel memory allocation. The copyfromuser function copies the contents of usrbuf which contains what has been written to procpid to the recently allocated kernel memory. Your kernel module will have to obtain the integer equivalent of this value using the kernel function kstrtol which has the signature
int kstrtolconst char str unsigned int base, long res
This stores the character equivalent of str which is expressed as a base into res.
Finally, note that we return memory that was previously allocated with kmalloc back to the kernel with the call to kfree Careful memory managementwhich includes releasing memory to prevent memory leaksis crucial when developing kernellevel code.
II Reading from the proc file system
Once the process identifier has been stored, any reads from procpid will return the name of the command, its process identifier, and its state. As illustrated in Section the PCB in Linux is represented by the structure taskstruct, which is found in the include file. Given a process identifier, the function pidtask returns the associated taskstruct. The signature of this function appears as follows:
struct taskstruct pidtaskstruct pid pid
enum pidtype type
The kernel function findvpidint pid can be used to obtain the struct pid, and PIDTYPEPID can be used as the pidtype.
For a valid pid in the system, pidtask will return its taskstruct. You can then display the values of the command, pid, and state. You will probably have to read through the taskstruct structure in to obtain the names of these fields.
If pidtask is not passed a valid pid, it returns NULL. Be sure to perform appropriate error checking to check for this condition. If this situation occurs, the kernel module function associated with reading from procpid should return
In the source code download, we give the C program pid.c which provides some of the basic building blocks for beginning this project.Submit a Word or PDF document, including a screenshot of each completed step:
I. Writing to the proc File System points
II Reading from the proc File System points
III. Include in your document a screenshot of the code you had to add or modify to produce the results in parts I and II Make sure your name appears in the output of both parts.
Include in your document the name of each student and their contribution to this assignment. points What should screenshots look like? give me examples or images
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
