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
Lld0€恢
QQEMU-□X
Llaa企仪
动态优先级:
yzu-□x
Llachn*
^ifQEMU-PressCtrltAlttCtoreleasegrab—□X
Lld0€恢