i am trying to copy the context of a thread including the stack to create a checkpoint, which i can restore later on. For that reason i tried to move the call of getcontext and setcontext into a function which also saves the stack, but that wont work.
Working example from wikipedia:
#include <stdio.h>
#include <ucontext.h>
#include <unistd.h>
int main(int argc, const char *argv[]){
ucontext_t context;
getcontext(&context);
puts("Hello world");
sleep(1);
setcontext(&context);
return 0;
}
This just repeatingly prints "Hello world".
I would like to do something like:
#include <stdio.h>
#include <ucontext.h>
#include <unistd.h>
void set_context(ucontext_t * ct)
{
setcontext(ct);
}
void get_context(ucontext_t * ct)
{
getcontext(ct);
}
int main()
{
ucontext_t context;
get_context(&context);
puts("Hello world");
sleep(1);
set_context(&context);
return 0;
}
But this just prints "Hello world" once and exits.
Now I am stuck. Thanks in advance.
The saved context is invalid once the function that called getcontext
returns. This is explained in the documentation for these functions.