-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp1.cpp
More file actions
393 lines (307 loc) · 8.95 KB
/
Copy pathp1.cpp
File metadata and controls
393 lines (307 loc) · 8.95 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<chrono>
#include<ctime>
#include<tuple>
#if defined(_WIN32)
#include<conio.h>
#endif
using namespace std;
template<typename T>
struct SLinkNode{
SLinkNode():
next(nullptr) {}
SLinkNode(const T& elem):
elem(elem),next(nullptr) {}
~SLinkNode(){
delete next;
}
T elem;
SLinkNode* next;
};
template<typename T>
struct DLinkNode{
DLinkNode():
next(nullptr),prev(nullptr) {}
DLinkNode(const T& elem):
elem(elem),next(nullptr),prev(nullptr) {}
~DLinkNode(){
delete next;
}
T elem;
DLinkNode *next,*prev;
};
// erase element if work return true
template<typename T>
void traverse(SLinkNode<T>* head,int count,auto &&work){
for(SLinkNode<T> *prev=head,*cur=prev->next;
cur!=nullptr && count>0;--count)
if(work(cur->elem)){
prev->next=cur->next;
cur->next=nullptr;
delete cur;
cur=prev->next;
}else{
prev=cur;
cur=cur->next;
}
}
// erase element if work return true
template<typename T>
void traverse(DLinkNode<T>* head,int count,auto &&work){
for(DLinkNode<T> *prev=head,*cur=prev->next;
cur!=nullptr && count>0;--count)
if(work(cur->elem)){
prev->next=cur->next;
cur->next=nullptr;
delete cur;
cur=prev->next;
}else{
prev=cur;
cur=cur->next;
}
}
template<typename T>
int list_length(SLinkNode<T>* list){
int len=0;
auto counter=[&](auto &&x){
++len;
return false;
};
traverse(list,1e9,counter);
return len;
}
template<typename T>
int list_length(DLinkNode<T>* list){
int len=0;
auto counter=[&](auto &&x){
++len;
return false;
};
traverse(list,1e9,counter);
return len;
}
template<typename T>
void BubbleSort(SLinkNode<T>* head,auto cmp){
int len=0;
for(auto p=head->next;p!=nullptr;p=p->next)
++len;
for(bool flag=false;len>1 && !flag;--len,flag=false){
SLinkNode<T> *p0=head,*p1=p0->next,*p2=p1->next;
for(int i=0;i+1<len;i++,p0=p1,p1=p2,p2=p2->next)
if(!cmp(p1->elem,p2->elem)){
p0->next=p2;
p1->next=p2->next;
p2->next=p1;
swap(p1,p2);
flag=true;
}
}
}
template<typename T>
void BubbleSort(DLinkNode<T>* head,auto cmp){
int len=0;
for(auto p=head->next;p!=nullptr;p=p->next)
++len;
for(bool flag=false;len>1 && !flag;--len,flag=false){
DLinkNode<T> *p0=head,*p1=p0->next,*p2=p1->next;
for(int i=0;i+1<len;i++,p0=p1,p1=p2,p2=p2->next)
if(!cmp(p1->elem,p2->elem)){
p0->next=p2;
p1->next=p2->next;
p2->next=p1;
swap(p1,p2);
flag=true;
}
}
}
struct process{
friend ostream& operator<<(ostream& out,const process& p){
return
out<<"PID: "<<p.pid
<<", NAME: "<<p.name
<<", MEM(K): "<<p.mem;
}
int pid;
string name;
int mem;
};
// return a SLinkList with head node.
SLinkNode<process>* GetProcess(){
SLinkNode<process>* res=nullptr;
#if defined(__linux__)
const char cmd[]="ps aux > system_process_data.txt";
#elif defined(_WIN32)
const char cmd[]="tasklist /FO CSV /NH > system_process_data.txt";
#endif
if(system(cmd)){
cerr<<"Fail to get process!\n";
return nullptr;
}
ifstream fin("system_process_data.txt",ios::in);
if(!fin.is_open()){
cerr<<"Fail to open system_process_data.txt!\n";
return nullptr;
}
char buf[0x400];
#if defined(__linux__)
fin.getline(buf,0x3ff,'\n');
for(process p;
fin>>buf
>>p.pid
>>buf>>buf>>buf
>>p.mem
>>buf>>buf>>buf>>buf
>>p.name;
fin.getline(buf,0x3ff,'\n')){
auto cur=new SLinkNode<process>(p);
cur->next=res; res=cur;
}
#elif defined(_WIN32)
for(process p;fin.getline(buf,0x3ff,'\n');){
// get data from csv
for(int i=0,j=0,n=strlen(buf);i<n;++i,++j){
while(i<n && buf[i]!='\"') ++i;
int l=++i;
while(i<n && buf[i]!='\"') ++i;
int r=i;
if(j==0)
p.name=string(buf+l,buf+r);
else if(j==1)
p.pid=stoi(string(buf+l,buf+r));
else if(j==4){
p.mem=0;
for(int k=l;k<r;k++)
if(isdigit(buf[k]))
p.mem=p.mem*10+(buf[k]-'0');
}
}
auto cur=new SLinkNode<process>(p);
cur->next=res; res=cur;
}
#endif
auto head=new SLinkNode<process>;
head->next=res; res=head;
return res;
}
using process_start=tuple<process,chrono::time_point<chrono::system_clock>>;
using process_end_dur=tuple<process,chrono::time_point<chrono::system_clock>,chrono::duration<double>>;
const int max_rows=10;
void react(){
auto t1=chrono::system_clock().now();
auto alive=new SLinkNode<process_start>;
auto deadhead=new DLinkNode<process_end_dur>,
deadtail=deadhead;
int cnt=0;
while(true){
// exit when input 'q'
#if defined(_WIN32)
while(kbhit()){
char c=getch();
if(c=='q'){
delete alive;
delete deadhead;
cout<<"exit"<<endl;
return;
}
}
#endif
// react each seconds
auto t2=chrono::system_clock().now();
if(t2-t1<1s) continue; t1=t2;
SLinkNode<process>* cur=GetProcess();
BubbleSort(cur,[](const process& a,const process& b){
return a.mem>b.mem;
});
// print alive process list
#if defined(__linux__)
const char cmd[]="clear";
#elif defined(_WIN32)
const char cmd[]="cls";
#endif
system(cmd);
cout<<"count: "<<(++cnt)<<endl;
cout<<"Alive Process (head "<<max_rows
<<", all: "<<list_length(cur)<<"):"
<<endl;
traverse(cur,max_rows,[](const process& p){
cout<<p<<endl;
return false;
});
auto cur_contains_p=[&](const process& p){
bool has_p=false;
auto contains_p=[&](const process& _p){
if(_p.pid==p.pid)
has_p=true;
return false;
};
traverse(cur,1e9,contains_p);
return has_p;
};
// update dead process list
// erase which appear
traverse(deadhead,1e9,[&](const process_end_dur& ped){
const auto &[p,e,d]=ped;
return cur_contains_p(p);
});
// append which disappear (also erase them from alive process list)
traverse(alive,1e9,[&](const process_start& ps){
const auto &[p,s]=ps;
bool has_p=cur_contains_p(p);
if(has_p) return false;
auto node=new DLinkNode<process_end_dur>;
node->next=nullptr;
deadtail->next=node;
node->prev=deadtail;
deadtail=node;
auto &[_p,e,d]=node->elem;
_p=p; e=t2; d=e-s;
return true;
});
// update alive process list
// append which appear
traverse(cur,1e9,[&](const process& p){
bool has_p=false;
auto contains_p=[&](const process_start& ps){
const auto &[_p,s]=ps;
if(_p.pid==p.pid)
has_p=true;
return false;
};
traverse(alive,1e9,contains_p);
if(!has_p){
auto node=new SLinkNode<process_start>;
node->next=alive->next;
alive->next=node;
auto &[_p,s]=node->elem;
_p=p; s=t2;
}
return false;
});
delete cur;
BubbleSort(deadhead,[](const process_end_dur& a,const process_end_dur& b){
const auto &[ap,ae,ad]=a;
const auto &[bp,be,bd]=b;
return ad<bd;
});
// print dead process list
cout<<endl<<"Dead Process (head "<<max_rows
<<", all: "<<list_length(deadhead)<<"):"
<<endl;
traverse(deadhead,max_rows,[](const process_end_dur& ped){
const auto &[p,e,d]=ped;
auto _e=chrono::system_clock::to_time_t(e);
char* buffer=ctime(&_e);
buffer[19]='\0';
cout<<p<<" end_time("<<(buffer+11)<<") duration:"<<d.count()<<"s"<<endl;
return false;
});
}
}
int main(){
react();
return 0;
}