操作系统课程设计试验报告格式16700字 四 川 大 学操作系统课程设计报告学 院:专 业:年 级:组 编 号:组 成 员: 软件 学 院 第 X组 乔心轲(姓名) 0743111340(学号)张雯(姓名) XXXXXXXX(学号) 康小芳(姓名) XXXXXXXX(学号)提交时间: 20xx年 月 日.指导教师评阅成绩:XXX1:XXX1:XXX1:XXX1:XXX1:实验项目一项目名称:实验目的:实验时间:人员分工:实验环境:实验环境的搭建过程、选用的操作系统、机器配置、编译器等 实验内容:对实践过程的详细说明,针对已经满足的实践要求,采用了何种算法或思想,对Nachos平台的哪些代码进行了什么样的修改实验结果:对实践要求的满足程度,代码是否编写完成,是否调试通过,能否正常运行,本项目的要求中共满足了哪几项参考文献:实验项目二实验项目名称:Nachos中的线程管理实验项目目的:1.最多能够同时存在128个用户线程2.改变为遵循“优先级调度”的抢占式调度参与人员及分工:乔心轲,何赵平,康小芳完成主要代码编写;崔蓉,张文进行程序的测试及维护实验环境:? Nachos: Not Another Completely Heuristic Operating System ? Linux? Gcc? Windows实验内容:1.对于最多能够同时存在128个用户线程,我们在Thread.cc中声明了一个static变量numOfThreads;具体代码如下:static int numOfThreads = 0;//the count of the threads在Thread的构造函数中对其值进行加1;即每创建一个线程时,都会把numOfThreads加1;++numOfThreads;并在SimpleThread()中进行了如下修改,完成了最多能够同时存在128个用户线程。
static voidSimpleThread(int which){if(numOfThreads<128){for(inti=0;i<(kernel->currentThread->Time())-(kernel->currentThread->executeTime)+2;i++){cout << "*** thread " <currentThread->executeTime<<" times.\n";cout<<"priority "<currentThread->Priority()<<"\n"; kernel->currentThread->Yield();}}else{if(count == 0)printf("The number of the threads can not be larger than 128!\n"); kernel->currentThread->Yield();count++;}}为了实现遵循“优先级调度”的抢占式调度策略,首先为Thread增加了三个变量:executeTime;time;priority在Thread.h中对它们的声明:public:Thread(char* debugName); // initialize a Thread ~Thread(); // deallocate a Thread// NOTE -- thread being deleted// must not be running when delete// is calledint executeTime;//The time that this thread has executed// basic thread operationvoid setExTime(int exT);//set the executeTime of the thread to exT int Time();//return the time that the thread should be servedvoid setTime(int t);//set the time that the thread should be served to tvoid setPriority(int pri); //set the priority of the thread to priint Priority(); //return the priority of the threadprivate:// some of the private data for this class is listed aboveint time; // the time that the thread should be servedint priority; //the priority of each thread在Thread.cc中的实现:Thread::Thread(char* threadName){name = threadName;stackTop = NULL;stack = NULL;status = JUST_CREATED;for (int i = 0; i < MachineStateSize; i++) {machineState[i] = NULL; // not strictly necessary, since // new thread ignores contents // of machine registers}space = NULL;executeTime=1;setPriority(0);setTime(2);}在Fork()中对numOfThreads进行了+1操作:voidThread::Fork(VoidFunctionPtr func, void *arg){Interrupt *interrupt = kernel->interrupt;Scheduler *scheduler = kernel->scheduler;IntStatus oldLevel;DEBUG(dbgThread, "Forking thread: " << name << " f(a): " << (int) func << " " << arg);StackAllocate(func, arg);oldLevel = interrupt->SetLevel(IntOff);scheduler->ReadyToRun(this); // ReadyToRun assumes that interrupts // are disabled!(void) interrupt->SetLevel(oldLevel);++numOfThreads;}//--------------------------------------------------------------------- //Thread::setExTime// set the executeTime of the thread to exT//---------------------------------------------------------------------voidThread::setExTime(int exT){executeTime = exT;}//--------------------------------------------------------------------- //Thread::setPriority// set the priority of the thread to pri//--------------------------------------------------------------------- voidThread::setPriority(int pri){priority=pri;}//--------------------------------------------------------------------- //Thread::Priority// return the priority of the thread//--------------------------------------------------------------------- intThread::Priority(){return priority;}//--------------------------------------------------------------------- //Thread::setPriority// set the time of the thread to t//--------------------------------------------------------------------- voidThread::setTime(int t){time=t;}//---------------------------------------------------------------------//Thread::Time// return the time of the thread need to run//--------------------------------------------------------------------- intThread::Time(){return time;}并在SimpleThread()中进行了如下修改:static voidSimpleThread(int which){if(numOfThreads<128){for(inti=0;i<(kernel->currentThread->Time())-(kernel->currentThread->executeTime)+2;i++){cout << "*** thread " <currentThread->executeTime<<" times.\n";cout<<"priority "<currentThread->Priority()<<"\n";kernel->currentThread->Yield();}}else{if(count == 0)printf("The number of the threads can not be larger than 128!\n"); kernel->currentThread->Yield();count++;}}对Scheduler.cc中进行了以下修改,实现了按照优先级进行线程的调度,返回最高优先级的线程,让其运行。
Thread *Scheduler::FindNextToRun (){ASSERT(kernel->interrupt-。