Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to gather the following page-related system statistics in a C program in

ID: 3914578 • Letter: I

Question

I need to gather the following page-related system statistics in a C program in a Linux environment (centOS). I need to use libraries (#include <..>), not command line commands (system calls), since the code is intended to be part of a static system call.

The info I need:

1. the current number of free pages (over all memory zones);
2. the current number of pages used by slab allocator (over all memory zones);
3. the current number of pages in the active list (over all memory zones);
4. the current number of pages in the inactive list (over all memory zones);
5. the current number of pages in the active list whose reference bits are set (over all memory zones);
6. the current number of pages in the inactive list whose reference bits are set (over all memory zones);
7. the cumulative number of pages moved from the active list to the inactive list (since the last machine boot);
8. the cumulative number of pages evicted from the inactive list (since the last machine boot);

Explanation / Answer

#include "sys/types.h" #include "sys/sysinfo.h" int parseLine(char *line) { // This assumes that a digit will be found and the line ends in " Kb". int i = strlen(line); const char *p = line; while (*p < '0' || *p > '9') p++; line[i - 3] = ''; i = atoi(p); return i; } typedef struct { uint32_t virtualMem; uint32_t physicalMem; } processMem_t; processMem_t GetProcessMemory() { FILE *file = fopen("/proc/self/status", "r"); char line[128]; processMem_t processMem; while (fgets(line, 128, file) != NULL) { if (strncmp(line, "VmSize:", 7) == 0) { processMem.virtualMem = parseLine(line); break; } if (strncmp(line, "VmRSS:", 6) == 0) { processMem.physicalMem = parseLine(line); break; } } fclose(file); return processMem; }