-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm2.cc
61 lines (44 loc) · 955 Bytes
/
m2.cc
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
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
size_t status();
const size_t N=1<<20;
main(int argc, char**argv)
{
bool tail=0;
int opt;
while((opt=getopt(argc,argv,"01t")) != -1)
switch(opt) {
case '0': return 0;
case '1': return 1;
case 't': tail=1; break;
default : return 1;
}
if(optind >= argc) { fprintf(stderr, "argument missing\n"); return 1; }
size_t l=atol(argv[optind]);
size_t L=N/l; if(!L) L=1;
char** p=new char*[L];
for(size_t i=0; i < L; ++i)
p[i]=new char[l];
auto s1=status();
if(tail)
auto r=new char;
for(size_t i=0; i < L; ++i)
delete[] p[i];
auto s2=status();
delete p;
return s2 < s1;
}
size_t status()
{
char stat[256];
sprintf(stat, "/proc/%u/stat", getpid());
FILE *f=fopen(stat, "r");
if(!f) { perror(stat); exit(1); }
for(int i=0; i < 22; ++i) fscanf(f, "%s", stat);
size_t vss;
fscanf(f, "%lu", &vss);
fclose(f);
return vss;
}