-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocmon.c
More file actions
55 lines (52 loc) · 1.64 KB
/
procmon.c
File metadata and controls
55 lines (52 loc) · 1.64 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
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
/* what this code does:
1. printf("%-8s %-20s %-6s %8s\n", "PID", "Name", "State", "Mem(kB)") */
int main(void)
{
while (1)
{ system("clear");
fflush(stdout);
printf("%-8s %-39s %-6s %8s\n", "PID", "Name", "State", "Mem(kB)");
DIR *d = opendir("/proc");
if (!d) return -1;
struct dirent *entry;
while ((entry = readdir(d)) != NULL)
{
if (atoi(entry->d_name) == 0)
continue;
char path[50], buffer[256];
if (snprintf(path, sizeof(path), "/proc/%d/status", atoi(entry->d_name)) < 0)
break;
FILE *fp = fopen(path, "r");
if (!fp)
{ if(errno==ENOENT) continue;
else perror(path);
break;
}
int pid;
char name[100];
char state;
int mem;
while (fgets(buffer, sizeof(buffer), fp) > 0)
{
if (strncmp(buffer, "Pid", 3) == 0)
sscanf(buffer, "Pid:\t%d", &pid);
else if (strncmp(buffer, "Name", 4) == 0)
sscanf(buffer, "Name:\t%s", name);
else if (strncmp(buffer, "State", 5) == 0)
sscanf(buffer, "State:\t%c", &state);
else if (strncmp(buffer, "VmRSS", 5) == 0)
sscanf(buffer, "VmRss:\t%d", &mem);
}
fclose(fp);
printf("%-8d %-39s %-6c %8d\n", pid, name, state, mem);
}
closedir(d);
sleep(2);
}
return 0;
}