教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

山大操作系统课程设计报告(全套)(6)

来源:网络收集 时间:2026-05-20
导读: 计算机科学与技术学院实验报告:9 实验题目:设计并实现具有优先级的线程调度策略 日期:2010-11-13 Email: 实验目的: Nachos系统采用基本的FCFS的线程调度策略,修改成为具有优先级的线程调度策略 硬件环境: 软

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

实验题目:设计并实现具有优先级的线程调度策略 日期:2010-11-13 Email: 实验目的:

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

硬件环境:

软件环境:

linux操作系统,Nachos操作系统

实验步骤:

1.修改Thread类的构造函数,加入优先级priority属性,并且加入getPrioty方法。以便在线程的 等待队列中找到优先级最高的线程。其中,线程优先级的范围从1到7,默认为7,即最低优先级。 修改代码如下:(thread.cc,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.cc,thread.cc,scheduler.cc文件 中的内容了解线程调度的方式。

首先,修改threadtest.cc 文件中的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', \

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', \

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, 结果分析:

可以看出,线程执行的顺序不是在ThreadTest中的生成Thread(1,2,3,4,5)的顺序,而是按照优先级的顺序 调度的。首先从等待队列中找一个优先级最高的线程,然后把当前运行的线程加入到等待队列中,然后再运行

(本次实验所在所在位置:csc2404/nachos-3.4/code/threadsWithPrioty) 结论分析与体会:

通过这个实验,为了实现线程优先级的调度,细致分析了Thread目录下的各个文件的内容,尤其是thread.cc, Scheduler.cc,文件中对于线程的状态,以及调度函数的实现等问题上面有了更深一步的了解。从而,知道如何 修改可以实现线程的优先级调度问题,也就是管理一个SortedList,其内容是状态为Ready的线程。 由概念到实践,了解了简单的线程调度的实现。

返回

…… 此处隐藏:1436字,全部文档内容请下载后查看。喜欢就下载吧 ……
山大操作系统课程设计报告(全套)(6).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/598633.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)