欢迎来到天天文库
浏览记录
ID:46903178
大小:80.00 KB
页数:7页
时间:2019-11-29
《计算机操作系统实验_线程调度的优化与控制》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、西北工业大学操作系统实验实验报告一、实验目的掌握GeekOS系统的线程调度算法以及线程同步与互斥方法,实现线程调度的优化与控制。二、实验要求1.按照实验讲义P146页中的设计要求,增加线程调度算法的选择策略,使系统可以在时间片轮转调度和四级反馈队列调度之间切换,并且实现四级反馈队列调度算法,给出关键函数的代码以及实验结果。2.在GeekOS中实现信号量,使用信号量保证用户程序运行时的线程同步,给出关键函数的代码以及实验结果。三、实验过程及结果答:1、进程间的调度实现:Get_Next_Runnable代码如下:structKernel_
2、Thread*Get_Next_Runnable(void){structKernel_Thread*best=0;inti,best_index_queue=-1;if(g_schedulingPolicy==ROUND_ROBIN){structKernel_Thread*best_in_queue=NULL;for(i=0;i3、ex_queue=i;}elseif(best_in_queue!=NULL){if(best_in_queue->priority>best->priority){best=best_in_queue;best_index_queue=i;}}-7-}}elseif(g_schedulingPolicy==MULTILEVEL_FEEDBACK){if(g_currentThread->priority!=PRIORITY_IDLE){if(g_currentThread->blocked&&g_currentThread->curr4、entReadyQueue>0)g_currentThread->currentReadyQueue--;}for(i=0;icurrentReadyQueuecurrentReadyQueue++;}KASSERT(best!=NULL);Remove_Thread(&s_runQue5、ue[best_index_queue],best);returnbest;}结果如图:-7-2、信号量操作P、V操作代码如下:intP(intsid){if(!validateSID(sid)){returnEINVALID;}boolatomic=Begin_Int_Atomic();if(g_Semaphores[sid].resources==0){Wait(&g_Semaphores[sid].waitingThreads);KASSERT(g_Semaphores[sid].resources==1);}KASSERT(0<6、g_Semaphores[sid].resources);g_Semaphores[sid].resources--;End_Int_Atomic(atomic);return0;}intV(sid){if(!validateSID(sid)){returnEINVALID;}boolatomic=Begin_Int_Atomic();g_Semaphores[sid].resources++;if(g_Semaphores[sid].resources==1){Wake_Up_One(&g_Semaphores[sid].waitin7、gThreads);-7-}End_Int_Atomic(atomic);return0;}修改后的Ping.c代码如下:#include#include#include#include#includeintmain(intargc,char**argv){inti,j;/*loopindex*/intscr_sem;/*idofscreensemaphore*/inttime;/*currentandstarttime*/intping,po8、ng;/*idofsemaphorestosyncprocessesb&c*/time=Get_Time_Of_Day();scr_sem=Create_Semaphore("screen",1);/*re
3、ex_queue=i;}elseif(best_in_queue!=NULL){if(best_in_queue->priority>best->priority){best=best_in_queue;best_index_queue=i;}}-7-}}elseif(g_schedulingPolicy==MULTILEVEL_FEEDBACK){if(g_currentThread->priority!=PRIORITY_IDLE){if(g_currentThread->blocked&&g_currentThread->curr
4、entReadyQueue>0)g_currentThread->currentReadyQueue--;}for(i=0;icurrentReadyQueuecurrentReadyQueue++;}KASSERT(best!=NULL);Remove_Thread(&s_runQue
5、ue[best_index_queue],best);returnbest;}结果如图:-7-2、信号量操作P、V操作代码如下:intP(intsid){if(!validateSID(sid)){returnEINVALID;}boolatomic=Begin_Int_Atomic();if(g_Semaphores[sid].resources==0){Wait(&g_Semaphores[sid].waitingThreads);KASSERT(g_Semaphores[sid].resources==1);}KASSERT(0<
6、g_Semaphores[sid].resources);g_Semaphores[sid].resources--;End_Int_Atomic(atomic);return0;}intV(sid){if(!validateSID(sid)){returnEINVALID;}boolatomic=Begin_Int_Atomic();g_Semaphores[sid].resources++;if(g_Semaphores[sid].resources==1){Wake_Up_One(&g_Semaphores[sid].waitin
7、gThreads);-7-}End_Int_Atomic(atomic);return0;}修改后的Ping.c代码如下:#include#include#include#include#includeintmain(intargc,char**argv){inti,j;/*loopindex*/intscr_sem;/*idofscreensemaphore*/inttime;/*currentandstarttime*/intping,po
8、ng;/*idofsemaphorestosyncprocessesb&c*/time=Get_Time_Of_Day();scr_sem=Create_Semaphore("screen",1);/*re
此文档下载收益归作者所有