I wanted a convenient way to restart a program. I thought I could just catch a signal (USR1 in the example) and call exec.
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
char* const* args;
void restart() {
    printf("restarting\n");
    execv(args[0], args);
}
int main(int argc, char* const argv[]) {
    printf("Starting\n");
    args = argv;
    signal(SIGUSR1, restart);
    raise(SIGUSR1); // could use pkill -SIGUSR1 file-name instead
    pause();
    printf("Terminated normally\n");
    return 0;
}
The above example kinda works. The output is
Starting
restarting
Starting
and then it hangs. Other signals can still be received.
I assume I'm just failing to clear the signal. The expected behavior is for program to keep restarting indefinitely.
 
     
     
     
     
    