[英]Implementing thread scheduler round robin and thread cancel
I'm very new in C language and linux and English is not my mother language. 我是C语言和linux的新手,英语不是我的母语。 Sorry for those in advance.
对不起,那些提前。
I'm working on school project which is to implement a round robin scheduler on linux and I have some problems on implementing scheduler and thread_self. 我正在进行学校项目,该项目是在Linux上实现循环调度程序,在实现调度程序和thread_self时遇到一些问题。
Scheduler checks if ready queue is empty first, if yes set time slice and alarm(timeslice). 调度程序首先检查就绪队列是否为空,如果是,则设置时间片和警报(时间片)。 Otherwise, look up a new thread from ready list, dispatch TCB of new thread, set timeslice, context switch to new thread and set alarm(timeslice).
否则,从就绪列表中查找新线程,调度新线程的TCB,设置时间片,上下文切换到新线程并设置警报(时间片)。 But I keep getting error at some point and I couldn't find where to fix.
但是我有时会出现错误,并且找不到解决方法。
Another thing is about thread_cancel. 另一件事是关于thread_cancel的。 int thread_cancel(thread_t tid) function removes target tcb and I need to find the tcb using tid.
int thread_cancel(thread_t tid)函数删除了目标tcb,我需要使用tid查找tcb。 I tried like
我试过像
Thread* temp = getpid();
kill(pid, SIGKILL);
but I couldn't figure out how to remove tcb from queue. 但我不知道如何从队列中删除tcb。 Please give me some better idea!
请给我一些更好的主意!
Thread.h Thread.h
typedef struct _Thread{
ThreadStatus status;
pid_t pid;
Thread* pPrev;
Thread* pNext;
}Thread;
Thread* ReadyQHead;
Thread* ReadyQTail;
-Queue -队列
typedef struct _queue{
Thread* head;
Thread* tail;
unsigned int num;
} queue;
queue* runList;
queue* readyList;
queue* waitList;
-Queue.c -Queue.c
void enqueue(queue * q, Thread* tcb)
{
if(q->num == 0) {
q->head = tcb;
q->tail = tcb;
} else {
q->tail->pNext = tcb;
q->tail = tcb;
}
q->num ++;
}
Thread * dequeue(queue * q)
{
Thread * tmp;
if(q->num == 0) return NULL;
else if(q->num == 1) {
tmp = q->head;
q->head = NULL;
q->tail = NULL;
} else {
tmp = q->head;
q->head = q->head->pNext;
}
q->num --;
return tmp;
}
-Scheduler -Scheduler
void alarmHandler(int signal)
{
printf("Scheduler awake!!");
/*Do schedule*/
}
int RunScheduler( void )
{
//check if ready queue is empty
if(is_empty(readyList) != 0)
{
printf("this is weird");
signal(SIGALRM, alarmHandler);
}
else {
/*Look up a new thread from ready list*/
Thread* tmp = ReadyQHead;
/*send sigcont*/
kill(tmp->pid, SIGCONT);
//dispatch TCB of new thread
if(is_empty(runList) != 0 && runList->head->status == 0)
enqueue(readyList, tmp);
//pick thread at head of ready list as first thread to dispatch
tmp->status = 0; // tmp == runningTcb
printf("alive tcb : %d\n", tmp->pid);
ReadyQHead = dequeue(readyList);
//set timeslice
signal(SIGALRM, alarmHandler);
//context switch to new thread
_ContextSwitch(tmp->pid, ReadyQHead->pid);
}
while(1){
alarm(TIMESLICE);
}
return 0;
}
void _ContextSwitch(int curpid, int tpid)
{
kill(curpid, SIGSTOP);
kill(tpid, SIGCONT);
}
here is an simple example of round robin
这是
round robin
的简单例子
while(1)
{
process1();
process2();
process3();
process4();
....
processN();
}
anything more complex, such as context switching while a process is 'sleeping' or context switching while waiting for I/O or context switching because a process needs to be run on a timed basis or due to an interrupt event adds massive complexity. 任何更复杂的事情,例如进程“睡眠”时的上下文切换或等待I / O时的上下文切换或上下文切换,因为进程需要定时运行或由于中断事件而增加了极大的复杂性。
So exactly what is your goal/objective? 那么,您的目标到底是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.