0

I would like to implement my kernel to be able to monitoring memory of each process. However, all I can do is to only print out the process and pid. I can't find a function that can help in monitoring memory. Here is the code in the kernel that i Implemented. I use Linux kernel version 4.11.0-rc7.

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/resource.h>
#include <linux/sched.h>
#include <linux/syscalls.h>

asmlinkage long sys_listProcessInfo(void){
struct task_struct *process;
struct rusage usage;
int i = 0;
for_each_process(process){
    if(i%10 == 0){
    printk("Process %s\n PID: %ld\n",process->comm,(long)task_pid_nr(process));
    i++;
}
}
return 0;
}

This one can only view process and pid. I would like to know if there is any function that can look for the memory of the process. Thank you in advance.

asiandudeCom
  • 441
  • 1
  • 5
  • 26

2 Answers2

0

For a process other than the current one, use the /proc file system.

  char fName[32]; // you should really only need 24
  sprintf(fName, "/proc/%d/status", pid);
  FILE* status = fopen(fName , "r" );

That file will have information about the process's memory allocation (virtual, resident, etc.) See man7's page on /proc for details.

This is similar but not identical to this post.

Community
  • 1
  • 1
dlasalle
  • 1,615
  • 3
  • 19
  • 25
  • Thank you for your answer. I did know that this is also the way to do this. However, is there any other way from reading /proc file system. I thought that there might be the way to do this by using kernel function. @dlasalle – asiandudeCom Apr 26 '17 at 16:10
  • another way: look very carefully at process control blocks and how to access them. Look very carefully at the 'in memory' layout of the process. – user3629249 Apr 26 '17 at 20:44
0

This is the standard wrong kernel code (ignoring locking requirements etc.). The function name and intended purpose strongly hint this is just a college assignment. There were several other people coming up with problems of similar quality.

As each time it was clear the person asking is fundamentally not prepared to do this work, each time I ask who gave them the assignment and what material were they provided with.

Can you please answer the above?

Thanks.

EDIT

See this for a general overview what's wrong with code samples of the sort: http://codingtragedy.blogspot.com/2016/12/bad-kernel-tutorials-for-beginners.html

This website is unsuitable for kernel-related questions (or most lower level stuff from that matter). I'm afraid you will have to consult your fellow students. Preferably though someone would make sure tasks of the sort are not assigned to unprepared students, or even at all.

  • Yes, it is college assignment. The lecturer gave this assignment and he didn't provide anything. The student must install and study about kernel by themselves. There is no kernel lecture only basic stuff about the operating system. Hope this clarify. – asiandudeCom Apr 26 '17 at 16:08
  • I don't want to get you into trouble, but I would like to get contact information to your lecturer to ask him what's up with this course. –  Apr 26 '17 at 16:15
  • Sorry, I don't have contact information of my lecturer. Most of the work is supervised by TA but TA also busy that why I don't have any place to ask other than this place. – asiandudeCom Apr 26 '17 at 16:28