makecontext segfault?

Posted by cdietschrun on Stack Overflow See other posts from Stack Overflow or by cdietschrun
Published on 2010-12-28T17:55:04Z Indexed on 2010/12/28 18:53 UTC
Read the original article Hit count: 155

Filed under:
|

I am working on a homework assignment that will be due in the next semester. It requires us to implement our own context switching/thread library using the ucontext API. The professor provides code that does it, but before a thread returns, he manually does some work and calls an ISR that finds another thread to use and swapcontexts to it or if none are left, exits.

The point of the assignment is to use the uc_link field of the context so that when it hits a return it takes care of the work. I've created a function (type void/void args) that just does the work the functions did before (clean up and then calls ISR). The professor said he wanted this.

So all that's left is to do a makecontext somewhere along the way on the context in the uc_link field so that it runs my thread, right? Well, when I do makecontext on seemingly any combination of ucontext_t's and function, I get a segfault and gdb provides no help.. I can skip the makecontext and my program exist 'normally' when it hits a return in the threads I created because (presumably) the uc_link field is not properly setup (which is what I'm trying to do).

I also can't find anything on why makecontext would segfault. Can anyone help?

stack2.ss_sp = (void *)(malloc(STACKSIZE));
if(stack2.ss_sp == NULL){
printf("thread failed to get stack space\n");
exit(8);
}
stack2.ss_size = STACKSIZE;
stack2.ss_flags = 0;

if(getcontext(&main_context) == -1){
perror("getcontext in t_init, rtn_env");
exit(5);
}

//main_context.uc_stack = t_state[i].mystk;                                                                                                                
main_context.uc_stack = stack2;
main_context.uc_link = 0;
makecontext(&main_context, (void (*)(void))thread_rtn, 0);

I've also tried just thread_rtn, &thread_rtn and other things. thread_rtn is declared as void thread_rtn(void).

later, in each thread. run_env is of type ucontext_t: ...

t_state[i].run_env.uc_link = &main_context;

© Stack Overflow or respective owner

Related posts about c

    Related posts about multithreading