스레드를 사용하고 예약하기위한 자체 라이브러리를 빌드하려고합니다.세그먼트 오류 스레드 사용
시스템은 사용자가 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);
그것은 주 만 일을하고 thread (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;
}
'active'는'NULL'이어야하며 그 순간에'SIGSEGV'가 발생합니다. 쓰레드 제한이나 메모리 부족을 만났기 때문에'uthread_spawn (f3);'이 성공할 때마다 또는 호출 할 때마다 확인해야한다. –
@ReckHou Hou Hou 사실 내가 확인했을 때 나는 단지 f1 만 보였습니다. 무엇을 의미합니까? 너는 무엇을 제안 하는가? –