|
i'm having the same problem-here's my code:
code:
//code snippet from main:
/* context names for scheduler and threads, todo maybe set up threads as an array? */
ucontext_t sched;
ucontext_t t1;
/* set up context and memory for threads, not sure what
* the flags do
*/
if(debug){ printf("create context and memory for threads\n");}
getcontext(&t1);
if ((t1.uc_stack.ss_sp = malloc(thread_stk)) == null){
perror("t1 malloc"), exit(1);
}
t1.uc_stack.ss_flags = 0;
t1.uc_link = &sched;
makecontext(&t1, produce_sm, 0);
/* save main, swap to a thread */
if(debug){ printf("save sched, swap to t1\n");}
if(getcontext(&sched) == -1){ /* save main */
perror("sched get error");
}
if ((sched.uc_stack.ss_sp = malloc(thread_stk)) == null){ /* main stack space */
die("sched malloc");
}
//fails here
if(swapcontext(&sched, &t1) == -1){ /* swap to t1 */
die("sched swap error");
}
this is the function it's supposed to swap to:
code:
void produce_sm(){
printf("i'm t1\n");
printf("swap from t1 to sched\n");
if(swapcontext(&t1, &sched) == -1){die("t1 swap");}
}
if i ever get this right, its supposed to be a threaded implementation of the
producer-consumer problem. this is my first venture into threads, i'd appreciate any advice.
|