ユーザー レベルのスレッド ライブラリを実装しようとしていますが、ラウンド ロビン方式でスレッドをスケジュールする必要があります。現在、makecontext、getcontext、および swapcontext を使用して作成した 2 つのスレッドの切り替えを機能させようとしています。ITIMER_PROF 値を持つ setitimer が使用され、SIGPROF シグナルが生成されるたびに新しいスレッドをスケジュールするためのハンドラーが sigaction に割り当てられます。ただし、シグナル ハンドラは呼び出されないため、スレッドはスケジュールされません。その理由は何ですか?コードの一部を次に示します。
void userthread_init(long period){
/*long time_period = period;
//Includes all the code like initializing the timer and attaching the signal
// handler function "schedule()" to the signal SIGPROF.
// create a linked list of threads - each thread's context gets added to the list/updated in the list
// in userthread_create*/
struct itimerval it;
struct sigaction act;
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = &schedule;
sigemptyset(&act.sa_mask);
sigaction(SIGPROF,&act,NULL);
time_period = period;
it.it_interval.tv_sec = 4;
it.it_interval.tv_usec = period;
it.it_value.tv_sec = 1;
it.it_value.tv_usec = 100000;
setitimer(ITIMER_PROF, &it,NULL);
//for(;;);
}
上記のコードは、タイマーを初期化し、ハンドラー スケジュールをシグナル ハンドラーにアタッチするものです。scheduler() 関数を呼び出す上記の関数にシグナル SIGPROF が与えられると想定しています。スケジューラ関数は次のとおりです。
void schedule(int sig, siginfo_t *siginf, ucontext_t* context1){
printf("\nIn schedule");
ucontext_t *ucp = NULL;
ucp = malloc(sizeof(ucontext_t));
getcontext(ucp);
//ucp = &sched->context;
sched->context = *context1;
if(sched->next != NULL){
sched = sched->next;
}
else{
sched = first;
}
setcontext(&sched->context);
}
それぞれのコンテキストが格納されている準備完了スレッドのキューがあります。setcontext 命令が実行されるたびに、各スレッドをスケジュールする必要があります。ただし、scheduler() は呼び出されません。誰かが私の間違いを指摘できますか??