Restarting my program

Discussion in 'C Programming' started by kevin donne, Feb 1, 2011.

  1. kevin donne

    kevin donne Guest

    Whatup,

    I have a program that sometimes I need to restart from within the program
    itself.

    Obviously I can store away argc and argv and just call main() again,
    however this does not reinitialize any static variables in my code and
    any libraries.

    How can I achieve this please? Is there some way with setjump()?

    TIA
    kevin donne, Feb 1, 2011
    #1
    1. Advertising

  2. kevin donne

    Willem Guest

    kevin donne wrote:
    ) I have a program that sometimes I need to restart from within the program
    ) itself.
    )
    ) Obviously I can store away argc and argv and just call main() again,
    ) however this does not reinitialize any static variables in my code and
    ) any libraries.
    )
    ) How can I achieve this please? Is there some way with setjump()?

    There's usually a system-dependent way of achieving this.
    For example, on posix systems, you can use exec() to achieve what you want.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Feb 1, 2011
    #2
    1. Advertising

  3. kevin donne

    kevin donne Guest

    Willem writes:
    > kevin donne wrote:
    > ) I have a program that sometimes I need to restart from within the
    > program ) itself.
    > )
    > ) Obviously I can store away argc and argv and just call main() again, )
    > however this does not reinitialize any static variables in my code and )
    > any libraries.
    > )
    > ) How can I achieve this please? Is there some way with setjump()?
    >
    > There's usually a system-dependent way of achieving this. For example,
    > on posix systems, you can use exec() to achieve what you want.


    Thanks Willelm, I'd really like a portable solution if possible though.
    kevin donne, Feb 1, 2011
    #3
  4. kevin donne

    Ian Collins Guest

    On 02/ 2/11 08:56 AM, kevin donne wrote:
    > Willem writes:
    >> kevin donne wrote:
    >> ) I have a program that sometimes I need to restart from within the
    >> program ) itself.
    >> )
    >> ) Obviously I can store away argc and argv and just call main() again, )
    >> however this does not reinitialize any static variables in my code and )
    >> any libraries.
    >> )
    >> ) How can I achieve this please? Is there some way with setjump()?
    >>
    >> There's usually a system-dependent way of achieving this. For example,
    >> on posix systems, you can use exec() to achieve what you want.

    >
    > Thanks Willelm, I'd really like a portable solution if possible though.


    There isn't one.

    It's probably easier to run your application in a wrapper script that
    checks the return code and restarts it on a non-zero value.

    --
    Ian Collins
    Ian Collins, Feb 1, 2011
    #4
  5. On Feb 1, 12:45 pm, kevin donne <> wrote:
    > Whatup,
    >
    > I have a program that sometimes I need to restart from within the program
    > itself.
    >
    > Obviously I can store away argc and argv and just call main() again,
    > however this does not reinitialize any static variables in my code and
    > any libraries.
    >
    > How can I achieve this please? Is there some way with setjump()?
    >
    > TIA


    You can use setjmp for this. But you may have to reorganize
    to get it to work right.

    [TOH: untested]

    jmp_buf restart_env;

    int main(int argc, char *argv[]) {
    if (setjmp(restart_env)) {;}
    /* before initializing anything */
    ....

    void restart_program(void) {
    longjmp(restart_env, 1);
    }

    For one thing, argc and argv have to
    stay unmodified throughout the program.
    To iterate through them, you must use
    an auxiliary variable.

    And even though we don't care here
    what value is returned by setjmp,
    it must be called within a condition-
    expression context.

    I'm sure there are other complications.
    luser- -droog, Feb 1, 2011
    #5
  6. kevin donne

    Willem Guest

    luser- -droog wrote:
    ) On Feb 1, 12:45?pm, kevin donne <> wrote:
    )> Whatup,
    )>
    )> I have a program that sometimes I need to restart from within the program
    )> itself.
    )>
    )> Obviously I can store away argc and argv and just call main() again,
    )> however this does not reinitialize any static variables in my code and
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )> any libraries.
    )>
    )> How can I achieve this please? Is there some way with setjump()?
    )>
    )> TIA
    )
    ) You can use setjmp for this. But you may have to reorganize
    ) to get it to work right.

    Since when does longjmp restore static variables ?


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Feb 1, 2011
    #6
  7. kevin donne

    Ian Collins Guest

    On 02/ 2/11 11:09 AM, luser- -droog wrote:
    > On Feb 1, 12:45 pm, kevin donne<> wrote:
    >> Whatup,
    >>
    >> I have a program that sometimes I need to restart from within the program
    >> itself.
    >>
    >> Obviously I can store away argc and argv and just call main() again,
    >> however this does not reinitialize any static variables in my code and
    >> any libraries.
    >>
    >> How can I achieve this please? Is there some way with setjump()?
    >>
    >> TIA

    >
    > You can use setjmp for this.


    No, you can't.

    --
    Ian Collins
    Ian Collins, Feb 1, 2011
    #7
  8. On Feb 1, 4:23 pm, Willem <> wrote:
    > luser- -droog wrote:
    >
    > ) On Feb 1, 12:45?pm, kevin donne <> wrote:
    > )> Whatup,
    > )>
    > )> I have a program that sometimes I need to restart from within the program
    > )> itself.
    > )>
    > )> Obviously I can store away argc and argv and just call main() again,
    > )> however this does not reinitialize any static variables in my code and
    >                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > )> any libraries.
    > )>
    > )> How can I achieve this please? Is there some way with setjump()?
    > )>
    > )> TIA
    > )
    > ) You can use setjmp for this. But you may have to reorganize
    > ) to get it to work right.
    >
    > Since when does longjmp restore static variables ?
    >


    Hence the part about "reorganizing". OP will have to explicitly
    initialize these after the setjmp call in main.
    luser- -droog, Feb 2, 2011
    #8
  9. kevin donne

    Dr Nick Guest

    luser- -droog <> writes:

    > On Feb 1, 4:23 pm, Willem <> wrote:
    >> luser- -droog wrote:
    >>
    >> ) On Feb 1, 12:45?pm, kevin donne <> wrote:
    >> )> Whatup,
    >> )>
    >> )> I have a program that sometimes I need to restart from within the program
    >> )> itself.
    >> )>
    >> )> Obviously I can store away argc and argv and just call main() again,
    >> )> however this does not reinitialize any static variables in my code and
    >>                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >> )> any libraries.
    >> )>
    >> )> How can I achieve this please? Is there some way with setjump()?
    >> )>
    >> )> TIA
    >> )
    >> ) You can use setjmp for this. But you may have to reorganize
    >> ) to get it to work right.
    >>
    >> Since when does longjmp restore static variables ?
    >>

    >
    > Hence the part about "reorganizing". OP will have to explicitly
    > initialize these after the setjmp call in main.


    In which case he might as well "just":
    * replace all static variables with globals (perhaps in a structure)
    * write an "initialise global state" function
    * make the main program into a tiny one that checks (NB, checks, not
    reads) its parameters and environment as appropriate and saves argc and
    argv and then calls do_everything(global.argc,global.argv)
    * call do_everything from anywhere when you need to start again

    If this starts causing the stack to grow enormously, then some sort of
    return up the chain or - at that stage - setjmp/longjump may be called for.

    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
    Dr Nick, Feb 2, 2011
    #9
  10. On Feb 2, 1:34 am, Dr Nick <> wrote:
    > luser- -droog <> writes:
    > > On Feb 1, 4:23 pm, Willem <> wrote:
    > >> luser- -droog wrote:

    >
    > >> ) On Feb 1, 12:45?pm, kevin donne <> wrote:
    > >> )> Whatup,
    > >> )>
    > >> )> I have a program that sometimes I need to restart from within the program
    > >> )> itself.
    > >> )>
    > >> )> Obviously I can store away argc and argv and just call main() again,
    > >> )> however this does not reinitialize any static variables in my code and
    > >>                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > >> )> any libraries.
    > >> )>
    > >> )> How can I achieve this please? Is there some way with setjump()?
    > >> )>
    > >> )> TIA
    > >> )
    > >> ) You can use setjmp for this. But you may have to reorganize
    > >> ) to get it to work right.

    >
    > >> Since when does longjmp restore static variables ?

    >
    > > Hence the part about "reorganizing". OP will have to explicitly
    > > initialize these after the setjmp call in main.

    >
    > In which case he might as well "just":
    > * replace all static variables with globals (perhaps in a structure)
    > * write an "initialise global state" function
    > * make the main program into a tiny one that checks (NB, checks, not
    > reads) its parameters and environment as appropriate and saves argc and
    > argv and then calls do_everything(global.argc,global.argv)
    > * call do_everything from anywhere when you need to start again
    >
    > If this starts causing the stack to grow enormously, then some sort of
    > return up the chain or - at that stage - setjmp/longjump may be called for.
    >


    Yeah. Pretty much. He'll probably also need to destroy all structures,
    flush output files, reset terminal. The controlling script idea is
    starting to sound easier. But none of this sounds impossible.
    luser- -droog, Feb 2, 2011
    #10
  11. luser- -droog <> writes:

    > On Feb 2, 1:34 am, Dr Nick <> wrote:
    >> luser- -droog <> writes:
    >> > On Feb 1, 4:23 pm, Willem <> wrote:
    >> >> luser- -droog wrote:

    >>
    >> >> ) On Feb 1, 12:45?pm, kevin donne <> wrote:
    >> >> )> Whatup,
    >> >> )>
    >> >> )> I have a program that sometimes I need to restart from within the program
    >> >> )> itself.
    >> >> )>
    >> >> )> Obviously I can store away argc and argv and just call main() again,
    >> >> )> however this does not reinitialize any static variables in my code and
    >> >>                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >> >> )> any libraries.
    >> >> )>
    >> >> )> How can I achieve this please? Is there some way with setjump()?
    >> >> )>
    >> >> )> TIA
    >> >> )
    >> >> ) You can use setjmp for this. But you may have to reorganize
    >> >> ) to get it to work right.

    >>
    >> >> Since when does longjmp restore static variables ?

    >>
    >> > Hence the part about "reorganizing". OP will have to explicitly
    >> > initialize these after the setjmp call in main.

    >>
    >> In which case he might as well "just":
    >> * replace all static variables with globals (perhaps in a structure)
    >> * write an "initialise global state" function
    >> * make the main program into a tiny one that checks (NB, checks, not
    >> reads) its parameters and environment as appropriate and saves argc and
    >> argv and then calls do_everything(global.argc,global.argv)
    >> * call do_everything from anywhere when you need to start again
    >>
    >> If this starts causing the stack to grow enormously, then some sort of
    >> return up the chain or - at that stage - setjmp/longjump may be called for.

    >
    > Yeah. Pretty much. He'll probably also need to destroy all structures,
    > flush output files, reset terminal. The controlling script idea is
    > starting to sound easier. But none of this sounds impossible.


    With C99, I don't think it is possible to return the program to it's
    initial sate. For example, stream orientation is not determined at
    start-up and can be set by the first IO operation on the stream, but
    there is no way to set the orientation back to this unspecified state.

    In addition to the huge lists of things you'd have to reset (and you are
    bound to forget something) there is the problem of atexit handlers.

    Having said all that, if the program is well-designed it is possible
    that writing an initialisation function and putting a loop round the
    body of main would fit the bill. Some program assume very little about
    their initial state.

    --
    Ben.
    Ben Bacarisse, Feb 2, 2011
    #11
  12. kevin donne

    Tom St Denis Guest

    On Feb 1, 2:20 pm, Willem <> wrote:
    > kevin donne wrote:
    >
    > ) I have a program that sometimes I need to restart from within the program
    > ) itself.
    > )
    > ) Obviously I can store away argc and argv and just call main() again,
    > ) however this does not reinitialize any static variables in my code and
    > ) any libraries.
    > )
    > ) How can I achieve this please? Is there some way with setjump()?
    >
    > There's usually a system-dependent way of achieving this.
    > For example, on posix systems, you can use exec() to achieve what you want.


    If you're crafty enough you can actually exec() yourself with a new
    argv[0] value, detect that. Then when you want to restart just exit()
    from the process with a code that tells the parent "restart", e.g.
    exit(123); Then the parent can just exec() itself again.

    Bonus points if if you daemon() the parent process.

    Tom
    Tom St Denis, Feb 2, 2011
    #12
  13. kevin donne

    Willem Guest

    Tom St Denis wrote:
    ) On Feb 1, 2:20?pm, Willem <> wrote:
    )> kevin donne wrote:
    )>
    )> ) I have a program that sometimes I need to restart from within the program
    )> ) itself.
    )> )
    )> ) Obviously I can store away argc and argv and just call main() again,
    )> ) however this does not reinitialize any static variables in my code and
    )> ) any libraries.
    )> )
    )> ) How can I achieve this please? Is there some way with setjump()?
    )>
    )> There's usually a system-dependent way of achieving this.
    )> For example, on posix systems, you can use exec() to achieve what you want.
    )
    ) If you're crafty enough you can actually exec() yourself with a new
    ) argv[0] value, detect that. Then when you want to restart just exit()
    ) from the process with a code that tells the parent "restart", e.g.
    ) exit(123); Then the parent can just exec() itself again.
    )
    ) Bonus points if if you daemon() the parent process.

    lolwut ?
    exec() replaces the process, it doesn't create a new one.

    Just exec() yourself, that fits the requirements perfectly.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Feb 2, 2011
    #13
  14. kevin donne

    Tom St Denis Guest

    On Feb 2, 11:16 am, Willem <> wrote:
    > Tom St Denis wrote:
    >
    > ) On Feb 1, 2:20?pm, Willem <> wrote:)> kevin donne wrote:
    >
    > )>
    > )> ) I have a program that sometimes I need to restart from within the program
    > )> ) itself.
    > )> )
    > )> ) Obviously I can store away argc and argv and just call main() again,
    > )> ) however this does not reinitialize any static variables in my code and
    > )> ) any libraries.
    > )> )
    > )> ) How can I achieve this please? Is there some way with setjump()?
    > )>
    > )> There's usually a system-dependent way of achieving this.
    > )> For example, on posix systems, you can use exec() to achieve what you want.
    > )
    > ) If you're crafty enough you can actually exec() yourself with a new
    > ) argv[0] value, detect that.  Then when you want to restart just exit()
    > ) from the process with a code that tells the parent "restart", e.g.
    > ) exit(123);  Then the parent can just exec() itself again.
    > )
    > ) Bonus points if if you daemon() the parent process.
    >
    > lolwut ?
    > exec() replaces the process, it doesn't create a new one.


    Well I assumed you'd fork() after daemonizing but before
    exec(). ... Rube Goldberg.

    > Just exec() yourself, that fits the requirements perfectly.


    You would have to save the parameters for main for that to work
    though.

    Tom
    Tom St Denis, Feb 2, 2011
    #14
  15. Ben Bacarisse <> writes:

    > luser- -droog <> writes:
    >
    >> On Feb 2, 1:34 am, Dr Nick <> wrote:
    >>> luser- -droog <> writes:
    >>> > On Feb 1, 4:23 pm, Willem <> wrote:
    >>> >> luser- -droog wrote:
    >>>
    >>> >> ) On Feb 1, 12:45?pm, kevin donne <> wrote:
    >>> >> )> Whatup,
    >>> >> )>
    >>> >> )> I have a program that sometimes I need to restart from within
    >>> >> the program
    >>> >> )> itself.
    >>> >> )>
    >>> >> )> Obviously I can store away argc and argv and just call main() again,
    >>> >> )> however this does not reinitialize any static variables in my code and
    >>> >>                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >>> >> )> any libraries.
    >>> >> )>
    >>> >> )> How can I achieve this please? Is there some way with setjump()?
    >>> >> )>
    >>> >> )> TIA
    >>> >> )
    >>> >> ) You can use setjmp for this. But you may have to reorganize
    >>> >> ) to get it to work right.
    >>>
    >>> >> Since when does longjmp restore static variables ?
    >>>
    >>> > Hence the part about "reorganizing". OP will have to explicitly
    >>> > initialize these after the setjmp call in main.
    >>>
    >>> In which case he might as well "just":
    >>> * replace all static variables with globals (perhaps in a structure)
    >>> * write an "initialise global state" function
    >>> * make the main program into a tiny one that checks (NB, checks, not
    >>> reads) its parameters and environment as appropriate and saves argc and
    >>> argv and then calls do_everything(global.argc,global.argv)
    >>> * call do_everything from anywhere when you need to start again
    >>>
    >>> If this starts causing the stack to grow enormously, then some sort of
    >>> return up the chain or - at that stage - setjmp/longjump may be called for.

    >>
    >> Yeah. Pretty much. He'll probably also need to destroy all structures,
    >> flush output files, reset terminal. The controlling script idea is
    >> starting to sound easier. But none of this sounds impossible.

    >
    > With C99, I don't think it is possible to return the program to it's
    > initial sate. For example, stream orientation is not determined at
    > start-up and can be set by the first IO operation on the stream, but
    > there is no way to set the orientation back to this unspecified state.


    You can do this both in C an C++ :

    test.c:
    -------
    static int i = 0;
    int main()
    {
    if (1)
    #include "test2.c"
    return 0;
    }

    test2.c:
    --------
    static int i = 1;


    This should restore your program to any state but there
    are other ways to do it (e.g. managing your stack.)

    >
    > In addition to the huge lists of things you'd have to reset (and you are
    > bound to forget something) there is the problem of atexit handlers.
    >


    You can also reload your environment.
    int main(int argc, char *argv[], char **envp)
    { return 0; }

    > Having said all that, if the program is well-designed it is possible
    > that writing an initialisation function and putting a loop round the
    > body of main would fit the bill. Some program assume very little about
    > their initial state.


    This is where goto comes in.

    TW
    Turtle Wizard, Feb 1, 2012
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. LJ
    Replies:
    0
    Views:
    1,109
  2. Mark Feather

    Problems after restarting IIS ????

    Mark Feather, Jul 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    454
    Mark Feather
    Jul 14, 2003
  3. Geoldr

    Restarting a program

    Geoldr, May 22, 2008, in forum: Python
    Replies:
    4
    Views:
    260
    Geoldr
    May 22, 2008
  4. pgodfrin

    Restarting a Program mid stream

    pgodfrin, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    88
    pgodfrin
    Nov 25, 2008
  5. Peace
    Replies:
    3
    Views:
    83
    dieter
    Mar 22, 2014
Loading...

Share This Page