在线程库中使用 getcontext() 出现分段错误

编程入门 行业动态 更新时间:2024-10-25 23:28:17
本文介绍了在线程库中使用 getcontext() 出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

本文发布于:2023-11-27 02:55:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   库中   错误   getcontext

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!