Question

I am studying context switching in C programming and have found the following example code on the Internet. I am trying to figure out whether only makecontext() function can trigger a function that does something. The other functions such as setcontext(), getcontext(), and swapcontext() are used for setting the context.

The makecontext() attaches a function and its parameter(s) to a context, does the function stick to the context all the time until a change is committed to it?

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <ucontext.h>
  4 #define MEM 64000
  5 
  6 ucontext_t T1, T2, Main;
  7 ucontext_t a;
  8 
  9 int fn1()
 10 {
 11   printf("this is from 1\n");
 12   setcontext(&Main);
 13 }
 14 
 15 void fn2()
 16 {
 17   printf("this is from 2\n");
 18   setcontext(&a);
 19   printf("finished 1\n");
 20 }
 21 
 22 void start()
 23 {
 24   getcontext(&a);
 25   a.uc_link=0;
 26   a.uc_stack.ss_sp=malloc(MEM);
 27   a.uc_stack.ss_size=MEM;
 28   a.uc_stack.ss_flags=0;
 29   makecontext(&a, (void*)&fn1, 0);
 30 }
 31 
 32 int main(int argc, char *argv[])
 33 {
 34   start();
 35   getcontext(&Main);
 36   getcontext(&T1);
 37   T1.uc_link=0;
 38   T1.uc_stack.ss_sp=malloc(MEM);
 39   T1.uc_stack.ss_size=MEM;
 40   makecontext(&T1, (void*)&fn1, 0);
 41   swapcontext(&Main, &T1);
 42   getcontext(&T2);
 43   T2.uc_link=0;
 44   T2.uc_stack.ss_sp=malloc(MEM);
 45   T2.uc_stack.ss_size=MEM;
 46   T2.uc_stack.ss_flags=0;
 47   makecontext(&T2, (void*)&fn2, 0);
 48   swapcontext(&Main, &T2);
 49   printf("completed\n");
 50   exit(0);
 51 }
Was it helpful?

Solution

makecontext writes the function info into a context, and it will remain there until it is overwritten by something else. getcontext overwites the entire context, so would overwrite any function written there by a previous call to makecontext. Likewise, swapcontext completely overwrites the context pointed at by its first argument.

The basic idea is that a u_context contains a snapshot of part of the process context at some time. It contains all the machine registers, stack info, and signal masks. It does NOT include any memory map or file descriptor state. The state in the u_context is precisely all the state you need to manipulate in order to implement threads or coroutines.

edit

swapcontext(&current, &another) saves the current context in current and switches to another. At some later point, the code (runnning from another context) may switch back to current (with another call to swapcontext) or it might switch to some third context. When a context ends (the function set into it with makecontext returns), if some context is pointed at by its uc_link field, it will switch to that context. But if uc_link is NULL, then the thread (and process if there's only one thread) will exit -- other contexts that are not running will just be abandoned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top