重庆大学操作系统实验二.docx

上传人:b****3 文档编号:27530746 上传时间:2023-07-02 格式:DOCX 页数:17 大小:80.94KB
下载 相关 举报
重庆大学操作系统实验二.docx_第1页
第1页 / 共17页
重庆大学操作系统实验二.docx_第2页
第2页 / 共17页
重庆大学操作系统实验二.docx_第3页
第3页 / 共17页
重庆大学操作系统实验二.docx_第4页
第4页 / 共17页
重庆大学操作系统实验二.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

重庆大学操作系统实验二.docx

《重庆大学操作系统实验二.docx》由会员分享,可在线阅读,更多相关《重庆大学操作系统实验二.docx(17页珍藏版)》请在冰豆网上搜索。

重庆大学操作系统实验二.docx

重庆大学操作系统实验二

重庆大学操作系统实验二

(总12页)

本页仅作为文档封面,使用时可以删除

Thisdocumentisforreferenceonly-rar21year.March

重庆大学

学生实验报告

开课实验室重庆大学DS1501

学院软件工程年级2013专业班

学生姓名学号开课时间2015至2016学年第一学期

总成绩

教师签名

洪明坚

重庆大学软件学院制

开课实验室:

DS1501

《操作系统原理》实验报告

学院

软件学院年级、专业、班

姓名成绩

课程

名称

操作系统原理

实验项目

名称

线程及其调度

指导教师

教师评语

教师签名:

年月曰

一、实验目的(软件需求文档)

掌握线程的创建

掌握线程的调度

a)静态优先级调度

b)动态优先级调度

二、实验原理(软件设计文档)

系统调用接口,线程相关函数:

•Stepl:

定义线程函数

voidtsk_foo(void*pv)

{

printf(nThisistaskfoowithtid=%d\r\n/z,task_getid());task_exit(O);

}

•Step2:

申请用户栈

unsignedchar*stack_foo;

stack_foo=(unsignedchar*)malloc{1024*1024);

-线程退出后,才能把用户栈用free释放掉!

•Step3:

创建线程

inttid_foo;

tid_foo=task_create(stack_foo+1024*1024/tsk_foo,(void*)0);

2016年1月6日

3.使用仪器、材料(软硬件开发环境)

Notepad++

expenv

四、实验步骤(实现的过程)

随机生成3组非负整数列表,创建3个线程,分别用3种不同的排序算法(插入,冒泡,选

择)对列表进行排序

三线程:

voidtsk_foo」inel(void*pv)

