Skip to content

Commit 5140976

Browse files
textanalyticsmantextanalyticsman
textanalyticsman
authored and
textanalyticsman
committed
Adding information about processes
1 parent 016f262 commit 5140976

File tree

5 files changed

+148
-49
lines changed

5 files changed

+148
-49
lines changed

include/process.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,29 @@ It contains relevant attributes as shown below
88
*/
99
class Process {
1010
public:
11+
Process(int pid, std::string puser = "", std::string pcommand = "", float pcpu = 0, std::string pram = "", long puptime = 0);
1112
int Pid(); // TODO: See src/process.cpp
1213
std::string User(); // TODO: See src/process.cpp
1314
std::string Command(); // TODO: See src/process.cpp
1415
float CpuUtilization(); // TODO: See src/process.cpp
1516
std::string Ram(); // TODO: See src/process.cpp
1617
long int UpTime(); // TODO: See src/process.cpp
1718
bool operator<(Process const& a) const; // TODO: See src/process.cpp
18-
19+
// To set values
20+
void SetPid(int pid);
21+
void SetPuser(std::string puser);
22+
void SetPcommand(std::string pcommand);
23+
void SetPcpu(float pcpu);
24+
void SetPram(std::string pram);
25+
void SetPuptime(long puptime);
1926
// TODO: Declare any necessary private members
2027
private:
28+
int process_id;
29+
std::string process_user;
30+
std::string process_command;
31+
float process_cpu_utilization;
32+
std::string process_ram;
33+
long process_up_time;
2134
};
2235

2336
#endif

src/linux_parser.cpp

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,54 @@ string LinuxParser::Command(int pid[[maybe_unused]]) { return string(); }
182182
// REMOVE: [[maybe_unused]] once you define the function
183183
string LinuxParser::Ram(int pid[[maybe_unused]]) { return string(); }
184184

185-
// TODO: Read and return the user ID associated with a process
186-
// REMOVE: [[maybe_unused]] once you define the function
187-
string LinuxParser::Uid(int pid[[maybe_unused]]) { return string(); }
185+
// Read and return the user ID associated with a process
186+
string LinuxParser::Uid(int pid)
187+
{
188+
std::string line;
189+
std::string pattern{ R"(Uid:\s+(\d+))" };
190+
191+
std::ifstream filestream(kProcDirectory + std::to_string(pid) + kStatusFilename);
192+
std::string user_id;
193+
194+
if (filestream.is_open())
195+
{
196+
while (std::getline(filestream, line))
197+
{
198+
user_id = RegularExpression(pattern, line, 1);
199+
200+
if(user_id != "")
201+
break;
202+
}
203+
}
204+
205+
return user_id;
206+
}
188207

189208
// TODO: Read and return the user associated with a process
190209
// REMOVE: [[maybe_unused]] once you define the function
191-
string LinuxParser::User(int pid[[maybe_unused]]) { return string(); }
210+
string LinuxParser::User(int pid)
211+
{
212+
std::string line;
213+
214+
std::string uid { Uid(pid) };
215+
std::string pattern{ "(\\w+[-]*\\w+):x:" + uid };
216+
217+
std::ifstream filestream(kPasswordPath);
218+
std::string user_name;
219+
220+
if (filestream.is_open())
221+
{
222+
while (std::getline(filestream, line))
223+
{
224+
user_name = RegularExpression(pattern, line, 1);
225+
226+
if(user_name != "")
227+
break;
228+
}
229+
}
230+
231+
return user_name;
232+
}
192233

193234
// TODO: Read and return the uptime of a process
194235
// REMOVE: [[maybe_unused]] once you define the function

src/process.cpp

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,42 @@ using std::string;
1010
using std::to_string;
1111
using std::vector;
1212

13-
// TODO: Return this process's ID
14-
int Process::Pid() { return 0; }
15-
16-
// TODO: Return this process's CPU utilization
17-
float Process::CpuUtilization() { return 0; }
18-
19-
// TODO: Return the command that generated this process
20-
string Process::Command() { return string(); }
21-
22-
// TODO: Return this process's memory utilization
23-
string Process::Ram() { return string(); }
24-
25-
// TODO: Return the user (name) that generated this process
26-
string Process::User() { return string(); }
27-
28-
// TODO: Return the age of this process (in seconds)
29-
long int Process::UpTime() { return 0; }
13+
Process::Process(int pid, std::string puser, std::string pcommand, float pcpu, std::string pram, long puptime)
14+
:process_id{ pid }, process_user{ puser }, process_command{ pcommand }, process_cpu_utilization{ pcpu }, process_ram{ pram }, process_up_time{ puptime }
15+
{
16+
17+
}
18+
19+
// Set process ID
20+
void Process::SetPid(int pid){ Process::process_id = pid; }
21+
// Set process user
22+
void Process::SetPuser(std::string puser){ Process::process_user = puser; }
23+
// Set process command
24+
void Process::SetPcommand(std::string pcommand){ Process::process_command = pcommand; }
25+
// Set process CPU consumption
26+
void Process::SetPcpu(float pcpu){ Process::process_cpu_utilization = pcpu; }
27+
// Set process RAM consumption
28+
void Process::SetPram(std::string pram){ Process::process_ram = pram; }
29+
// Set process uptime
30+
void Process::SetPuptime(long puptime){ Process::process_up_time = puptime; }
31+
32+
// Return this process's ID
33+
int Process::Pid() { return Process::process_id; }
34+
35+
// Return this process's CPU utilization
36+
float Process::CpuUtilization() { return Process::process_cpu_utilization; }
37+
38+
// Return the command that generated this process
39+
string Process::Command() { return Process::process_command; }
40+
41+
// Return this process's memory utilization
42+
string Process::Ram() { return Process::process_ram; }
43+
44+
// Return the user (name) that generated this process
45+
string Process::User() { return Process::process_user; }
46+
47+
// Return the age of this process (in seconds)
48+
long int Process::UpTime() { return Process::process_up_time; }
3049

