Ucontext in linux

10,817

ucontext.h defines functions (setcontext, getcontext, makecontext, and swapcontext) and structures (ucontext_t and mcontext_t) that allow the programmer to save and restore the program context.

Using these functions, a programmer can implement advanced flow control schemes, such as coroutines or lightweight user threads.

A good amount of detail (including an example of implementing coroutines) is given in the GNU C Library: Complete Context Control.

It should be noted ucontext.h was obsoleted in POSIX.1-2008 ("IEEE Std 1003.1-2001/Cor 2-2004, item XBD/TC2/D6/28 is applied, updating the getcontext(), makecontext(), setcontext(), and swapcontext() functions to be obsolescent." on the ucontext.h page ).)

Share:
10,817

Related videos on Youtube

Embedded Programmer
Author by

Embedded Programmer

Learning linux programming.

Updated on June 04, 2022

Comments

  • Embedded Programmer
    Embedded Programmer almost 2 years

    I read that ucontext is used to save context between multiple threads in linux. Since the OS does the context switching between different threads, why does linux provide this header file (ucontext.h) for context switching?

  • Jean-Michaël Celerier
    Jean-Michaël Celerier almost 11 years
    Most serious userland threads would use setjmp() / longjmp() I think now.
  • Embedded Programmer
    Embedded Programmer almost 11 years
    does it mean that ucontext.h is provided to make own threads in program? it depends upon user whether he wants to make own threads or not. what is problem with setjmp/longjmp in program?
  • user1095108
    user1095108 about 2 years
    setjmp/longjmp unwind the stack with msvc.