Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Restarting my program

Reply
Thread Tools

Restarting my program

 
 
kevin donne
Guest
Posts: n/a
 
      02-01-2011
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
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      02-01-2011
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
 
Reply With Quote
 
 
 
 
kevin donne
Guest
Posts: n/a
 
      02-01-2011
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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-01-2011
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
 
Reply With Quote
 
luser- -droog
Guest
Posts: n/a
 
      02-01-2011
On Feb 1, 12:45*pm, kevin donne <(E-Mail Removed)> 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.
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      02-01-2011
luser- -droog wrote:
) On Feb 1, 12:45?pm, kevin donne <(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-01-2011
On 02/ 2/11 11:09 AM, luser- -droog wrote:
> On Feb 1, 12:45 pm, kevin donne<(E-Mail Removed)> 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
 
Reply With Quote
 
luser- -droog
Guest
Posts: n/a
 
      02-02-2011
On Feb 1, 4:23*pm, Willem <(E-Mail Removed)> wrote:
> luser- -droog wrote:
>
> ) On Feb 1, 12:45?pm, kevin donne <(E-Mail Removed)> 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.

 
Reply With Quote
 
Dr Nick
Guest
Posts: n/a
 
      02-02-2011
luser- -droog <(E-Mail Removed)> writes:

> On Feb 1, 4:23*pm, Willem <(E-Mail Removed)> wrote:
>> luser- -droog wrote:
>>
>> ) On Feb 1, 12:45?pm, kevin donne <(E-Mail Removed)> 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
 
Reply With Quote
 
luser- -droog
Guest
Posts: n/a
 
      02-02-2011
On Feb 2, 1:34*am, Dr Nick <(E-Mail Removed)> wrote:
> luser- -droog <(E-Mail Removed)> writes:
> > On Feb 1, 4:23*pm, Willem <(E-Mail Removed)> wrote:
> >> luser- -droog wrote:

>
> >> ) On Feb 1, 12:45?pm, kevin donne <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Restarting a Program mid stream pgodfrin Perl Misc 7 11-25-2008 09:23 PM
Restarting a program Geoldr Python 4 05-22-2008 07:32 PM
computer keep restarting. =?Utf-8?B?ZGV3YQ==?= Wireless Networking 2 03-13-2005 01:01 AM
restarting =?Utf-8?B?Y2hyaXM=?= Microsoft Certification 1 04-04-2004 05:53 PM
Restarting the tray icon James Messick Firefox 6 11-15-2003 01:09 AM



Advertisments