Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Kernel Questions

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 4th September 2008, 05:45
rocosd rocosd is offline
Junior Member
 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
 
Default 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.
Reply With Quote
Sponsored Links
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 12:05.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.