下面的代码就是不能工作。有谁能指出原因吗?
#define STACK_SIZE 1524
static void mt_allocate_stack(struct thread_struct *mythrd)
{
unsigned int sp = 0;
void *stck;
stck = (void *)malloc(STACK_SIZE);
sp = (unsigned int)&((stck));
sp = sp + STACK_SIZE;
while((sp % 8) != 0)
sp--;
#ifdef linux
(mythrd->saved_state[0]).__jmpbuf[JB_BP] = (int)sp;
(mythrd->saved_state[0]).__jmpbuf[JB_SP] = (int)sp-500;
#endif
}
void mt_sched()
{
fprintf(stdout,"\n Inside the mt_sched");
fflush(stdout);
if ( current_thread->state == NEW )
{
if ( setjmp(current_thread->saved_state) == 0 )
{
mt_allocate_stack(current_thread);
fprintf(stdout,"\n Jumping to thread = %u",current_thread->thread_id);
fflush(stdout);
longjmp(current_thread->saved_state, 2);
}
else
{
new_fns();
}
}
}我所要做的就是在一个新的堆栈上运行new_fns()。但是is在new_fns()上显示了分段错误。
有人能给我指出哪里出了问题吗?
发布于 2010-03-16 20:53:40
抛开所有其他注意事项,您可以使用" &stck“代替ok "stck”作为堆栈!&stck指向包含指向已分配堆栈的指针的单元格
然后,一些观察结果:
2)我认为BP应该以其他方式进行评估。我建议检查一下你是如何编译组成堆栈框架的。ESP指向EBP- size _OF_LOCAL_CONTEXT,不同的编译器通常以不同的方式计算该大小。
据我所知,你是在使用某种“纤维”。如果你正在使用Win32,有一组函数可以安全地实现这个功能(参见“纤程”)。在linux上,我建议你去看看"libfiber“。
问候
https://stackoverflow.com/questions/2454289
复制相似问题