setcontext()分段错误
我正在尝试使用C中的上下文切换创建一个自定义pthread库。我在调用setcontext()时遇到了分段错误的问题-关于这个函数的文档似乎很有限,所以我无法真正弄清楚到底发生了什么,这已经花费了我大量的停机时间。我的代码:setcontext()分段错误,c,segmentation-fault,C,Segmentation Fault,我正在尝试使用C中的上下文切换创建一个自定义pthread库。我在调用setcontext()时遇到了分段错误的问题-关于这个函数的文档似乎很有限,所以我无法真正弄清楚到底发生了什么,这已经花费了我大量的停机时间。我的代码: #include <signal.h> #include <ucontext.h> struct mypthread_t{ int id; int pid; int isRunning; mypthread_t *next;
#include <signal.h>
#include <ucontext.h>
struct mypthread_t{
int id;
int pid;
int isRunning;
mypthread_t *next;
ucontext_t* context;
};
int mypthread_create(mypthread_t *thread, const mypthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
{
printf("Hello\n");
thread->id = threadID++;
thread->context = malloc(sizeof(ucontext_t));
thread->context->uc_stack.ss_sp = malloc(16384);
thread->context->uc_stack.ss_size = 16384;
thread->context->uc_link = 0;
makecontext(thread->context, (void *) start_routine, 1, arg);
setcontext(thread->context);
}
在调用
makecontext(3)
之前,通过调用getcontext(3)
初始化ucontext\t
对象:
fldenv (%rcx)
if (getcontext(thread->context) == -1) {
// ... error handling
// ... errno is set appropriately to indicate the error that occurred
}
makecontext(...);