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

barp
  • 6,489
  • 9
  • 30
  • 37

2 Answers2

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;
}
orlp
  • 112,504
  • 36
  • 218
  • 315
  • Why the copying? Why don't you strdup() the members of argv[], too ? – wildplasser Apr 18 '12 at 09:20
  • @wildplasser: because we need a null-terminated array. – orlp Apr 18 '12 at 09:41
  • 1
    The last member of argv[] is guaranteed to be NULL. – wildplasser Apr 18 '12 at 09: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 Apr 18 '12 at 09:43
  • Be careful when using the `restart_process()` from a signal handler. `malloc` and `execv` are not defined as reentrant by POSIX.1 - which means that when you call them in a signal handler undefined things might happen (like internal data structure corruption). For further information you can e.g. consult 'Advanced Programming in a UNIX Environment', W. Richard Stevens, Addison-Weseley, 18 pr. 1999, p. 278. – Andreas Florath Apr 18 '12 at 09:51
  • An alternative may be to just have the process killed by HUP. If the process is self-restarting with fork (like [this](http://stackoverflow.com/a/6538718/572743)) then that will go seamlessly. This has the advantage that you can even decide to keep _some_ data (anything you did before the first `fork`). – Damon Apr 18 '12 at 10:55
  • 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 Apr 18 '12 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.

Community
  • 1
  • 1
huntse
  • 11
  • 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 Apr 18 '12 at 09:30