diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-09 11:29:22 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-09 11:29:22 +0100 |
commit | 24b3afa684e57176f5068fd5896679ae0fa047ad (patch) | |
tree | c9f6d03fadcb762ae0163f14f52462338249e62a /process.cpp | |
parent | e7dd0b98770ee0c88c1ea976d9e9a6d3979782f7 (diff) |
Add process.h: is_running()
Diffstat (limited to 'process.cpp')
-rw-r--r-- | process.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
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 <filesystem> +#include <string> + +#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"; +} |