-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSystem.cpp
More file actions
99 lines (85 loc) · 2.28 KB
/
Copy pathSystem.cpp
File metadata and controls
99 lines (85 loc) · 2.28 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
#include "System.h"
System::System()
{
UpdateProcessList();
}
System::~System()
{
}
void System::ThreadUpdateProcs()
{
if (!procsThreadRunning)
{
procsThreadRunning = true;
updatedProcList = false;
updateProcs = std::thread(&System::UpdateProcessList, this);
updateProcs.detach();
printf("Successfully created process update thread.\n");
return;
}
printf("The process update thread is already running...\n");
}
void System::ThreadUpdateAddrSpace(int procID)
{
if (!addrSpaceThreadRunning)
{
addrSpaceThreadRunning = true;
updatedAddrSpace = false;
updateAddrSpace = std::thread(&System::UpdateAddressSpace, this, procID);
updateAddrSpace.detach();
printf("Successfully created address space update thread.\n");
return;
}
printf("The address space update thread is already running...\n");
}
void System::UpdateProcessList()
{
printf("Updating process list...\n");
int pid;
char progName[1024] = {};
char commandStr[] = "ps -A";
char *lineBuffer = (char*)calloc(0x1000, 1);
FILE * fp = popen(commandStr, "r");
if (fp == NULL)
{
perror("Unable to open ps, or invalid argument\n");
free(lineBuffer);
procsThreadRunning = false;
updatedProcList = true;
return;
}
procListMutex.lock();
procList.clear();
while(fgets(lineBuffer, 0x1000, fp) != NULL)
{
sscanf(lineBuffer, "%i %*s %*s %s", &pid, progName);
procList.push_back(Process(progName, pid));
memset(progName, 0, 1024);
}
procListMutex.unlock();
updatedProcList = true;
int ret = pclose(fp);
if (ret < 0)
{
perror("Unable to close fp for 'ps -A'\n");
}
free(lineBuffer);
printf("Finished updating process list.\n");
procsThreadRunning = false;
return;
}
void System::UpdateAddressSpace(int row)
{
addrSpaceMutex.lock();
procList[row].UpdateAddrSpace();
addrSpaceMutex.unlock();
}
void System::PrintProcessList()
{
std::cout << " PID " << " Name " << std::endl;
for (size_t i = 0; i < procList.size(); i++)
{
std::cout << procList[i].pid << " " << procList[i].procName << std::endl;
}
std::cout << std::endl;
}