nachos操作系统-基于优先级的线程调度
基于优先级的线程调度
1.实验目的
熟悉nachos操作系统,掌握线程优先级的调度,深入理解操作系统内核,了解用户程序的加载过程以及多用户进程的内存分配机制。
2.实验内容
Nachos的线程管理模块升级源代码及实现线程的优先级调度
3.实验方法(实验步骤)
Thread.h文件
(1)、指定线程的最大值;
(2)、重载 Thread构造函数,用于用户线程的创建;
(3)、定义成员变量和成员函数;
Thread.cc文件
(1)、设置random()函数用于获取三个优先级使用;
(2)、初始化tId;
(3)、重载函数给出用户创建的线程(id号从1000起)
(4)、用selftest()对三个队列分别设置不同的优先级(1、高优先级,2、中优先级,3、低优先级);
scheduler.h文件
(1)、在scheduler中定义其他两个就绪队列并使用不用的优先级;
scheduler.cc文件
(1)、构造函数scheduler创建三个就绪队列,分别为不同的优先级;
(2)、构造函数scheduler分别析构
(3)、构造函数scheduler进行优先级比较。
4.实验过程(源代码、配置清单必须带注释)
注:对以下的文件进行修改源代码
(1)、Thread.h文件 (添加tIDs、priority、tID变量)
private:
int *stackTop;
void *machineState[MachineStateSize];
static int threadNum;
static Bitmap *tIDs;
int priority;
int tId;
public:
Thread(char* debugName);
Thread(char* debugName,int p);
~Thread();
int random();
void setTId(int id);
int getTId();
void setPriority(int p);
int getPriority();
(2)、thread.cc文件 (新增random()、getTId()、Thread(char* threadName,int p)函数)
int Thread::random() //获取线程优先级
{
inti,number;
for(i=0; i<3; i++)
{
number= rand() % 3+1;
//printf("%d", number);
}
return number;
}
voidThread::setTId(int id){ //设置线程ID
this->tId = id;
}
intThread::getTId(){
return this->tId;
}
intThread::threadNum = 0;
Thread::Thread(char*threadName,int p)
{
if(++threadNum > MAXTHREADS)
{
cout<<" error123"<<endl;
ASSERT(threadNum <= MAXTHREADS-1);
}
name = threadName;
setTId(tIDs->FindAndSet() + 1000);//
setPriority(p);
stackTop = NULL;
stack = NULL;
status = JUST_CREATED;
for (int i = 0; i < MachineStateSize;i++) {
machineState[i] = NULL;
}
space = NULL;
cout << "Create No."<< threadNum << " userThread with tId=" << getTId()<< " and priority=" << getPriority() << endl;
}
voidThread::setPriority(int p) //
{
this->priority = p;
}
intThread::getPriority() //
{
return this->priority;
}
VoidThread::SelfTest()
{
DEBUG(dbgThread, "EnteringThread::SelfTest");
Thread *t1 = newThread("UserThread",random());
Thread *t2 = newThread("UserThread",random());
Thread *t3 = newThread("UserThread",random());
Thread *t4 = newThread("UserThread",random());
t1->Fork((VoidFunctionPtr)SimpleThread,(void*)t1->getTId());
t2->Fork((VoidFunctionPtr)SimpleThread,(void*)t2->getTId());
t3->Fork((VoidFunctionPtr)SimpleThread,(void*)t3->getTId());
t4->Fork((VoidFunctionPtr)SimpleThread,(void*)t4->getTId());
kernel->scheduler->Print();
kernel->currentThread->Yield();
/*
Thread *t[5];
//cout<<"Thread finishiederror"<<endl;
for(int i=1;i<5;i++)
{
t[i] = new Thread("creat thread:",random());
t[i]->Fork((VoidFunctionPtr)SimpleThread,(void *)t[i]->getTId());
kernel->currentThread->Yield();
}*/
}
(3)、scheduler.cc文件 (对线程优先级进行比较排序)
Scheduler::Scheduler()
{
readyList= new List<Thread *>;
readyMidList= new List<Thread *>;
readyHigList= new List<Thread *>;
toBeDestroyed= NULL;
}
Scheduler::~Scheduler()
{
delete readyList;
delete readyMidList;
delete readyHigList;
}
VoidScheduler::ReadyToRun (Thread *thread)
{
Thread *currentThread;
ASSERT(kernel->interrupt->getLevel()== IntOff);
DEBUG(dbgThread, "Putting thread onready list: " << thread->getName());
thread->setStatus(READY);
if(thread->getPriority() == 1){
readyHigList->Append(thread);
}
else if(thread->getPriority() == 2){
readyMidList->Append(thread); //
}
else if(thread->getPriority() == 3){
readyList->Append(thread); //
}
}
(4)、scheduler.h文件 (新增两个优先级的成员变量)
private:
List<Thread *> *readyList;
List<Thread *> *readyMidList;
List<Thread *> *readyHigList;
6、实验截图
5.实验体会
基本实现要求功能,实现限制线程数目和根据优先级来调度;