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
|
|
0
|
|
|
|
Reply
|
kevin
|
2/1/2011 6:45:54 PM |
|
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
|
|
0
|
|
|
|
Reply
|
Willem
|
2/1/2011 7:20:25 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
kevin
|
2/1/2011 7:56:44 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
Ian
|
2/1/2011 8:03:48 PM
|
|
On Feb 1, 12:45=A0pm, 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.
[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.
|
|
0
|
|
|
|
Reply
|
luser
|
2/1/2011 10:09:49 PM
|
|
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 ?
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
|
|
0
|
|
|
|
Reply
|
Willem
|
2/1/2011 10:23:40 PM
|
|
On 02/ 2/11 11:09 AM, 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.
No, you can't.
--
Ian Collins
|
|
0
|
|
|
|
Reply
|
Ian
|
2/1/2011 10:27:34 PM
|
|
On Feb 1, 4:23=A0pm, 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 prog=
ram
> )> 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
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0^^^^^^^^^^^^^^^^^^^^^^=
^^^^^^^^^^^
> )> 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.
|
|
0
|
|
|
|
Reply
|
luser
|
2/2/2011 2:52:51 AM
|
|
luser- -droog <mijoryx@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.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
|
|
0
|
|
|
|
Reply
|
Dr
|
2/2/2011 7:34:12 AM
|
|
On Feb 2, 1:34=A0am, Dr Nick <3-nos...@temporary-address.org.uk> wrote:
> luser- -droog <mijo...@yahoo.com> writes:
> > On Feb 1, 4:23=A0pm, 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 p=
rogram
> >> )> 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
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0^^^^^^^^^^^^^^^^^^^=
^^^^^^^^^^^^^^
> >> )> 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 fo=
r.
>
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.
|
|
0
|
|
|
|
Reply
|
luser
|
2/2/2011 8:12:40 AM
|
|
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.
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.
|
|
0
|
|
|
|
Reply
|
Ben
|
2/2/2011 2:17:09 PM
|
|
On Feb 1, 2:20=A0pm, Willem <wil...@turtle.stack.nl> wrote:
> kevin donne wrote:
>
> ) I have a program that sometimes I need to restart from within the progr=
am
> ) 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 wan=
t.
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
|
|
0
|
|
|
|
Reply
|
Tom
|
2/2/2011 3:59:23 PM
|
|
Tom St Denis wrote:
) On Feb 1, 2:20?pm, Willem <wil...@turtle.stack.nl> 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
|
|
0
|
|
|
|
Reply
|
Willem
|
2/2/2011 4:16:01 PM
|
|
On Feb 2, 11:16=A0am, Willem <wil...@turtle.stack.nl> wrote:
> Tom St Denis wrote:
>
> ) On Feb 1, 2:20?pm, Willem <wil...@turtle.stack.nl> wrote:)> kevin donne=
wrote:
>
> )>
> )> ) I have a program that sometimes I need to restart from within the pr=
ogram
> )> ) 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 a=
nd
> )> ) 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. =A0Then when you want to restart just exit(=
)
> ) from the process with a code that tells the parent "restart", e.g.
> ) exit(123); =A0Then 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
|
|
0
|
|
|
|
Reply
|
Tom
|
2/2/2011 4:59:38 PM
|
|
Willem <willem@turtle.stack.nl> writes:
> Tom St Denis wrote:
> ) On Feb 1, 2:20?pm, Willem <wil...@turtle.stack.nl> 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.
replaces? To replace insinuates a new one...
|
|
0
|
|
|
|
Reply
|
Richard
|
2/2/2011 5:21:58 PM
|
|
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
|
|
0
|
|
|
|
Reply
|
0wl256NOSPAM (1)
|
2/1/2012 12:10:03 PM
|
|
|
15 Replies
94 Views
(page loaded in 0.199 seconds)
|