I am trying to call getcontext into another function (instead of calling it directly into main) in order to copy the thread's stack and restore it later. This code should print repeatedly, but it won't work once the function that called getcontext returns.
Is there a way to circumvent this limitation and call getcontext in another function (other than inline macros)?
#include <stdio.h>
#include <ucontext.h>
#include <unistd.h>
ucontext_t context;
void set_context() {
setcontext(&context);
}
void get_context() {
getcontext(&context);
}
int main() {
get_context();
puts("Hello world");
sleep(1);
set_context();
return 0;
}