C语言中的切换上下文
我正在学习如何在C中使用ucontext,并编写了以下代码。我希望使用一个无限while循环来观察两个上下文(ctx_main和ctx_thread)之间的切换。但是,上下文似乎卡在while循环中。我正在寻找一些关于更正代码的帮助。我将我的评论放在下面的代码中:C语言中的切换上下文,c,context-switch,C,Context Switch,我正在学习如何在C中使用ucontext,并编写了以下代码。我希望使用一个无限while循环来观察两个上下文(ctx_main和ctx_thread)之间的切换。但是,上下文似乎卡在while循环中。我正在寻找一些关于更正代码的帮助。我将我的评论放在下面的代码中: #include <stdlib.h> #include <stdio.h> #include <string.h> #include <ucontext.h> #define ME
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ucontext.h>
#define MEM 64000
ucontext_t ctx_main, ctx_thread;
static int thread_id = 0;
/* the function thread_init() will initialize the ctx_main context
* this is the first function to be called in main() */
void thread_init()
{
getcontext(&ctx_main);
ctx_main.uc_link = 0;
ctx_main.uc_stack.ss_sp = malloc(MEM);
ctx_main.uc_stack.ss_size = MEM;
ctx_main.uc_stack.ss_flags = 0;
printf("printed in thread_init()\n");
}
/* This function revert_main is supposed to increment the global variable thread_id,
* then switch back to ctx_main context */
void *revert_main(void *n)
{
thread_id += *(int *) n;
printf("printed in the revert_main()\n");
swapcontext(&ctx_thread, &ctx_main); /* now switch back to ctx_main context */
}
int main()
{
thread_init(); /* Initialize the ctx_main context */
getcontext(&ctx_thread); /* Initialize the ctx_thread context */
ctx_thread.uc_link = 0;
ctx_thread.uc_stack.ss_sp = malloc(MEM);
ctx_thread.uc_stack.ss_size = MEM;
ctx_thread.uc_stack.ss_flags = 0;
int *j = (int *) malloc(sizeof(int));
*j = 1;
while(1) /* Infinite loop to switch between ctx_main and ctx_thread */
{
printf("printed in the main while loop\n");
printf("the thread id is %d\n", thread_id);
makecontext(&ctx_thread, (void *)&revert_main, 1, (void *)j); /* hopefully this will trigger the revert_main() function */
}
return 0;
}
#包括
#包括
#包括
#包括
#定义内存64000
ucontext_t ctx_主线程,ctx_线程;
静态int线程_id=0;
/*函数thread_init()将初始化ctx_主上下文
*这是在main()中调用的第一个函数*/
void thread_init()
{
getcontext(&ctx_-main);
ctx_main.uc_link=0;
ctx_main.uc_stack.ss_sp=malloc(MEM);
ctx_main.uc_stack.ss_size=MEM;
ctx_main.uc_stack.ss_标志=0;
printf(“在线程_init()\n中打印”);
}
/*此函数revert_main应递增全局变量thread_id,
*然后切换回ctx_主上下文*/
void*revert_main(void*n)
{
线程id+=*(int*)n;
printf(“在revert_main()\n中打印”);
swapcontext(&ctx_线程,&ctx_主上下文);/*现在切换回ctx_主上下文*/
}
int main()
{
线程_init();/*初始化ctx_主上下文*/
getcontext(&ctx_线程);/*初始化ctx_线程上下文*/
ctx_thread.uc_link=0;
ctx_thread.uc_stack.ss_sp=malloc(MEM);
ctx_thread.uc_stack.ss_size=MEM;
ctx_thread.uc_stack.ss_flags=0;
int*j=(int*)malloc(sizeof(int));
*j=1;
而(1)/*无限循环用于在ctx_主线程和ctx_线程之间切换*/
{
printf(“在主while循环中打印\n”);
printf(“线程id为%d\n”,线程id);
makecontext(&ctx_-thread,(void*)&revert_-main,1,(void*)j);/*希望这将触发revert_-main()函数*/
}
返回0;
}
您的主例程设置了一个新的上下文,但从不切换到该上下文,因此revert\u main
从不运行
对于给定的u_上下文对象,只需调用一次
makecontext
。因此,将对makecontext
的调用移出循环。如果您想使用线程,那么使用POSIX线程库几乎肯定会更好。跳过ucontext不会遗漏任何内容。不,线程只是一个名称,我不需要使用pthread库。谢谢你,克里斯·多德。makecontext是否也将函数附加到该上下文?make_context
仅设置上下文的pc和堆栈指针,以便在切换到时,它将在大部分空堆栈上运行函数(仅包含指定的参数)。