Nachos实验报告9

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

计算机科学与技术学院实验报告:9

实验题目:设计并实现具有优先级的线程调度策略姓名:李威

日期:2013-12-1 学号:201100300259 班级:11级软件3班Email:sduliwei@

实验目的:

Nachos系统采用基本的FCFS的线程调度策略,修改成为具有优先级的线程调度策略

硬件环境:

软件环境:

linux操作系统,Nachos操作系统

实验步骤:

1.修改Thread类的构造函数,加入优先级priority属性,并且加入getPrioty方法。以便在线程的

等待队列中找到优先级最高的线程。其中,线程优先级的范围从1到7,默认为7,即最低优先级。

修改代码如下:(,thread.h)

class Thread {

……………………………………

public:

Thread(char* debugName, int priority=7);// initialize a Thread …………………………………………………

int getPriority(){return this->priority;

}

Thread::Thread(char* threadName, int p)

{

if(p<1) priority = 1;

else if(p>7) priority = 7;

else priority = p;

name = threadName;

stackTop = NULL;

stack = NULL;

status = JUST_CREATED;

#ifdef USER_PROGRAM

space = NULL;

#endif

}

2,首先,了解Nachos系统原来的线程调度方式。通过读,,文件

中的内容了解线程调度的方式。

首先,修改 文件中的ThreadTest方法,创建有优先级的Thread,代码如下:

然后,从Thread类中找到Fork方法,代码如下:

这说明ThreadTest方法的目的是,实例化新的线程,调用Fork方法,也就是让新产生的线程去执行SimpleThread方法,并且把当前执行的线程加入到等待队列。

从SimpleThread的定义中可以知道,新生产的线程就是打印一条信息然后去执行Yield();

通过查看yield,可知,先从等待队列中找到一个线程保留在nextThread中,并将当前线程加到等待队列,然后使nextThread运行

void

Thread::Yield ()

{

Thread *nextThread;

IntStatus oldLevel = interrupt->SetLevel(IntOff);

ASSERT(this == currentThread);

DEBUG('t', "Yielding thread \"%s\"\n", getName());

nextThread = scheduler->FindNextToRun();

if (nextThread != NULL) {

scheduler->ReadyToRun(this);

scheduler->Run(nextThread);

}

(void) interrupt->SetLevel(oldLevel);

}

3,为了实现按优先级调度,需要按优先级选择等待队列中的,即状态为ready的线程,因而,在线程插入,移除等待队列的时候使用List类中提供好的SortedInsert(void *item, int sortKey)

SortedRemove(int *keyPtr)

方法,而不是原来使用的Append,Remove方法;

void

Scheduler::ReadyToRun (Thread *thread)

{

DEBUG('t', "Putting thread %s on ready list.\n", thread->getName());

thread->setStatus(READY);

readyList->SortedInsert((void *)thread, thread->getPriority());

}

//----------------------------------------------------------------------

Thread *

Scheduler::FindNextToRun ()

{

int p;

return (Thread *)readyList->SortedRemove(&p);

}

4,结果显示:

lu@ubuntu:~/csc2404/nachos-3.4/code/threadsWithPrioty$ ./nachos ******* thread 2 looped 0 times

******* thread 4 looped 0 times

******* thread 2 looped 1 times

******* thread 2 looped 2 times

******* thread 4 looped 1 times

******* thread 2 looped 3 times

******* thread 4 looped 2 times

******* thread 2 looped 4 times

******* thread 4 looped 3 times

******* thread 4 looped 4 times

******* thread 5 looped 0 times

******* thread 5 looped 1 times

******* thread 5 looped 2 times

******* thread 3 looped 0 times

******* thread 5 looped 3 times

******* thread 3 looped 1 times

******* thread 5 looped 4 times

******* thread 3 looped 2 times

******* thread 1 looped 0 times

******* thread 3 looped 3 times

******* thread 1 looped 1 times

******* thread 3 looped 4 times

******* thread 1 looped 2 times

******* thread 1 looped 3 times

******* thread 1 looped 4 times

No threads ready or runnable, and no pending interrupts.

Assuming the program completed.

Machine halting!

Ticks: total 400, idle 10, system 390, user 0

Disk I/O: reads 0, writes 0

Console I/O: reads 0, writes 0

Paging: faults 0

Network I/O: packets received 0, sent 0

Cleaning up...

5,结果分析:

相关文档
最新文档