我正在尝试使用系统调用(例如获取上下文、交换上下文等)在 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() 出现分段错误
发布评论