gpt4 book ai didi

c++ - 从 C 和 C++ 中的父进程获取子进程列表(跨平台,无命令行)

转载 作者:行者123 更新时间:2023-12-01 14:47:37 25 4
gpt4 key购买 nike

如何在不使用命令行的情况下以跨平台方式在 C 和 C++ 中从给定的父进程 ID 获取子进程 ID 列表?我自己在下面提供了一个答案,其中涵盖了 Win32、macOS、Linux、FreeBSD、DragonFly BSD 和 Darwin,使用 C++。
随意将我的代码转换为 C 解决方案(根据需要以及 native API 或 POSIX),或者使用不同的 API 或方法提供您自己的解决方案,但不使用 popen() 或 system() 之类的东西。添加对更多平台的支持显然也是受欢迎的。
例如:其他*BSD、Solaris、移动平台等。

最佳答案

下面的代码主要来自 this command line app I wrote .
赢32

#include <algorithm>
#include <vector>

#include "childpids.h"

#include <windows.h>
#include <tlhelp32.h>

using std::vector;

void ProcIdFromParentProcId(PROCID parentProcId, PROCID **procId, int *size) {
vector<PROCID> vec; int i = 0;
HANDLE hp = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe = { 0 };
pe.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hp, &pe)) {
do {
if (pe.th32ParentProcessID == parentProcId) {
vec.push_back(pe.th32ProcessID); i++;
}
} while (Process32Next(hp, &pe));
}
CloseHandle(hp);
*procId = (PROCID *)malloc(sizeof(PROCID) * vec.size());
if (procId) {
std::copy(vec.begin(), vec.end(), *procId);
*size = i;
}
}
macOS 和达尔文
#include <algorithm>
#include <vector>

#include "childpids.h"

#include <sys/proc_info.h>
#include <libproc.h>

using std::vector;

void ParentProcIdFromProcId(PROCID procId, PROCID *parentProcId) {
proc_bsdinfo proc_info;
if (proc_pidinfo(procId, PROC_PIDTBSDINFO, 0, &proc_info, sizeof(proc_info)) > 0) {
*parentProcId = proc_info.pbi_ppid;
}
}

void ProcIdFromParentProcId(PROCID parentProcId, PROCID **procId, int *size) {
vector<PROCID> vec; int i = 0;
int cntp = proc_listpids(PROC_ALL_PIDS, 0, nullptr, 0);
vector<PROCID> proc_info(cntp);
std::fill(proc_info.begin(), proc_info.end(), 0);
proc_listpids(PROC_ALL_PIDS, 0, &proc_info[0], sizeof(PROCID) * cntp);
// this Darwin API ^ lists them in reverse order from other platforms
for (int j = cntp - 1; j >= 0; j--) {
if (proc_info[j] == 0) { continue; }
PROCID ppid; ParentProcIdFromProcId(proc_info[j], &ppid);
if (ppid == parentProcId) {
vec.push_back(proc_info[j]); i++;
}
}
*procId = (PROCID *)malloc(sizeof(PROCID) * vec.size());
if (procId) {
std::copy(vec.begin(), vec.end(), *procId);
*size = i;
}
}
Linux(链接:-lprocps)
// Note: Ubuntu/Debian need to install libprocps-dev for the development headers.
// All major Linux distros *should* have the actual library installed by default.
// Arch-Linux/Gentoo are exceptions if users did not install a DE which needs it.

#include <algorithm>
#include <vector>

#include "childpids.h"

#include <proc/readproc.h>

using std::vector;

void ProcIdFromParentProcId(PROCID parentProcId, PROCID **procId, int *size) {
vector<PROCID> vec; int i = 0;
PROCTAB *proc = openproc(PROC_FILLSTAT);
while (proc_t *proc_info = readproc(proc, nullptr)) {
if (proc_info->ppid == parentProcId) {
vec.push_back(proc_info->tgid); i++;
}
freeproc(proc_info);
}
*procId = (PROCID *)malloc(sizeof(PROCID) * vec.size());
if (procId) {
std::copy(vec.begin(), vec.end(), *procId);
*size = i;
}
}
FreeBSD(链接:-lutil -lc)
#include <algorithm>
#include <vector>

#include <cstdlib>

#include "childpids.h"

#include <sys/types.h>
#include <sys/user.h>

#include <libutil.h>

using std::vector;

void ProcIdFromParentProcId(PROCID parentProcId, PROCID **procId, int *size) {
vector<PROCID> vec; int i = 0; int cntp;
if (kinfo_proc *proc_info = kinfo_getallproc(&cntp)) {
for (int j = 0; j < cntp; j++) {
if (proc_info[j].ki_ppid == parentProcId) {
vec.push_back(proc_info[j].ki_pid); i++;
}
}
free(proc_info);
}
*procId = (PROCID *)malloc(sizeof(PROCID) * vec.size());
if (procId) {
std::copy(vec.begin(), vec.end(), *procId);
*size = i;
}
}
DragonFly BSD(链接:-lkvm -lc)
#include <algorithm>
#include <vector>

#include <cstdlib>
#include <climits>

#include "childpids.h"

#include <fcntl.h>

#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>

#include <kvm.h>

using std::vector;

static kvm_t *kd = nullptr;

void ProcIdFromParentProcId(PROCID parentProcId, PROCID **procId, int *size) {
vector<PROCID> vec; int i = 0;
char errbuf[_POSIX2_LINE_MAX];
kinfo_proc *proc_info = nullptr;
const char *nlistf, *memf; nlistf = memf = "/dev/null";
kd = kvm_openfiles(nlistf, memf, nullptr, O_RDONLY, errbuf); if (!kd) return;
int cntp = 0; if ((proc_info = kvm_getprocs(kd, KERN_PROC_ALL, 0, &cntp))) {
for (int j = 0; j < cntp; j++) {
if (proc_info[j].kp_pid >= 0 && proc_info[j].kp_ppid >= 0 &&
proc_info[j].kp_ppid == parentProcId) {
vec.push_back(proc_info[j].kp_pid); i++;
}
}
free(proc_info);
}
*procId = (PROCID *)malloc(sizeof(PROCID) * vec.size());
if (procId) {
std::copy(vec.begin(), vec.end(), *procId);
*size = i;
}
}
childpids.h
#ifdef _WIN32
typedef unsigned long PROCID;
#else
typedef int PROCID;
#endif

void ProcIdFromParentProcId(PROCID parentProcId, PROCID **procId, int *size);
childpids.cpp
#include <iostream>
#include <vector>

#include <cstring>

#include "childpids.h"

using std::vector;

void PrintPidFromPpidRecursive(PROCID parentProcId) {
PROCID *procId; int size;
ProcIdFromParentProcId(parentProcId, &procId, &size);
if (procId) {
for (int i = 0; i < size; i++) {
if (procId[i] == 0) { break; }
PrintPidFromPpidRecursive(procId[i]);
std::cout << procId[i] << std::endl;
}
free(procId);
}
}

int main(int argc, char *argv[]) {
PROCID parentProcId = (argc == 2) ? strtoul(argv[1], nullptr, 10) : 0;
PrintPidFromPpidRecursive(parentProcId);
return 0;
}

关于c++ - 从 C 和 C++ 中的父进程获取子进程列表(跨平台,无命令行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62365978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com