UNX511/DSP912 - Lab 1: Monitoring Process Memory Usage

Due: Sunday, May 19, 2024

Information on all processes running on a Linux system is contained within the /proc directory. All directories under /proc which are digits represent a process. If you issue the command ps aux you will see all processes that are running. You can also see the processes ordered by cpu usage by issuing the command top.

In this lab, you will write a C/CPP program to scan all processes running on the system and report on those which have used more than 10Mbytes of resident memory. You can do this by going through each process directory in /proc and looking at the file status. For instance, let's say you want to look at the status information for process 10167:

$ cd /proc/10167
$ vim status

Inside the file status are variables such as Name: (the process name) and Pid: (the process pid). Virtual Memory is represented by VmSize: and Resident memory is represented by VmRSS:

  1. Write and execute a C/CPP program that will scan every directory in /proc that represents a process (each directory that begin with a number represents a process). Open the file status. If VmRSS: is greater than 10000 kB, then print out the process name, the process id and the memory used. See the code at the following link as an example on how to get all files in a directory:
    https://www.unix.com/programming/33263-unix-c-get-files-folder.html.

  2. Open a second command line and execute the command top which orders processes by cpu usage.
    $ top
    Compare the processes reported in top with those reported by your program. You can cancel the execution of top with ctrl-c.
    See the following link for an explanation of top:
    https://linux.die.net/man/1/top.

  3. Explain the difference between virtual memory and resident memory. What is virtual memory? What is resident memory?

  4. Research and report on any of the three of the processes returned by your program. Simply explain what these processes do.

The following web-page contains detailed information about each of the files under /proc:
proc - process information pseudo-filesystem: https://linux.die.net/sag/proc-fs.html.

Sample source code that retrieves system information such as model name, cpu speed, memory usage, processes running/blocked, up time and idle time can be seen at MemMonitor.cpp.

Lab Submission:

Email me your C/CPP file and Makefile (#1 above) as well as a short document (#3 and #4 above) to: miguel.watler@senecapolytechnic.ca

NB: My last name is Watler, not Walter.