在线程库中使用 getcontext() 出现分段错误 [英] Segmentation Fault using getcontext() in thread library
问题描述
我正在尝试使用系统调用(例如获取上下文、交换上下文等)在 C 中实现用户级线程库
I am trying to implement a user level thread library in C using systems calls such as get context, swap context , etc
我有一个看起来像这样的线程控制块:
I have a thread control block that looks like this :
struct tcb {
int thread_id;
int thread_pri;
ucontext_t *thread_context;
struct tcb *next;
}
我有一个名为 init() 的函数,它看起来像这样:
And I have a function called init() that looks like this:
void t_init()
{
tcb *tmp;
tmp = malloc(sizeof(tcb));
getcontext(tmp->thread_context); /* let tmp be the context of main() */
running_head = tmp;
}
我使用了 gdb,并且在运行时在 getcontext(tmp->thread_context) 函数中遇到了分段错误.
I used gdb and I got a segmentation fault during runtime at the getcontext(tmp->thread_context) function.
我已阅读 getcontext() 的手册页,但不确定为什么这会向我返回分段错误!
I have read the man pages for getcontext() but am unsure as to why this is returning a segmentation fault to me!
请问有什么建议吗?
推荐答案
您还没有为 thread_context
分配任何空间,请尝试
You haven't allocated any space for thread_context
, try
void t_init()
{
struct tcb *tmp;
tmp = malloc(sizeof(struct tcb));
if (!tmp)
return -1;
memset(&tmp, 0, sizeof(struct tcb));
tmp->thread_context = malloc(sizeof(ucontext_t));
if (!tmp->thread_context)
return -1;
getcontext(tmp->thread_context);
}
这篇关于在线程库中使用 getcontext() 出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!