Re: [pstreams-users] Empty process output
Brought to you by:
redi
From: Jonathan W. <pst...@ka...> - 2013-02-06 14:27:32
|
On 6 February 2013 13:39, Johannes Wienke wrote: > > Actually the process I am inspecting is still alive, but there seems to > be a bug in pstree when a thread of that process goes away. In that case > pstree tries to access the thread's stat-file and exists with "No such > file or directory" on stderr. Not exactly what I expected from such a > tool... I will search for a different way to find all subprocesses. Eurgh, that's not very helpful either. I suppose you could read from the child process' stderr and if you get that error then you could try calling pstree again, e.g. (untested) #include <pstreams/pstream.h> #include <sstream> #include <iostream> int main(int argc, char** argv) { std::string pid = argc > 1 ? argv[1] : "1"; std::string pstreeOut; while (pstreeOut.empty()) { redi::ipstream in("pstree -pa " + pid); std::stringstream ss; ss << in.rdbuf(); pstreeOut = ss.str(); if (pstreeOut.empty()) { ss << in.err().rdbuf(); pstreeOut = ss.str(); // handle pstree error when a thread terminates while processing if (pstreeOut.find("No such file or directory") == std::string::npos) throw std::runtime_error("No such process: " + pid); } } std::cout << pstreeOut << '\n'; } |