diff --git a/include/boost/process/v2/impl/pid.ipp b/include/boost/process/v2/impl/pid.ipp index 61e848bb..e638fa14 100644 --- a/include/boost/process/v2/impl/pid.ipp +++ b/include/boost/process/v2/impl/pid.ipp @@ -147,15 +147,14 @@ std::vector child_pids(pid_type pid, boost::system::error_code & ec) std::vector all_pids(boost::system::error_code & ec) { std::vector vec; - vec.reserve(proc_listpids(PROC_ALL_PIDS, 0, nullptr, 0)); - if (proc_listpids(PROC_ALL_PIDS, 0, &vec[0], sizeof(pid_type) * vec.size())) + vec.resize(proc_listpids(PROC_ALL_PIDS, 0, nullptr, 0) / sizeof(pid_type)); + const auto sz = proc_listpids(PROC_ALL_PIDS, 0, &vec[0], sizeof(pid_type) * vec.size()); + if (sz < 0) { BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) return {}; } - auto itr = std::partition(vec.begin(), vec.end(), [](pid_type pt) {return pt != 0;}); - vec.erase(itr, vec.end()); - std::reverse(vec.begin(), vec.end()); + vec.resize(sz); return vec; } @@ -176,15 +175,14 @@ pid_type parent_pid(pid_type pid, boost::system::error_code & ec) std::vector child_pids(pid_type pid, boost::system::error_code & ec) { std::vector vec; - vec.reserve(proc_listpids(PROC_PPID_ONLY, (uint32_t)pid, nullptr, 0)); - if (proc_listpids(PROC_PPID_ONLY, (uint32_t)pid, &vec[0], sizeof(pid_type) * vec.size())) + vec.resize(proc_listpids(PROC_PPID_ONLY, (uint32_t)pid, nullptr, 0) / sizeof(pid_type)); + const auto sz = proc_listpids(PROC_PPID_ONLY, (uint32_t)pid, &vec[0], sizeof(pid_type) * vec.size()); + if (sz < 0) { BOOST_PROCESS_V2_ASSIGN_LAST_ERROR(ec) return {}; } - auto itr = std::partition(vec.begin(), vec.end(), [](pid_type pt) {return pt != 0;}); - vec.erase(itr, vec.end()); - std::reverse(vec.begin(), vec.end()); + vec.resize(sz); return vec; } diff --git a/test/v2/pid.cpp b/test/v2/pid.cpp index 4102acec..570bc259 100644 --- a/test/v2/pid.cpp +++ b/test/v2/pid.cpp @@ -34,6 +34,7 @@ BOOST_AUTO_TEST_CASE(child_pid) using boost::unit_test::framework::master_test_suite; const auto pth = bp2::filesystem::absolute(master_test_suite().argv[1]); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); auto cs = bp2::child_pids(bp2::current_pid()); boost::asio::io_context ctx;