getcontext und setcontext won't work in functions

makecontext
getcontext c
swapcontext
ucontext_t
setcontext react
mcontext_t
setcontext vs swapcontext
swapcontext with null

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.

setcontext, #include <ucontext.h> int getcontext(ucontext_t *ucp); int setcontext(const their own bookkeeping device, and a register variable won't do since registers are� getcontext und setcontext won't work in functions. Ask Question Asked 7 years ago. Active 6 months ago. Viewed 3k times 2. i am trying to copy the context of a thread

Cause you use a stack pointer address, which gonna disappear when the function ends. Use ucontext_t ** instead

getcontext(2) - Linux manual page - Michael Kerrisk, getcontext, setcontext - get or set the user context <ucontext.h> and the four functions getcontext(), setcontext(), makecontext(3) and The user has to invent her own bookkeeping device, and a register variable won't do since registers are � setcontext is one of a family of C library functions (the others being getcontext, makecontext and swapcontext) used for context control. The setcontext family allows the implementation in C of advanced control flow patterns such as iterators, fibers, and coroutines.

When you save the context using getcontext() all registers etc are stored in the provided context. This does mean that the content on the stack is saved - you only have one stack.

When you us setcontext() the registers are restored but you still have the same content on your stack. Since you have made a call to set_context() the return instruction pointer is on the stack, you set the content of all registers but the content on the stack remains the same.

setcontext(2): get/set user context, The getcontext()–setcontext() pair, the sigsetjmp()–siglongjmp() pair, the setjmp() –longjmp() pair, and the _setjmp()–_longjmp() pair cannot be intermixed. The function getcontext() initializes the structure pointed at by ucp to the currently active context. The function setcontext() restores the user context pointed at by ucp. A successful call does not return. The context should have been obtained by a call of getcontext(), or makecontext(3), or passed as third argument to a signal handler.

setcontext() — Restore user context, The context API provides a mechanism for components to 'talk' to each other without passing around data and functions as props, or dispatching lots of events. It's� The setcontext() function restores the user context pointed to by ucp. A successful call to setcontext() does not return; program execution resumes at the point specified by the ucp argument passed to setcontext(). The ucp argument should be created either by a prior call to getcontext(), or by being

Context API / setContext and getContext • Svelte Tutorial, getcontext, setcontext - get and set current user context The getcontext() function shall initialize the structure pointed to by ucp to the current user context of the� setcontext(2) sets the context of the calling process to the state stored in the ucontext struct pointed to by ucp. The struct must either have been created by getcontext(2) or have been passed as the third parameter of the sigaction(2) signal handler. The ucontext struct created by getcontext(2) is defined in <ucontext.h> as follows:

getcontext, The getcontext() and setcontext() functions are thread-safe. The user has to invent her own bookkeeping device, and a register variable won't do since� The function getcontext() initializes the structure pointed at by ucp to the currently active context. The function setcontext() restores the user context pointed at by ucp. A successful call does not return. The context should have been obtained by a call of getcontext(), or makecontext(3), or passed as third argument to a signal handler.

Comments
  • In practice it might appear to work with inline functions, but you're at the whim of the compiler. It's still undefined behavior.