Restarting my program

K

kevin donne

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
 
W

Willem

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
 
K

kevin donne

Willem said:
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.
 
I

Ian Collins

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.
 
L

luser- -droog

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.
 
W

Willem

luser- -droog 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
 
L

luser- -droog

luser- -droog 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.
 
D

Dr Nick

luser- -droog said:
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.
 
L

luser- -droog

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.
 
B

Ben Bacarisse

luser- -droog said:
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.
 
T

Tom St Denis

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
 
W

Willem

Tom St Denis 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
 
T

Tom St Denis

Tom St Denis 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
 
T

Turtle Wizard

Ben Bacarisse said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top