I’m trying to get my program to restart itself, but nothing seems to work. I tried using fork(), but after killing the parent process the child gets killed too.
CODE
void sigup_handler(int signum) {
int pid = fork();
if (pid == 0) {
execve("prog2", NULL);
}
else
kill(getpid(), SIGTERM);
}
int main() {
puts("Program 2 started.");
signal(SIGHUP, sigup_handler);
sleep(50);
puts("Program 2 terminated.");
return 0;
}
Why bother with the
forkif you’re just going tokilltheparent? Just do theexec. The new instance of the program will still be the same process but will effectively be rebooted.