单纯根据PID是没有办法判断的number具体流程是什么。原因是:进程 ID 在启动时从 PID=1 开始按顺序分配(某种程度上),并且不同系统的启动可能不同。例如,如果 Finder 或 Dock 崩溃并且必须重新启动,进程 ID 也将被重新分配。
如果您可以使用特定的命令运行终端命令pid
不过,你已经这样做了:
ps -p <pid> -o ucomm=
您将获得进程的文件名,您可以根据您知道的 UI 进程列表进行检查。例如,这是某些的输出ps
我的系统上当前登录会话的命令:
> ps -p 110 -o ucomm=
Dock
> ps -p 112 -o ucomm=
Finder
以下命令将为您提供按进程 ID 顺序排列的进程列表,仅包含名称:
> ps -ax -o pid=,ucomm=
1 launchd
10 kextd
11 DirectoryService
...
EDIT:你也许能够按照你的要求去做,尽管这很复杂。This答案提到:
CGWindow.h 中的函数 CGWindowListCopyWindowInfo() 将返回一组字典,每个字典对应与您设置的条件匹配的窗口,包括其他应用程序中的窗口。它只允许您按给定窗口上方的窗口、给定窗口下方的窗口和“屏幕上”窗口进行过滤,但返回的字典包含所属应用程序的进程 ID,您可以使用该进程 ID 将窗口与应用程序进行匹配。
如果你能获得全部CGWindow
s 及其各自的pid
s,那么你就会知道pid
无需运行所有 UI 应用程序ps
at all.
Rahul 已为此方法实现了以下代码,他要求我将其添加到我的答案中:
CFArrayRef UiProcesses()
{
CFArrayRef orderedwindows = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
CFIndex count = CFArrayGetCount (orderedwindows);
CFMutableArrayRef uiProcess = CFArrayCreateMutable (kCFAllocatorDefault , count, &kCFTypeArrayCallBacks);
for (CFIndex i = 0; i < count; i++)
{
if (orderedwindows)
{
CFDictionaryRef windowsdescription = (CFDictionaryRef)CFArrayGetValueAtIndex(orderedwindows, i);
CFNumberRef windowownerpid = (CFNumberRef)CFDictionaryGetValue (windowsdescription, CFSTR("kCGWindowOwnerPID"));
CFArrayAppendValue (uiProcess, windowownerpid);
}
}
return uiProcess;
}