-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_info.c
More file actions
64 lines (49 loc) · 1.54 KB
/
Copy pathprocess_info.c
File metadata and controls
64 lines (49 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <dirent.h>
#include <ctype.h>
#include <string.h>
#include <ncurses.h>
int row=3,col=5;
void get_pid_info(const char * pid) {
char path[256];
long rss;
FILE *fp;
// status file
snprintf(path, sizeof(path), "/proc/%s/status", pid);
fp = fopen(path, "r");
if (!fp) return;
char line[256];
char name[256] = "Unknown";
while (fgets(line, sizeof(line), fp)) {
if (strncmp(line, "Name:", 5) == 0) sscanf(line, "Name:\t%s", name);
if (strncmp(line, "VmRSS:", 6) == 0) sscanf(line, "VmRSS:\t%ld", &rss);
}
fclose(fp);
//display
mvprintw(row++, col,"%-8s %-20s %ld KB", pid, name, rss);
}
void get_process_info() {
//open proc directory
DIR *dir = opendir("/proc");
if (dir == NULL) {
perror("opendir");
return;
}
/*
struct dirent {
ino_t d_ino; // Inode number (the file's unique ID)
off_t d_off; // Not an offset you'd usually use
unsigned short d_reclen; // Length of this record
unsigned char d_type; // Type of file (DT_REG for file, DT_DIR for folder)
char d_name[256]; // The actual filename
};
*/
mvprintw(row++,col,"%-8s %-20s %s", "PID", "NAME", "MEMORY (RSS)");
mvprintw(row++,col,"------------------------------------------");
struct dirent *entry;
while ((entry= readdir(dir)) ) {
// select only processes
if (isdigit(entry->d_name[0]))
get_pid_info(entry->d_name);
}
closedir(dir);
}