Tag: 上下文切换

成功swapcontext后返回函数执行时出现段错误

我正在尝试编写一个库来管理使用上下文的线程(getcontext,setcontext,makecontext,swapcontext),而不使用pthreads。 函数MyThreadYield(),暂停当前线程上下文,将其发送到就绪队列的末尾,并开始在就绪队列的头部执行线程。 在MyThreadYield()中,我能够通过swapcontext()检索暂停线程的上下文,但是当它返回到函数执行时,我得到一个段错误。 例如: – 假设线程1是init线程,它运行并创建线程2.然后它产生。 现在线程2运行,然后调用yield。 这里swapcontext运行,我可以通过“%p”validation交换是否成功。 但是当它试图从MyThreadYield()返回以恢复线程1的函数运行时,我遇到了seg错误。 这是我的图书馆代码: – typedef void *MyThread; typedef void *MySemaphore; #include #include #include #include #include // structure of threads struct tnode { ucontext_t* ctextptr; int tid; // own thread id int pid; // parent thread id } ; struct tnode* cthread = NULL; // linked list for ready […]