Last active
November 21, 2016 01:35
-
-
Save eltonvs/b1c53d3861801a3ab8c179c99418daee to your computer and use it in GitHub Desktop.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <sstream> | |
#include <vector> | |
#include <map> | |
struct Proc { | |
int pid; | |
int ppid; | |
double memory; | |
double cpu; | |
int threads; | |
std::string name; | |
}; | |
int main() { | |
// Create Map | |
std::map<int, std::vector<Proc>> processes; | |
// Create file with all data about process | |
system("ps xao pid,ppid,pmem,pcpu,thcount,comm > proc_list.dat"); | |
// Open file | |
std::ifstream file("proc_list.dat"); | |
if (!file.is_open()) { | |
exit(1); | |
} | |
// Just a variable to keep the line content | |
std::string line; | |
// Ignore first line with header info | |
std::getline(file, line); | |
// Read all lines from file | |
while (std::getline(file, line)) { | |
Proc a; | |
std::stringstream ss(line); | |
ss >> a.pid >> a.ppid >> a.memory >> a.cpu >> a.threads; | |
if (ss >> a.name) { | |
processes[a.ppid].push_back(a); | |
} | |
} | |
// Close file and delete him | |
file.close(); | |
system("rm proc_list.dat"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment