Hard
PR_SET_PDEATHSIG in a child forked from a short-lived thread
A long-running supervisor launches each worker process from a short-lived helper thread, as below. The child sets PR_SET_PDEATHSIG to SIGKILL so that it dies if the supervisor crashes. The helper thread returns right after fork(), and the supervisor joins it and then keeps running for hours. What happens to ./worker?
supervisor.cpp
#include <sys/prctl.h>
#include <pthread.h>
#include <csignal>
#include <unistd.h>
void* spawn(void*) {
pid_t parent = getpid();
pid_t pid = fork();
if (pid == 0) {
prctl(PR_SET_PDEATHSIG, SIGKILL);
if (getppid() != parent) _exit(1); // parent already gone?
execl("./worker", "worker", nullptr);
_exit(127);
}
return nullptr;
}
int main() {
pthread_t t;
pthread_create(&t, nullptr, spawn, nullptr);
pthread_join(t, nullptr);
for (;;) pause(); // supervise for hours
}49 of 53 in Operating Systems