Rgg make context and stack initialization !
Hello All,
I am trying to create my own library of threads.
I got stuck with segmentation fault..
I need your help wuth this..
here is a code snippet...
< file mthread.c >
#include "mythread.h"
#include <malloc.h>
#include <stdio.h>
#define FIBER_STACK 2048
mythread_t T1;
int mythread_create(mythread_t *new_thread_ID,
mythread_attr_t *attr,
void * (*start_func)(void *),
void *arg)
{
// Get the current execution context
mythread_t t2 ;
t2 = (mythread_t)new_thread_ID;
getcontext(&t2->context);
// Modify the context to a new stack
t2->context.uc_stack.ss_sp = malloc( SIGSTKSZ );
t2->context.uc_stack.ss_size = 2048 ; t2->context.uc_stack.ss_flags = 0;
if ( t2->context.uc_stack.ss_sp == 0 )
{
perror( "malloc: Could not allocate stack" );
return 0;
}
// Create the new context
printf( "\n Creating child fiber" );
fflush(stdout);
makecontext(&t2->context,(void (*)())start_func,0);
}
void * display()
{
printf ("\n Hello \n");
fflush(stdout);
}
int main()
{
mythread_t t1;
mythread_create(&t1,NULL,display,NULL);
printf("\n thread creates \n");
fflush(stdout);
return 0;
}
<End of code >
The problem i get is a segmentation fault because of the highlighted lines ( in red).
Code gets compiled and linked successfully.
On execution , i get the desired o/p but at the end it does give a segmentation fault.
Please comment.
|
Recent comments
14 hours 58 min ago
15 hours 3 min ago
20 hours 1 min ago
1 day 2 hours ago
1 day 3 hours ago
1 day 4 hours ago
1 day 9 hours ago
1 day 15 hours ago
1 day 19 hours ago
1 day 21 hours ago