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