Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart program in C

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

like image 405
barp Avatar asked Apr 18 '12 09:04

barp


1 Answers

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;
}
like image 94
orlp Avatar answered Sep 26 '22 16:09

orlp