From 24b3afa684e57176f5068fd5896679ae0fa047ad Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 9 Jan 2023 11:29:22 +0100 Subject: Add process.h: is_running() --- process.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 process.cpp (limited to 'process.cpp') diff --git a/process.cpp b/process.cpp new file mode 100644 index 0000000..b30ef3d --- /dev/null +++ b/process.cpp @@ -0,0 +1,27 @@ +#include "process.h" + +#include +#include + +#include "file.h" + +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; + + std::string s{Reichwein::File::getFile(pid_file)}; + + 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"; +} -- cgit v1.2.3