{一一

intm;

inti;

intarry[50];

srand(time(NULL));

for(i=0;i<50;i++){

m=random()%200;if(m<0){m=0-m;}draw(i#10,0z0+m);arry(i]=m;

}

sort^mfarry,50,0);

task_exit(O);

voidtsk_foojine2(void*pv)

{"一

intm;

inti;

intarry[50];

srand(time(NULL));

for(i=0;i<50;i++){

m=random()%200;if(m<0){m=0-m;}drawfi#10,345,345+m);arry[i]=m;

}

sort_x(arry,50,345);

task_exit(O);

voidtsk_foojine3(void*pv)

{"一

intm;

inti;

intarry[50];

srand(time(NULL));

for(i=0;i<50;i++){

m=random()%200;if(m<0){m=0-m;}draw(is10,690,690+m);arry[i]=m;

}

sort_c(arry,50;690);

task_exit(O);

voiddraw(intx,intylzinty2){

for(i=yl;i

setPixel(i,x,RGB(255Z255,255));

voidresetBKfintx,intylfinty2){

inti;

for(i=yl;i

setPixel(i,x,RGB©0f0));

}

三排序:

冒泡

voidsort_m(int*arry,intn,in11){

intijtern;intt=500/n;

for(i=0;i

if(*(arry+j)>*(arry+j+1)){resetBK^t,I,I+*(arry+j));resetBK(j*t+1JI+*(arry+j+1));tem=*(arry+j);*(arry+j)=#(arry+j+1);*(arry+j+1)=tem;draw(j*t/l/l+*(arry+j));draw(j*t+1JI+*(arry+j+1));

插入

voidsort_c(int*arry,intn,intl){

intijkey;

intt=500/n;

for(j=n-2;j>=0;j-){

key=#(arry+j);i=j+l;resetBK^t,I,I+key);while(i

*(arry+i-1)=s(arry+i);draw(i*t-1JI+*(arry+i-1));i=i+1;

}

*(arry+i-1)=key;draw(i*t-1,1,1+key);

选择

voidsort_x(int*arry;intn,intl){

inti=0fj=0,lowindex=0;

intt=500/n;

for(i=0;i

lowindex=i;

for(j=n

if(arry[j]

if(lowindex!

=i)

{

resetBKfi*^I,I+*(arry+i));

resetBK(lowindexyI+*(arry+lowindex));inttemp=arry[i];arry[i]=arry[lowindex];

arry[lowindex]=temp;draw(i*tfI,I+#(arry+i));draw(lowindex*tzI,I+*(arry+lowindex));}

}

线程控制块tcb中增加nice属性,在函数sys_task_create中初始化nice=0/*系统调用getpriority的执行函数

获取当前线程的优先级

7

intsys_getpriority(inttid)

{

if(tid==O)returng_task_running->nice+NZERO;〃获取当前线程的niceffiuint32_tflags;structtcb*tsk;save_flags_cli(flags);tsk=get_task(tid);restore_flags(flags);

returntsk->nice+NZERO;〃获取线程的nice值

}

系统调ffjsetpriority的执行函数

调整当前线程的优先级

7

〃把线程tid的nice设为(prio-NZERO)

intsys_setpriority(inttid,intprio)

{

uint32_tflags;structtcb*tsk;if(tid==O){

save_flags_cli(flags);g_task_running->nice=prio-20;//设遥半前线程的niceffirestore_flags(flags);

return0;

}

//if(tsk==NULL)return-1;

if(prio<0)prio=0;//prio必须^[O^^NZERO-l]

if(prio>40)prio二40;

save_flags_cli(flags);

tsk=get_task(tid);//JIjsave_flags_cli/restore_flags保护

restore_flags(flags);tsk->nice=prio-20;//设星线程的nice值return0;

把这两个个函数做成系统调用,分别是getpriority(inttid),setpriority(inttid,intprio)

静态调度schedule:

voidschedule(){

structtcb^select=g_task_head;structtcb#my_select=g_task_running;while(SQlect!

二NULL){

if((select->tid!

=0)&&(select->state==TASK_STATE_READY))

//if(my_select==NULL){my_select=select;continue;}if(select->nice<=my_select->nice)//选抒等待队列里的线用优先级萬的my_select=select;

if(my_select->tid==O){//跳过taskO运行

my_select=select;

}

}

select=select->next;

if(my_select==g_t3sk-running){if(gjask_running->state==TASK_STATE_READY)return;my_select=taskO;//R当没有其他町运彳丁的线用讨,八}g_resched=0;switch_to(my_select);

线程控制块tcb中

增加estcpu属性,在函数sys_task_create中初始化estcpu=0;

增加priority属性,在函数sys_task_create中初始化priority=0;

中增加全局变Mg_load_avg:

表示系统的平均负荷

用浮点(float-point)表示g_load_avg和estcpu:

精度髙,效率低

动态调度schedule:

voidschedule()

{

structtcb*select=g_task_head;

structtcb#my_select=g_task_running;

while(select!

=NULL)

{

select->priority=127-fixedpt__toint(fixedpt_div(select->estcpuzfixedpt_fromint(4)))・select->nice*2;//计算所有线程的priority

select=select->next;

}

〃动态优先级

select=g_task_head;

while(select!

=NULL){

if((select->tid!

=0)&&(select->state==TASK_STATE_READY)){if(my_select->prioritypriority)

{

my_select二select;//选择等待队列里的线程优先级高的

}

elseif(my_select->tid==O)

{

my_select二select;

}

}

select二select•:

*next;

if(my_select==g_task_running){if(my-select-^state^=TASK_STATE_READY)return;

myselect=taskO;

printk(HOx%d->Ox%d\r\n:

(g_task_running==NULL)-1:

g_task_running->tid,select->tid);

g^resched二0;

switch_to(my_select);

}一一

中添加如下

g_task_running->estcpu=fixedpt_add(g_task_running->estcpuzFIXEDPT_ONE);〃计算线程使用CPUIbj间estcpu

if(gjimer_ticks%HZ==0){//每隔一秒计算一次

intnready=0;〃表示处丁就绪状态的线程个数

structtcbSmy_select=g_t3sk_head;

intnice;//g_task_running->nice;

//my_select=g_task_head;

fixedptratio;

while(my_select!

=NULL){

if(my_select->state==TASK_STATE_READY)nready++;

nice=my_select-ice;

ratio=fixedpt_mul(FIXEDPT_TWO,g_load_avg);〃每秒钟为所有纟殳程(运

行、就绪和等待〉更新一次—-

ratio=fixedpt_div(ratio,fixedpt_add(ratio,FIXEDPT_ONE));

my_select->estcpu=fixQdpt_add(fixQdpt_mul(ratio,my_select・>estcpu),

fixedpt_fromint(nice));

my_select=my_select->next;

}

fixedptr59_60=fixedpt_div(fixedpt_fromint(59),fixedpt_fromint(60));//计算系统的平均负荷

gjoad_avg

fixedptr01_60=fixedpt_div(FIXEDPT_ONEjixedpt_fromint(60));

gjoad_avg=fixedpt_add(fixedpt_mul(r59_60,g_load_avg),

fixedpt_muI{rOl_60,fixedpt_fromint(nready)));

}

主函数:

intmode=0x0118;

initGraphics(mode);

inty=0;

for(y=0;y<;y++){

setPixel/3,yfRGB(0z125,125));setPixel/3*2,y,RGB©125,125));

}

int"pcode^exit;

〃申请用月栈

unsignedcharsstack_fool=(unsignedchar*)ma//oc(1024#1024);unsignedcha广stack_foo2=(unsignedchar*)ma//oc(1024•1024);unsignedcha广stack_foo3=(unsignedchar*)ma//oc(1024#1024);unsignedcha广stack_foo4=(unsignedchar*)ma//oc(1024#1024);

inttid_fool,tid_foo2,tid_foo3;

setpriority©8);

〃创建冒泡排序线程函数1

tid^fool=task_create(stack_fool+1024*1024,tsk_foo」inel,(void*)0);setpriorityftid^fool,1);

〃创建选择押养线程函数2

tid_foo2=task_create(stack_foo2+1024*1024,tsk_foo」inQ2,(void*)0);setpriority(tid_?

oo2,10);

〃创建插入計於线程函数3

tid_foo3=task_create(stack_foo3+1024*1024,tsk_foojine3z(void*)0);setpriority(tid_foo3,8);

setpriorityfO,35);

〃用户栈释放

task_wait(tid_fool,pcode_exit);

/ree(stack_fool);task_wait(tid_foo2,pcode_exit);

/ree(stack_foo2);task_wait(tid_foo3,pcode_exit);

/ree(stack_foo3);

五、实验结果及分析(实现的效果,包括屏幕截图、系统总体运行情况和测试情况等)静态优先级:

 

aQEMU-□X

Lld

0€恢

QQEMU-□X

Llaa企仪

动态优先级:

 

yzu-□x

Llachn*

 

 

^ifQEMU-PressCtrltAlttCtoreleasegrab—□X

Lld

0€恢

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 英语学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1