Skip to content

Instantly share code, notes, and snippets.

@eltonvs
Last active November 21, 2016 01:35
Show Gist options
  • Save eltonvs/b1c53d3861801a3ab8c179c99418daee to your computer and use it in GitHub Desktop.
Save eltonvs/b1c53d3861801a3ab8c179c99418daee to your computer and use it in GitHub Desktop.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#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