如何判断进程是否在用户界面进程中?

发布于 2024-12-06 06:55:14 字数 180 浏览 1 评论 0原文

如何从进程中获取它是 UI(用户界面)进程还是非 ui 进程的信息?

我所说的 UI 进程是指 Finder、Dock、系统 UI 服务器或任何其他具有 UI 界面且由 Window Server 使用的 Mac 应用程序。

我想从 ProcessID 确定此信息。

我正在使用 mac os x。

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

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.

I want to determine this information from ProcessID.

I am using mac os x.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流云如水 2024-12-13 06:55:14

无法仅根据 PID 编号来确定特定进程是什么。原因是:进程 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
     ...

< em>编辑:你也许能够按照你的要求去做,尽管它很复杂。 这个答案提到:

CGWindow.h 中的函数 CGWindowListCopyWindowInfo() 将返回一组字典,每个字典对应与您设置的条件匹配的窗口,包括其他应用程序中的窗口。它只允许您按给定窗口上方的窗口、给定窗口下方的窗口和“屏幕上”窗口进行过滤,但返回的字典包含所属应用程序的进程 ID,您可以使用该进程 ID 将窗口与应用程序进行匹配。

如果你能获取所有的CGWindow和它们各自的pid,那么你不需要运行就能知道所有UI应用的pid ps 根本没有。

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

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.

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

ps -p <pid> -o ucomm=

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

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
     ...

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

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.

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 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;
}
顾北清歌寒 2024-12-13 06:55:14

有点复活这个...但是对于 macOS,要获取 UI 的各种元素的进程 ID,您可以使用 lsappinfo 因为也许是 Tiger?联机帮助页显示 2013 年 4 月,但我认为是在提出这个问题的时候,而且可能更早。该命令应以当前拥有 loginwindow 进程的用户身份运行。

lsappinfo info -only pid Dock
"pid"=545

Kind of resurrecting this one... But for macOS, to get the process ids of various elements of the UI, you can use lsappinfo since maybe Tiger? The manpage says April, 2013, but I think it was around at the time of this question being asked and probably earlier. The command should be run as the user who currently owns the loginwindow process.

lsappinfo info -only pid Dock
"pid"=545
千寻… 2024-12-13 06:55:14

尝试以下操作。

#include <unistd.h>

  if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || isatty(STDERR_FILENO))
    // Process associated with a terminal
  else
    // No terminal - probably UI process

Try the following.

#include <unistd.h>

  if (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || isatty(STDERR_FILENO))
    // Process associated with a terminal
  else
    // No terminal - probably UI process
初见终念 2024-12-13 06:55:14

根据 darvidsOn 的说法,以下是您问题的答案。

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

只需将您拥有的 processid 与数组项进行比较即可获得所需的结果。

On the lines of darvidsOn, below is the answer to your question.

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

Just compare the processid you have against the array items to get the desired result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文