-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_monitor.cpp
More file actions
132 lines (104 loc) · 3.42 KB
/
process_monitor.cpp
File metadata and controls
132 lines (104 loc) · 3.42 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
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <locale>
#include <stdexcept>
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <tlhelp32.h>
struct ProcessInfo {
DWORD pid;
std::wstring name;
};
std::vector<ProcessInfo> getProcesses() {
std::vector<ProcessInfo> processes;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE) return processes;
PROCESSENTRY32W pe32;
pe32.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(hSnapshot, &pe32)) {
CloseHandle(hSnapshot);
return processes;
}
do {
processes.push_back(ProcessInfo{ pe32.th32ProcessID, std::wstring(pe32.szExeFile) });
} while (Process32NextW(hSnapshot, &pe32));
CloseHandle(hSnapshot);
return processes;
}
void printProcesses(const std::vector<ProcessInfo>& procs) {
constexpr int pidWidth = 10;
constexpr int nameWidth = 40;
std::wcout << std::left << std::setw(pidWidth) << L"PID"
<< std::setw(nameWidth) << L"Process Name" << L"\n";
std::wcout << std::wstring(pidWidth + nameWidth, L'-') << L"\n";
for (const auto& p : procs) {
std::wstring truncatedName = p.name.substr(0, nameWidth - 1);
std::wcout << std::setw(pidWidth) << p.pid
<< std::setw(nameWidth) << truncatedName << L"\n";
}
std::wcout << L"\nTotal Processes: " << procs.size() << L"\n";
}
#else
#include <dirent.h>
#include <fstream>
#include <algorithm>
struct ProcessInfo {
int pid;
std::string name;
};
std::vector<ProcessInfo> getProcesses() {
std::vector<ProcessInfo> processes;
DIR* proc = opendir("/proc");
if (!proc) return processes;
struct dirent* entry;
while ((entry = readdir(proc)) != nullptr) {
if (entry->d_type == DT_DIR) {
std::string pidStr = entry->d_name;
if (std::all_of(pidStr.begin(), pidStr.end(), ::isdigit)) {
int pid = std::stoi(pidStr);
std::ifstream commFile("/proc/" + pidStr + "/comm");
std::string name;
if (commFile.is_open()) {
std::getline(commFile, name);
commFile.close();
processes.push_back({ pid, name });
}
}
}
}
closedir(proc);
return processes;
}
void printProcesses(const std::vector<ProcessInfo>& procs) {
constexpr int pidWidth = 10;
constexpr int nameWidth = 40;
std::cout << std::left << std::setw(pidWidth) << "PID"
<< std::setw(nameWidth) << "Process Name" << "\n";
std::cout << std::string(pidWidth + nameWidth, '-') << "\n";
for (const auto& p : procs) {
std::string truncatedName = p.name.substr(0, nameWidth - 1);
std::cout << std::setw(pidWidth) << p.pid
<< std::setw(nameWidth) << truncatedName << "\n";
}
std::cout << "\nTotal Processes: " << procs.size() << "\n";
}
#endif
int main() {
#if defined(_WIN32) || defined(_WIN64)
SetConsoleOutputCP(CP_UTF8);
try {
std::locale::global(std::locale(""));
std::wcout.imbue(std::locale());
} catch (const std::runtime_error&) {
}
#endif
auto processes = getProcesses();
if (processes.empty()) {
std::cerr << "No processes found or failed to retrieve.\n";
return 1;
}
printProcesses(processes);
return 0;
}