如何识别用户界面进程中的进程? [英] How to Identify if the process in User Interface Process?

查看:114
本文介绍了如何识别用户界面进程中的进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从UI(用户界面)进程或非UI进程中获取信息?

How can I get information from a process that it is UI(User Interface) process or non-ui?

对于UI进程,我是指Finder,Dock,系统UI服务器或任何其他具有UI界面且由Window Server使用的mac应用程序.

With UI process I mean, Finder, Dock, System UI server, or any other mac application which has UI interface and it is used by Window Server.

我想从ProcessID确定此信息.

I want to determine this information from ProcessID.

我正在使用Mac OSX.

I am using mac os x.

推荐答案

无法仅基于PID 数字来确定特定过程是什么.这样做的原因是:在启动时从PID = 1顺序分配(某种程度上)进程ID,并且对于不同的系统启动可能会有所不同.例如,如果Finder或Dock崩溃并且必须重新启动,则还将重新分配进程ID.

There is no way to determine based purely on the PID number what a specific process is. The reason for this: Process IDs are assigned (somewhat) sequentially from PID=1 on startup, and startup can be different for different systems. The process ID will also be reassigned if, for example, Finder or Dock crashes and has to be restarted.

但是,如果可以使用特定的pid运行终端命令,请执行以下操作:

If you can run a terminal command with a specific pid that you have, though, do this:

ps -p <pid> -o ucomm=

您将获得该进程的文件名,您可以根据您知道的UI进程列表来进行检查.例如,这是我系统上当前登录会话的某些ps命令的输出:

You'll get the filename of the process, which you can check against a list of ones you know are UI processes. For example, here is the output of certain ps commands on my system for my current login session:

> ps -p 110 -o ucomm=
Dock

> ps -p 112 -o ucomm=
Finder

以下命令将按进程ID的顺序提供进程列表,仅包含名称:

And the following command will give you a list of processes in order of process ID, with only the name:

> ps -ax -o pid=,ucomm=
   1 launchd
  10 kextd
  11 DirectoryService
     ...


尽管令人费解,但您仍然可以按照自己的要求做. 答案提到:


You may be able to do what you ask, though it is convoluted. This answer mentions:

来自CGWindow.h的函数CGWindowListCopyWindowInfo()将返回一个字典数组,每个与您设置的条件匹配的窗口(包括其他应用程序中的一个)都将包含一个字典.它仅允许您按给定窗口上方的窗口,给定窗口下方的窗口和屏幕上"窗口进行过滤,但是返回的字典包含拥有应用程序的进程ID,可用于将窗口与应用程序进行匹配.

The function CGWindowListCopyWindowInfo() from CGWindow.h will return an array of dictionaries, one for each window that matches the criteria you set, including ones in other applications. It only lets you filter by windows above a given window, windows below a given window and 'onscreen' windows, but the dictionary returned includes a process ID for the owning app which you can use to match up window to app.

如果可以获得所有CGWindow和它们各自的pid,那么您将知道所有UI应用程序的pid,而无需完全运行ps.

If you can obtain all the CGWindows and their respective pids, then you will know the pids of all UI applications without needing to run ps at all.

Rahul为此方法实现了以下代码,他要求我将其添加到我的答案中:

Rahul has implemented the following code for this approach, which he requested I add to my answer:

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;
}

这篇关于如何识别用户界面进程中的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