5

Hi I have a program written in C. I have global variables, arrays, dynamic variables and also arrays etc... I send SIGHUP to my program and in handler I clean up the dynamic memory.And I want to restart my program with HUP.IS there a way to restart the program in c?. I want to exit and return from main in order to clean the memory that are handled by the static arrays in main and restart main.

Hi again. I edited the signal handler and added the execv in the handler. in the handler i have 2 functions. first is clean_up(); that cleans the dynamic arrays and the second is execv(). after sending HUP,first clean_up runs and then execv. but after a small time i have seg fault. but ,when i dont call the clean_up func, then it works fine?is there a problem in cleanup?. but cleanup works fine with other signals,termination signal for example

And a question for execv?.Does not it start the new program from main?.When I call execv, it does not start from main again

2 Answers 2

8

Since you are on linux I think this is the cleanest way:

int main(int argc, char **argv) {
    /* your program here */

    /* if you want to restart call this */
    if (execv(argv[0], argv)) {
        /* ERROR, handle this yourself */
    }

    return 0;
}
7
  • Why the copying? Why don't you strdup() the members of argv[], too ? Commented Apr 18, 2012 at 9:20
  • @wildplasser: because we need a null-terminated array.
    – orlp
    Commented Apr 18, 2012 at 9:41
  • 1
    The last member of argv[] is guaranteed to be NULL. Commented Apr 18, 2012 at 9:42
  • @BarbarosKazan: if my answer solved your problem consider marking it as answered by clicking the green tickbox to the left of my question/
    – orlp
    Commented Apr 18, 2012 at 9:43
  • 1
    It will not work if you run the program not by its full path and execute chdir(SOME_OTHER_DIR_THAN_BEFORE) before call to execve.
    – JJJ
    Commented Apr 18, 2012 at 12:44
1

I'm assuming you're using some form of unix. There is an answer to a similar question here. That code is just rereading the config files, which is what people expect to happen when you sighup, rather than an actual restart. That said, if you want to restart, just replace the load_config() call with an exec. There are a few exec calls to choose from, so look at the manpages for exec or the execve to choose which one you want. You'll be passing the environment and argv pointers from the parent process to preserve them when you exec.

For whatever it's worth, I don't really see why you want to clean static arrays if you're going to restart the program and presumably just reinitialize them.

1
  • for example if an array int array[variable]; and then if I reinitialize this array,in the program again and the variable changes, does not the place where the array stay change?
    – barp
    Commented Apr 18, 2012 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.