tags:

views:

184

answers:

2

using c how do I wait for PID X to exit when it is not a child of my current process?

Kill(pid, SIGTERM);
waitpid(pid,NULL,0);

The above does not work as 'pid' is not a child process.

+2  A: 

See this answer by chaos:

The usual practice is to poll using kill(0, pid) and looking for return value -1 and errno of ESRCH to indicate that the process is gone

Based on the man page for kill, it might be kill(pid,0) instead. But the idea is the same.

Andomar
Andomar - thanks for your answer and pointing out chaos answer to similar question. I Should have spotted that before posting this one.
Andrew
this is a bit racy, because the pid can overflow and then be assigned to a new process, so you might end up verfying another process than you initially thought.
And also, polling for things like this in time intervals is really ugly, as it shortens battery life, and makes your apps appear on powertop and people will laugh at you until you stop to do that in shame.
+1  A: 

The only way to do this in a clean way (i.e. without polling in time intervals, and without risking PID overflows) is to use the Netlink cn_proc connector (which is Linux specific). There's not much example code or documentation around. It's not a nice API, but it's basically to only sensible API for this.

Look for PROC_EVENT_EXIT, which is the event your are interested in.

http://google.com/codesearch?q=PROC%5FEVENT%5FEXIT&hl=en&btnG=Search+Code