《计算机操作系统实验》由会员分享,可在线阅读,更多相关《计算机操作系统实验(12页珍藏版)》请在金锄头文库上搜索。
1、计算机操作系统实验-线程调度 的优化与控制西北工业大学 操作系统实验 实验报告一、实验目的掌握GeekOS系统的线程调度算法以及线程同步与互斥方法,实现线程调 度的优化与控制。二、实验要求1. 按照实验讲义P146页中的设计要求,增加线程调度算法的选择策略,使 系统可以在时间片轮转调度和四级反馈队列调度之间切换,并且实现四级反馈 队列调度算法,给出关键函数的代码以及实验结果。2. 在GeekOS中实现信号量,使用信号量保证用户程序运行时的线程同步, 给出关键函数的代码以及实验结果。三、实验过程及结果答:1、进程间的调度实现:KernelThread*Get_Next_Runnable 代码如下
2、: structGet_Next_Runnable(void)struct Kernel_Thread* best = 0;int i, best_index_queue = -1;if (g_schedulingPolicy = ROUND_ROBIN) (struct Kernel_Thread* best_in_queue = NULL;for (i = 0; i priority best-priority) best = best_in_queue; best_index_queue = i;if else if (g_schedulingPolicy = MULTILEVEL_FE
3、EDBACK) if ( g_currentThread-priority != PRIORITY_IDLE )if ( g_currentThread-blocked &g_currentThread-currentReadyQueue 0 )g_currentThread-currentReadyQueue-;for (i = 0; i currentReadyQueue currentReadyQueue+;KASSERT(best != NULL);Remove_Thread(&s_runQueuebest_index_q ueue, best);return best;结果如图:I
4、work load, mlf 100*祚*琳*祚*祚村* Start Uorkload Generator lias lias has isrocessrocessrocesspocessrocessrocessLongPingFongLongPongFingixbeenbeenbeendone at time:done at time:done at time:ests Completedat 1Zcreatedcreatedcreatedwith with with 10 2 2IB = Z0IB = Z1IB = ZZuorkload mlf 1二*共共*共*共奖*共 Start Uor
5、kload Generator *q g g g g n n n n n o i o o o L F F L Fbeencreated-1Fhas lias been created lias been created is is isdone at time done at time doneat timeui th with with 10 1 1ID = 16ID = 17ID = 18rests Cornu leted. at 1Zuork1oad rr 10QStart Uorklodhas been createdhas been createdhas been createdis
6、 done at time:i s done at time:i s done at time:Generatorwith IBuith IBwith ID900it* *=12=13=14Comuleted at 11;uorkload rr 1?* Start Uokload Generator * rocess Longhas teen createduithID=9rocess Longis done at time:9rocess Pinghas been createduithID=9rocess Fonghas been createdwithID=IOrocess Fongis
7、 done at time:Qrocess Fingis dene at time;1 ests Completed at 122、信号量操作P、V操作代码如下:.nt P(int sid)if(!validateSID(sid)return EINVALID;bool atomic = Begin_Int_Atomic();if(g_Semaphoressid.resources = 0)Wait(&g_Semaphoressid.waitingThreads);KASSERT(g_Semaphoressid.resources =1);KASSERT(0 g_Semaphoressid.r
8、esources);g_Semaphoressid.resources-;End_Int_Atomic(atomic);return 0;int V(sid)if(!validateSID(sid)return EINVALID;bool atomic = Begin_Int_Atomic();g_Semaphoressid.resources+;if(g_Semaphoressid.resources = 1)Wake_Up_One(&g_Semaphoressid.waiting Threads);End_Int_Atomic(atomic);return 0;修改后的Ping.c代码如下
9、:#include #include #include #include #include int main(int argc , char * argv)int ij ;/* loop index */int scr_sem;/* id of screen semaphore */int time; /* current and start time */ int ping,pong; /* id of semaphores to syncprocesses b & c */time = Get_Time_Of_Day();scr_sem = Create_Semaphore (screen
10、 , 1);/* register for screen use */ping = Create_Semaphore (ping , 1);pong = Create_Semaphore (pong , 0);for (i=0; i 5; i+) P(pong);for (j=0; j 35; j+);time = Get_Time_Of_Day() - time;P(src_sem);Print(Process Ping is done at time: %dn,time);V(src_sem);V(ping);time = Get_Time_Of_Day() - time;P (scr_s
11、em);Print (Process Ping is done at time: %dn, time);V(scr_sem);Destroy_Semaphore(pong);Destroy_Semaphore(ping);Destroy_Semaphore(scr_sem); return (0);修改后的Pong.c代码如下:#include #include #include #include #include int main(int argc , char * argv)int i,j ;/* loop index */int scr_sem;/* id of screen semap
12、hore */int time; /* current and start time */int ping,pong; /* id of semaphores to sync processes b & c */time = Get_Time_Of_Day();scr_sem = Create_Semaphore (screen , 1);/* register for screen use */ping = Create_Semaphore (ping , 1); pong = Create_Semaphore (pong , 0); for (i=0; i 5; i+) P(ping);f
13、or (j=0; j 35; j+);time = Get_Time_Of_Day() - time;P(src_sem);Print(Process Pong is done at time : %dn,time);V(src_sem);V(pong);time = Get_Time_Of_Day() - time;P (scr_sem);Print (Process Pong is done at time: %dn, time);V(scr_sem);return (0);结果如图:e e e e ie ie ie e e e ie ie e e e e e e e e e44 4 4 4 4 5 4 5 4 5 4 8 8 8 8 8 8 8 _u 0U 8 H- K- 5533 5 53355345 5 34553455r