1、数理系操作系统原理实验指导书操作系统实验指导书实验一 进程调度1目的和要求进程调度是处理机管理的核心内容。本实验要求用一种语言编写和调试一个简单的进程调度程序。通过本实验可以加深理解有关进程控制块、进程队列的概念,并体会和了解优先数和时间片轮转调度算法的具体实施办法。2实验内容设计进程控制块PCB表结构,分别适用于优先数调度算法和循环轮转调度算法。建立进程就绪队列。对两种不同算法编制入队子程序。编制两种进程调度算法:1)优先数调度;2)循环轮转调度3实验环境Windows系统,语言自选(建议C+)4实验提示本程序用两种算法对五个进程进行调度,每个进程可有三个状态,并假设初始状态为就绪状态。为了
2、便于处理,程序中的某进程运行时间以时间片为单位计算。各进程的优先数或轮转时间数以及进程需运行的时间片数的初始值均由用户给定。在优先数算法中,优先数可以先取值为98,进程每执行一次,优先数减3,CPU时间片数加1,进程还需要的时间片数减1。在轮转算法中,采用固定时间片(即:每执行一次进程,该进程的执行时间片数为已执行了2个单位),这时,CPU时间片数加2,进程还需要的时间片数减2,并排列到就绪队列的尾上。对于遇到优先数一致的情况,采用FIFS策略解决。5、学时数:2个学时程序代码如下:#include#include #include#include#include#define P_NUM 5
3、#define P_TIME 50enum state ready, execute, block, finish;struct pcb char name4; int priority; int cputime; int needtime; int count; int round; state process; pcb * next;pcb * get_process();pcb * get_process() pcb *q; pcb *t; pcb *p; int i=0; coutinput name and timeendl; while (iq-name; cinq-needtim
4、e; q-cputime=0; q-priority=P_TIME-q-needtime; q-process=ready; q-next=NULL; if (i=0) p=q; t=q; else t-next=q; t=q; i+; /while return p;void display(pcb *p) coutname cputime needtime priority stateendl; while(p) coutname; cout ; coutcputime; cout ; coutneedtime; cout ; coutpriority; coutprocess) case
5、 ready:coutreadyendl;break; case execute:coutexecuteendl;break; case block:coutblockendl;break; case finish:coutfinishnext; int process_finish(pcb *q) int bl=1; while(bl&q) bl=bl&q-needtime=0; q=q-next; return bl;void cpuexe(pcb *q) pcb *t=q; int tp=0; while(q) if (q-process!=finish) q-process=ready
6、; if(q-needtime=0) q-process=finish; if(tppriority&q-process!=finish) tp=q-priority; t=q; q=q-next; if(t-needtime!=0) t-priority-=3; t-needtime-; t-process=execute; t-cputime+; void priority_cal() pcb * p; p=get_process(); int cpu=0; while(!process_finish(p) cpu+; coutcputime:cpuendl; cpuexe(p); dis
7、play(p); printf(All processes have finished,press any key to exit); getch();void display_menu() coutCHOOSE THE ALGORITHM:endl; cout1 PRIORITYendl; cout2 ROUNDROBINendl; cout3 EXITendl;pcb * get_process_round() pcb *q; pcb *t; pcb *p; int i=0; coutinput name and timeendl; while (iq-name; cinq-needtim
8、e; q-cputime=0; q-round=0; q-count=0; q-process=ready; q-next=NULL; if (i=0) p=q; t=q; else t-next=q; t=q; i+; /while return p;void cpu_round(pcb *q) q-cputime+=2; q-needtime-=2; if(q-needtimeneedtime=0; q-count+; q-round+; q-process=execute;pcb * get_next(pcb * k,pcb * head) pcb * t; t=k; do t=t-ne
9、xt; while (t & t-process=finish); if(t=NULL) t=head; while (t-next!=k & t-process=finish) t=t-next; return t;void set_state(pcb *p) while(p) if (p-needtime=0) p-process=finish; if (p-process=execute) p-process=ready; p=p-next; void display_round(pcb *p) coutNAME CPUTIME NEEDTIME COUNT ROUND STATEend
10、l; while(p) coutname; cout ; coutcputime; cout ; coutneedtime; cout ; coutcount; cout ; coutround; coutprocess) case ready:coutreadyendl;break; case execute:coutexecuteendl;break; case finish:coutfinishnext; void round_cal() pcb * p; pcb * r; p=get_process_round(); int cpu=0; r=p; while(!process_fin
11、ish(p) cpu+=2; cpu_round(r); r=get_next(r,p); coutcpu cpuendl; display_round(p); set_state(p); void main() display_menu(); int k; scanf(%d,&k); switch(k) case 1:priority_cal();break; case 2:round_cal();break; case 3:break; display_menu(); scanf(%d,&k); 6. 设计两组实验数据,记录实验结果并分析。实验二 银行家算法避免死锁实验一、 实验目的:1)
12、 理解死锁基本概念,掌握产生死锁的原因和必要条件以及处理死锁的几种方法,体会银行家算法是避免死锁的一种行之有效的方法。2) 通过编写程序实现银行家算法,进一步理解银行家算法的原理和避免死锁的过程,掌握银行家算法的描述和应用,进一步熟练掌握处理处理机死锁的方法。二、 实验内容(1)首先对银行家算法原理进行深刻的理解和掌握;(2)选择一种熟悉的编程语言来实现对N个进程和M种系统资源进行银行家算法判定和资源分配;(3)根据银行家算法的要求设计相应的数据结构,如:可利用资源向量、最大需求矩阵、分配矩阵和需求矩阵,其中进程的个数N和资源的种类数目用户可以任意交互输入;(4)安全性判定要显示过程,要给出找到的安全序列。三、 实验环境Windows系统,语言自选(建议C+)四、 实验提示 1、初始化由用户输
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1