使用线程的分段错误

问题描述:

我正在尝试为使用和调度线程构建我自己的库。使用线程的分段错误

系统工作方式可以让您“准备好”make maximum 100 threads。然后有一个调度程序,每个时间段都接受活动线程,将其放入就绪列表中,并将第一个等待线程放入就绪列表中。

我的测试人员正在创建500000线程,并且每个线程都会创建一个新线程并尝试自行终止。第二个线程(第一个线程创建)执行相同的操作。第三只是终止自己。

这是测试仪:

void f3() 
{ 
    printf("f3 before terminate\n"); 
    uthread_terminate(uthread_get_tid()); 
    printf("f3 after terminate\n"); 
} 

void f2() 
{ 
    printf("f2 before spawn\n"); 
    uthread_spawn(f3); 
    printf("f2 after spawn\n"); 
    uthread_terminate(uthread_get_tid()); 
    printf("f2 after termination\n"); 
} 

void f1() 
{ 
    printf("f1 before spawn\n"); 
    uthread_spawn(f2); 
    printf("f1 after spawn\n"); 
    uthread_terminate(uthread_get_tid()); 
    printf("f1 after termination\n"); 
} 



int main(int argc, char **argv) 
{ 
    printf("test8:\n--------------\n"); 
    cout << "* Output should be:\n";  
    cout << "--------------\n"; 
    cout << "***1***\n"; 
    cout << "***2***\n"; 
    cout << "Output is:\n"; 
    uthread_init(100); 
    printf("***1***\n"); 
    for (volatile int i=0; i< 50000;++i){ 
     uthread_spawn(f1); 
    } 
    printf("***2***\n"); 

    uthread_terminate(0); 
    return 0; 
} 

虽然我的节目被 “分段错误”,当它进入我的调度:

static void scheduler(){ 
DBG(("Schedular ")) 
nQuantum++; 
if (ready.size()!=0){ 
    if (active != NULL){ 
     if (active->getState() == Thread::RUNNING){ 
      active->setState(Thread::READY); 
      ready.push_back(active); 
     } 

     int val = sigsetjmp(*active->getEnv(),1); 
     if (val !=0){ 
      blockTimerSignal(UNBLOCK); 
      return; 
     } 
    } 

    // Set new Active 
    active = ready.front(); 
    DBG(("Active thread Id: %d",active->getTid())) 
    ready.pop_front(); 
    DBG(("Doing pop front on ready list")) 
    DBG(("Number of threads in ready list is - %d",(ready.size()))) 
    active->setQuantums(active->getQuantums()+1); 
    active->setState(Thread::RUNNING); 
    setTimer(); 
    blockTimerSignal(UNBLOCK); 
    DBG(("UNBLOCKED")) 
    siglongjmp(*active->getEnv(),1); 
    DBG(("After siglong jmp")) 
}else{ 
    active->setQuantums(active->getQuantums()+1); 
} 
DBG(("Number of threads in ready list is - %d",(ready.size()))) 

blockTimerSignal(UNBLOCK); 
} 

在做siglongjmp(*active->getEnv(),1);

它仅发生于主线程(ID为0的程序的第一个线程)。 它也发生在程序运行一段时间后,也就是说,在程序中间,它可以为活动线程执行siglongjmp,但是当它在一段时间后再次尝试时,它会给出错误。

添加我的终止功能,如果帮助:

int uthread_terminate(int tid) { 
    DBG(("Terminate - %d", tid)) 

    if (tid == 0){ 
     // delete all (threads) - don't think it's needed because using stl! 
     // TODO : check if needed - and then delete all lists ! 
     //Added by Roni - Deleting all lists! 
     while(!sleeping.empty()){ 
      delete (sleeping.front()); 
      sleeping.pop_front(); 
     } 

     while(!suspended.empty()){ 
      delete (suspended.front()); 
      suspended.pop_front(); 
     } 

     while(!ready.empty()){ 
      delete (ready.front()); 
      ready.pop_front(); 
     } 


     exit(0); 
    } 

    pThread t = getThread(tid); 
    if (t == NULL){ 
     errmsgLibrary(THREAD_NOT_FOUND); 
     return FAIL; 
    } 
    Thread::threadState state = t->getState(); 
    DBG(("Terminate - %d in State %d", tid, state)) 
    DBG(("Number of threads in ready list is - %d",(ready.size()))) 
    blockTimerSignal(BLOCK); 
    switch (state){ 
    case (Thread::RUNNING): 
     //DBG(("Running Case")) 
     //DBG(("Active thread id is: %d ",active->getTid())) 
      delete active; 
      active = NULL; 
    //DBG(("Finsihed running Case")) 
      scheduler(); 

      break; 
    case (Thread::READY): 
     //DBG(("ready Case")) 
      ready.remove(t); 
      delete t; 
      //DBG(("Finsihed ready Case")) 
     break; 
    case (Thread::SLEEP): 
      sleeping.remove(t); 
      delete t; 
     break; 
    case (Thread::SUSPENDED): 
      suspended.remove(t); 
      delete t; 
      break; 
    default: 
     break; 
    } 
    //DBG(("Number of threads in ready list is - %d",(ready.size()))) 
    blockTimerSignal(UNBLOCK); 
    return SUCCESS; 
} 
+0

'active'必须是'NULL',因此在那个时候导致'SIGSEGV'。你应该检查'uthread_spawn(f3);'每次调用它时都成功或者不成功,因为你可能遇到线程限制或内存不足,所以失败了。 – 2013-04-11 08:21:30

+0

@ReckHou侯厚其实当我检查我只看到f1在行动。 这是什么意思?你有什么建议? – 2013-04-11 08:31:08

在猜测,您使用的siglongjmp这里,大概不会改变调用之间堆栈。

考虑使用getcontext()swapcontext()makecontext()来代替。