问题
So I want to get a process pointer and do something like Win32 WaitForSingleObject(hProcess, INFINITE);
that would wait for process to quit if it is not null. How to create such GetProcessByName function for Mac/Unix like Os?
回答1:
Look at [NSWorkspace runningApplications]
and [NSRunningApplication runningApplicationsWithBundleIdentifier:]
. You can KVO observe NSRunningApplication's terminated
property. You can also watch the notification NSWorkspaceDidTerminateApplicationNotification
.
These are all 10.6+. If you need earlier versions, update your question with your version requirements. It's possible in older versions, but 10.6 made it much easier.
回答2:
You can only wait for a child process anyway, and if it's a child process you would have its PID (I never forget the names of my children; just their birthdays).
See: waitpid(2)
来源:https://stackoverflow.com/questions/11780718/how-to-get-get-process-by-name-on-mac-os