问题
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.
回答1:
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 CGWindow
s and their respective pid
s, then you will know the pid
s 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;
}
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/7545695/how-to-identify-if-the-process-in-user-interface-process