"263" <[EMAIL PROTECTED]> writes:


> Hi, at first, i must thank Raju. K.V. for his answer.
> The reason why I read the setjmp.h file is that I want to write a 
> program which will do the following:
> At one checkpoint, the program copies the stack of the process to a 
> file. Then at the next checkpoint, the program will decide whether 
> restore the stack from the former file or not according to some 
> conditions.
> At first I think setjmp/longjmp can do the job, but after some work, I 
> find that setjmp/longjmp can only restore the stack context, but not 
> content of the stack. Then how can I copy and restore the stack?

The following is a quicky example that saves the stack.
The harder restoring, and context is left as an exercise.
Note:  None of this is truly portable, but with care you can
get it run on a fair variety of machines.

If you need an example I would suggest guile (or some other
interpreter that implements continuations in C).  Heck.  I
just about recommend guile anyway, or some other scheme dialect
because saving/restoring the stack is a builtin language construct.

But note.  Between one program invocation and another it is quite
possible nothing will work,  Changes in library version or, resulting
in code changing addresses, etc.

Eric


char *stack_bottom;
int main(void)
{
        char x;
        stack_bottom = &x;
        save_stack();
}

void save_stack()
{
        char x;
        really_save_stack(stack_bottom, &x);
}

void really_save_stack(char *bottom, char *top)
{
        fp = fopen(....)
        if (bottom > top) {
                fwrite(top, bottom - top, 1, fp);
        } else {
                fwrite(bottom, top - bottom, 1, fp);
        }
        fclose(fp);
}

> 
> Thanks in advance.
> 
> Fred
> 

Reply via email to