I am trying to find the best solution to the following problem. I
would like to have process A be able to determine when process B has
terminated. Process B does not necessarily have to be a child of
process A. Any solutions in the mach, POSIX, BSD, Carbon or Cocoa
layers would be considered.
The two solutions I have found so far are as follows.
Solution 1:
API: Posix
Poll using kill( pid, 0 ) and wait for an ESRCH error indicating
that there is no process with the id 'pid'. This solution is not
good because it requires frequent poling for the life of process B
which may be long lived. It has the benefit that no special
privileges are needed to execute it.
Solution 2:
API: mach
Use task_for_pid( myTask, pid, &task ) to get a port on process B.
Use mach_port_request_notification( myTask, task,
MACH_NOTIFY_DEAD_NAME, 1, port, MACH_MSG_TYPE_MAKE_SEND_ONCE,
&lastPort) to register a notification on the mach port 'port' when
the mach port 'task' becomes a dead name. The drawbacks to this
solution are that process A needs to be running as root, be in the
group procmod or be digitally signed. It has the benefit that no
polling is required.
Are there any other solutions to this on Darwin?