-
Notifications
You must be signed in to change notification settings - Fork 0
/
psjson.cpp
108 lines (96 loc) · 2.28 KB
/
psjson.cpp
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
/*
André Winston & Pitágoras Alves, UFRN, March 2017
This small program efficiently outputs all the current processes being run on
the machine in JSON format.
*/
#include <bits/stdc++.h>
using namespace std;
string run(const char* command){
int bufferSize = 128;
char buff[bufferSize];
string output = "";
FILE *procStream = popen(command, "r");
if(procStream == NULL){
throw std::runtime_error("Could not get process output");
}else{
try{
while (!feof(procStream)){
if (fgets(buff, bufferSize, procStream) != NULL){
output += buff;
}
}
}catch(...){
pclose(procStream);
throw std::runtime_error("Error while getting output of process");
}
pclose(procStream);
return output;
}
}
struct processo{
string nome;
string pid;
vector<processo> filhos;
};
map<string, vector<string> > arvore;
map<string, bool> vis;
string pgrep = "pgrep -P";
string pgrep1 = "pgrep -P";
string getname(string pid){
if(pid == "0") return "\n";
string str = "ps -p " + pid + " -o comm=";
return run(str.c_str());
}
string tabs(int k){
string a = "";
for(int i = 0; i < k; i++) a += "\t";
return a;
}
void printtabs(int k){
for(int i = 0; i < k; i++) printf("\t");
}
processo dfs(string v, int h, bool temprox){
processo p;
p.pid = v;
p.nome = getname(v);
//cout << "comando = " << pgrep.append(v).c_str() << endl;
printtabs(h);
printf("\"%s\": {\n", p.pid.c_str());
//cout << tabs(h) << "\""<<p.nome.substr(0, p.nome.size()-1)<<"\": {\n";
printtabs(h+1);
printf("\"nome\": \"%s\",\n", p.nome.substr(0, p.nome.size()-1).c_str());
//cout << tabs(h+1) << "\"pid\": " << p.pid << ",\n";
printtabs(h+1);
printf("\"filhos\": {\n");
//cout << tabs(h+1) << "\"filhos\": {\n";
vis[v] = true;
stringstream ss;
ss.str(run(pgrep.append(v).c_str()));
pgrep = pgrep1;
string filho;
vector<string> aux;
bool read = (ss>>filho);
while(read){
string prox;
read = (ss>>prox);
p.filhos.push_back(dfs(filho, h+2, read));
if(!read) break;
filho = prox;
}
printtabs(h+1);
printf("}\n");
//cout << tabs(h+1) << "}\n";
printtabs(h);
printf("}");
//cout << tabs(h) << "}";
if(temprox) puts(",");
//cout << endl;
puts("");
return p;
}
int main(){
cout << "{\n";
string v = "0";
processo p = dfs(v, 1, 0);
cout << "}\n";
}