int main(void) { listdir("/proc", 0); pstree(); ps(); return 0; }
// Returns the minimum list of process trees that include all of the // specified pids using the specified list of processes. inline Try<std::list<ProcessTree> > pstrees( const std::set<pid_t>& pids, const std::list<Process>& processes) { std::list<ProcessTree> trees; foreach (pid_t pid, pids) { // First, check if the pid is already connected to one of the // process trees we've constructed. bool disconnected = true; foreach (const ProcessTree& tree, trees) { if (tree.contains(pid)) { disconnected = false; break; } } if (disconnected) { Try<ProcessTree> tree = pstree(pid, processes); if (tree.isError()) { return Error(tree.error()); } // Now see if any of the existing process trees are actually // contained within the process tree we just created and only // includ the disjoint process trees. // C++11: // trees = trees.filter([] (const ProcessTree& t) { // return tree.get().contains(t); // }); std::list<ProcessTree> trees_ = trees; trees.clear(); foreach (const ProcessTree& t, trees_) { if (tree.get().contains(t.process.pid)) { continue; } trees.push_back(t); } trees.push_back(tree.get()); } }
int main(int argc, char* argv[]) { pstree(); return 0; }
// Returns a process tree for the specified pid (or all processes if // pid is none or the current process if pid is 0). inline Try<ProcessTree> pstree(Option<pid_t> pid = None()) { if (pid.isNone()) { pid = 1; } else if (pid.get() == 0) { pid = getpid(); } const Try<std::list<Process> >& processes = os::processes(); if (processes.isError()) { return Error(processes.error()); } return pstree(pid.get(), processes.get()); }