Rgg make context and stack initialization !

Discussion in 'Kernel Questions' started by rocosd, Sep 4, 2008.

  1. rocosd

    rocosd New Member

    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.
     

Share This Page