我有下面的代码,用于通过与 ntdll 互操作来获取 Windows 上的子进程列表。在 Linux 上是否有等效于“NtQueryInformationProcess”的方法,它可以让我获得指定进程的父进程的进程 ID(如 pbi.InheritedFromUniqueProcessId)?我需要代码通过 Mono 在 Linux 上运行,所以希望我只需要更改获取父进程 ID 的部分,以便代码与 Windows 上的代码基本相同。
public IList< Process > GetChildren( Process parent )
{
List< Process > children = new List< Process >();
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
ProcessBasicInformation pbi = new ProcessBasicInformation();
try
{
uint bytesWritten;
NtQueryInformationProcess(p.Handle,
0, ref pbi, (uint)Marshal.SizeOf(pbi),
out bytesWritten); // == 0 is OK
if (pbi.InheritedFromUniqueProcessId == parent.Id)
children.AddRange(GetChildren(p));
}
catch
{
}
}
return children;
}