Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #16601

Re: Restarting my program

From Turtle Wizard <0wl256NOSPAM@gmail.com>
Newsgroups comp.lang.c
Subject Re: Restarting my program
Organization Faerun
References (2 earlier) <slrnikh1vc.5ss.willem@turtle.stack.nl> <1d38c886-a329-47ed-b63c-a04ba436f334@o1g2000yqb.googlegroups.com> <87hbcmx53f.fsf@temporary-address.org.uk> <45022d16-495b-43f8-a912-ed4d127bfc29@r16g2000yql.googlegroups.com> <0.e74a9dacbf2bec613014.20110202141709GMT.87zkqe34ii.fsf@bsb.me.uk>
Date 2012-02-01 21:10 +0900
Message-ID <sa6ty3ahi04.fsf@gmail.com> (permalink)

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> luser- -droog <mijoryx@yahoo.com> writes:
>
>> On Feb 2, 1:34 am, Dr Nick <3-nos...@temporary-address.org.uk> wrote:
>>> luser- -droog <mijo...@yahoo.com> writes:
>>> > On Feb 1, 4:23 pm, Willem <wil...@turtle.stack.nl> wrote:
>>> >> luser- -droog wrote:
>>>
>>> >> ) On Feb 1, 12:45?pm, kevin donne <nos...@nospam.com> 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

Back to comp.lang.c | Previous | Next | Find similar | Unroll thread


Thread

Re: Restarting my program Turtle Wizard <0wl256NOSPAM@gmail.com> - 2012-02-01 21:10 +0900

csiph-web