I have a detached child process. It knows its parent's pid. Is there some way on both MacOS and Linux that I can listen for when the parent process exits, from the child process?
I need to make a call to delete a lockfile from the child, when the parent process dies.
I am looking for a solution that does not require polling.
I heard that waitid()
might do it. I have this small program that compiles on MacOS with gcc, but it exits with code 255 - my only guess is that this is because waitid() won't work with processes that aren't child process of the current process?
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
pid_t pid = atoi(argv[1]);
printf("pid = %jd\n", (intmax_t) pid);
siginfo_t sig;
return waitid(P_PID, pid, &sig, WEXITED|WNOWAIT);
}
ps
for PPID?select()
/poll()
/read()
to detect the pipe going away when the parent dies (which would cause the writing end of the pipe to be closed, andread()
to return for instance)