3150
// TODO: Overload the "less than" comparison operator for Process objects
3251
// REMOVE: [[maybe_unused]] once you define the function

src/processor.cpp

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
#include "processor.h"
22
#include "linux_parser.h"
3-
#include <thread>
4-
#include <chrono>
5-
#include <iostream>
63

74
// Return the aggregate CPU utilization
85
float Processor::Utilization()
96
{
107
// I have used the algorihm proposed at https://stackoverflow.com/questions/23367857/accurate-calculation-of-cpu-usage-given-in-percentage-in-linux
118

12-
// First reading to get CPU numbers
9+
// Reading to get CPU data
1310
Processor::cpu_data.push_back(LinuxParser::CpuUtilization());
14-
// Sleep during some time before executing second reading. Idea taken from https://stackoverflow.com/questions/52268378/c-usleep-returns-immediately-on-linux
15-
std::this_thread::sleep_for(std::chrono::milliseconds(Processor::delay_reading));
16-
// Second reading
17-
Processor::cpu_data.push_back(LinuxParser::CpuUtilization());
18-
11+
1912
float first_idle{ 0.0 };
2013
float first_non_idle{ 0.0 };
2114
float first_total{ 0.0 };
@@ -27,29 +20,51 @@ float Processor::Utilization()
2720
unsigned const int index_idle{3};
2821
unsigned const int index_wait{4};
2922

30-
for(long unsigned int i=0; i<cpu_data[0].size(); ++i)
23+
float consumption {0.0};
24+
25+
if(cpu_data.size() == 1)
3126
{
32-
if(i==index_idle || i==index_wait)
33-
{
34-
first_idle += std::stof(cpu_data[0][i]);
35-
second_idle += std::stof(cpu_data[1][i]);
36-
}
37-
else
27+
for(long unsigned int i=0; i<cpu_data[0].size(); ++i)
3828
{
39-
first_non_idle += std::stof(cpu_data[0][i]);
40-
second_non_idle += std::stof(cpu_data[1][i]);
41-
}
42-
}
29+
if(i==index_idle || i==index_wait)
30+
first_idle += std::stof(cpu_data[0][i]);
31+
else
32+
first_non_idle += std::stof(cpu_data[0][i]);
33+
}
34+
35+
first_total = first_idle + first_non_idle;
36+
second_total = second_idle + second_non_idle;
4337

44-
cpu_data.clear();
38+
float total_difference {second_total - first_total};
39+
float idle_difference {second_idle - first_idle};
40+
41+
consumption = (total_difference - idle_difference) / total_difference;
42+
}
43+
else if(cpu_data.size() == 2)
44+
{
45+
for(long unsigned int i=0; i<cpu_data[0].size(); ++i)
46+
{
47+
if(i==index_idle || i==index_wait)
48+
{
49+
first_idle += std::stof(cpu_data[0][i]);
50+
second_idle += std::stof(cpu_data[1][i]);
51+
}
52+
else
53+
{
54+
first_non_idle += std::stof(cpu_data[0][i]);
55+
second_non_idle += std::stof(cpu_data[1][i]);
56+
}
57+
}
4558

46-
first_total = first_idle + first_non_idle;
47-
second_total = second_idle + second_non_idle;
59+
first_total = first_idle + first_non_idle;
60+
second_total = second_idle + second_non_idle;
4861

49-
float total_difference {second_total - first_total};
50-
float idle_difference {second_idle - first_idle};
62+
float total_difference {second_total - first_total};
63+
float idle_difference {second_idle - first_idle};
5164

52-
float consumption {(total_difference - idle_difference) / total_difference};
65+
consumption = (total_difference - idle_difference) / total_difference;
5366

67+
cpu_data.erase(cpu_data.begin());
68+
}
5469
return consumption;
5570
}

src/system.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@ using std::vector;
1818
Processor& System::Cpu() { return cpu_; }
1919

2020
// TODO: Return a container composed of the system's processes
21-
vector<Process>& System::Processes() { return processes_; }
21+
vector<Process>& System::Processes()
22+
{
23+
vector<int> list_pids = LinuxParser::Pids();
24+
25+
for(int pid : list_pids)
26+
{
27+
Process process(pid, LinuxParser::User(pid));
28+
processes_.push_back(process);
29+
}
30+
31+
return processes_;
32+
}
2233

2334
// TODO: Return the system's kernel identifier (string)
2435
std::string System::Kernel() { return string(); }

0 commit comments

Comments
 (0)