summaryrefslogtreecommitdiffhomepage
path: root/process.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-02-05 00:44:21 +0100
committerRoland Reichwein <mail@reichwein.it>2023-02-05 00:44:21 +0100
commit7361b79da6f611478428ebac022ad41a22414c85 (patch)
treec12216eeac4e63918a4da3c12d8d9e5f956bca9e /process.cpp
parentce999fa6e8ec42342db5854188e7c4be7b2e576c (diff)
Added number_of_threads()
Diffstat (limited to 'process.cpp')
-rw-r--r--process.cpp43
1 files changed, 34 insertions, 9 deletions
diff --git a/process.cpp b/process.cpp
index 67cf1ce..c504330 100644
--- a/process.cpp
+++ b/process.cpp
@@ -16,23 +16,48 @@ namespace fs = std::filesystem;
bool Reichwein::Process::is_running(pid_t pid)
{
- fs::path pid_file{"/proc/" + std::to_string(pid) + "/stat"};
- if (!fs::exists(pid_file))
- return false;
+ // 3rd position is status for pid
+ fs::path pid_file{"/proc/" + std::to_string(pid) + "/stat"};
+ if (!fs::exists(pid_file))
+ return false;
- std::string s{Reichwein::File::getFile(pid_file)};
+ std::string s{Reichwein::File::getFile(pid_file)};
- auto pos0{s.find(' ', 0)};
+ auto pos0{s.find(' ', 0)};
+ pos0 = s.find(' ', pos0 + 1);
+ pos0++;
+
+ auto pos1{s.find(' ', pos0 + 1)};
+
+ std::string state{s.substr(pos0, pos1 - pos0)};
+
+ return state == "R" || state == "S";
+}
+
+int Reichwein::Process::number_of_threads(pid_t pid)
+{
+ // 20th position is the number of threads for pid
+ fs::path pid_file{"/proc/" + std::to_string(pid) + "/stat"};
+ if (!fs::exists(pid_file))
+ return false;
+
+ std::string s{Reichwein::File::getFile(pid_file)};
+
+ size_t pos0{};
+ for (int i = 0; i < 19; i++) {
pos0 = s.find(' ', pos0 + 1);
- pos0++;
+ if (pos0 == std::string::npos)
+ throw std::runtime_error("Bad format in /proc/" + std::to_string(pid) + "/stat");
+ }
- auto pos1{s.find(' ', pos0 + 1)};
+ size_t pos1{s.find(' ', pos0 + 1)};
- std::string state{s.substr(pos0, pos1 - pos0)};
+ std::string number_of_threads_s{s.substr(pos0, pos1 - pos0)};
- return state == "R" || state == "S";
+ return std::stoi(number_of_threads_s);
}
+
// tcp: tcp or tcp6
bool Reichwein::Process::tcp_is_pid_listening_on(const std::string& tcp, pid_t pid, int port)
{