The UNIX Forums
"Join the Network of UNIX System Users"


 
Subject: swapcontext() and segmentation fault
matt-baker
Newbie
Rank: 1



UID 256
Digest Posts 0
Credits 0
Posts 38
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 3, 2007 03:47 AM  Profile | P.M. 
swapcontext() and segmentation fault



swapcontext() and segmentation fault




i am in the process of trying to analysing a code snippet i just wrote, and am facing a segmentation fault when i try to swap context an a point...

i get a segmentation fault when i call swapcontext() function...

please guide me.


last edited by jacques83 : 11-25-2006 at 03:25 pm.


Top
joh1
Newbie
Rank: 1



UID 208
Digest Posts 0
Credits 0
Posts 37
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 3, 2007 03:47 AM  Profile | P.M. 
can you post the code snippet where you call the "swapcontext" function?
Top
aju_kup
Newbie
Rank: 1



UID 18
Digest Posts 0
Credits 0
Posts 35
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 3, 2007 03:48 AM  Profile | P.M. 
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.
Top
Celtik
Newbie
Rank: 1



UID 84
Digest Posts 0
Credits 0
Posts 62
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 3, 2007 03:48 AM  Profile | P.M. 
i'm on redhat linux, its a class project- we're supposed to implement our own threads, we're limited to make/set/getcontext or set/longjump.


quote:
gebuh, what os are you using? most os'es these days have kernel support for threads. if your kernel does, it will have allocated a thread for you to use. and it looks like you are trying to implement your own user-level threads within that kernel thread. why take this approach rather than just using pthreads?
Top
hani
Newbie
Rank: 1



UID 170
Digest Posts 0
Credits 0
Posts 31
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 3, 2007 03:48 AM  Profile | P.M. 
quote:
i'm on redhat linux, its a class project- we're supposed to implement our own threads, we're limited to make/set/getcontext or set/longjump.



we have some rules here and one of them is:
(6) do not post classroom or homework problems.


but once in a while we bend the rule a bit, so try setting ss_size when you define the stack.
Top
Hopper
Newbie
Rank: 1



UID 176
Digest Posts 0
Credits 0
Posts 58
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 3, 2007 03:49 AM  Profile | P.M. 
duly chastened [walk of shame]gonna go read the rules now[/end walk of shame]


quote:
we have some rules here and one of them is:
(6) do not post classroom or homework problems.


but once in a while we bend the rule a bit, so try setting ss_size when you define the stack.
Top
Bombaywala
Newbie
Rank: 1



UID 75
Digest Posts 0
Credits 0
Posts 25
Reading Access 10
Registered Apr 25, 2007
Status Offline
Post at Jun 3, 2007 03:49 AM  Profile | P.M. 
guh.

i'm gonna bend the rules since i feel obliged to point out that longjmp and setcontext are not signal-safe! see 'man signal' for the explicit list of functions that are. so if you're using signals as a trigger to switch contexts you're going to have to throw them out.
Top
 

 

All times are GMT, the time now is Jul 31, 2010 03:31 AM

Powered by Discuz! 5.0.0  © 2001-2006 UNIX Forums
Processed in 0.006107 second(s), 8 queries

Clear Cookies - Contact Us - UNIX Help - Archiver - WAP