2023年操作系统模拟实验单处理机系统的进程调度实验报告.docx
数学与计算机学院 单解决机系统的进程调度 实验报告年级旦 学号 姓名 王阳 成绩专业 信计实验地点主楼402 指导教师 .王硕实验项目单解决机系统的进程调度 实验日期实验报告规定:一、实验目的1、加深对进程概念的理解,明确进程和程序的区别。2、进一步了解系统如何组织进程、创建进程。3、进一步结识如何实现解决机调度。二、实验原理三、实验规定1、采用时间片轮转调度算法实现进程调度。2、拟定进程控制块的内容,进程控制块的组织方式。3、完毕进程创建原语和进程调度原语。4、编写主函数对所做工作进行测试。四、实验结果(程序)及分析i n c 1 u de <s t dio. h ># defi neN 1 0/系统中所允许的最大进程数量def i neSLOT 5/时间片大小 进程状态枚举ty p ed e f e n umu nning, / /运营状态A r cady, 就绪状态Block i ng/阻塞状态 Pro Status;进程控制块type def struct(oi n t n a me;ProStat u s s t atu s ;3 i nt ax,bx,cx, d x ;1 n t pc;int psw;“nt n e xt;)PCB;/就绪队列指针/进程标记符进程状态/通用寄存器/程序计数器寄存器程序状态字寄存器/指向下一个进程的指针t y pedef stru c t(<>int head;头指针。i nt t a il;尾指针 Re a dy;模拟寄存器in t PSW,AX,BX,CX, DX,PC,TIME;/PCB的静态链表/模拟PCB区域的数组/运营状态程序的指针/就绪队列指针/空闲队列的指针/模拟PCB区域的数组/运营状态程序的指针/就绪队列指针/空闲队列的指针PCB pcbA r eaN;int run;Rea d y ready;i n t p f ree;。/初始化运营状态进程指针void InitRun()(r u n=-l;)初始化就绪状态队列void InitRea d y()(ready.h e ad=rc a dy. tai 1 =-l;)初始化空闲队列void In i t F r ee ()。i n t temp;f o r(temp=O;tem p <N- 1 ; t emp+)(6 pc b A r e a temp, n ex t = t e mp+ 1 ;叩c b Areat e mp.n e x t =-l;。p f ree=O;。/ /就绪队列出队i n t PopReadyO/ /返回结点在P CB区域数组的编号(1 n t temp;A f (re a d y .head= 1)°。p r i mf("就绪队列为空,不能出队。 n ”);。 re t urn -1;。®tcmp= r cady.h e a d;«ready.head=pcbAreaft e mp.nex t ;» i f( r ea d y.head=-l)。 rea d y. t ai 1 =-l ;p c bArealtemp. n e x t= - 1 ;« r e t urn temp;)。/空闲队列出队i nt PopFree ()/返回结点在PC B区域数组的编号(« i n t temp;i f (P free=-1 ) gprintf ("空闲队列为空,不能出队。n ");g r eturn -1 ;6)®temp=p f re e ;®p f r e e= p c b Are a t emp. next;p c b Are a t e mpj.n e x t =-l;ar e t u rn tem p ;)。就绪队列入队vo i d PushR e a d y (int x )x为入队结点的编号(®int t emp;。i f (ready.he a d =-l)(。r eady. head=x;ready.tai 1= x ;®else。(«temp=rea d y. t ail;。代 a d y.t a i 1 =x;pcbArcarc a dy. t ailj.ncxt=-l ;/ /创建PCBv o id Cr e atePCB(int x,PCB p c b)/x为要创建PCB在PCB区域数组的编叩cbAre a x .a x = p cb. a x;叩 c b AreaxJ.bx= p cb.bx;®p c bA r e a x. cx = pc b . ex;p c b A r e a xj.d x = p c b . d x;p c bAre a x.name= p cb. n ame;p cbAreaf x . n ext=-1 ;®p c bAreafx . p c= p c b .pc;®p c bArea x. p s w = p cb. p sw;pcbArea x . s tatus = pcb.statu s ;)创建进程函数void C r e a t e (PCB peb)(in t temp;fif(pfr e e =-l)。print f ("空闲队列为空,不能创建进程。);。 ret u rn;)temp = Pop F ree();叩cb.stat u s =Ar e a dy;Crea t e PCB(temp, peb);PushRe a d y ( t e mp);进程调度函数v oid Sc h e d ule() (in t t e mp;i f (r e ady.hea d =-l) (平r in t f("系统内没有进程可以调度。) ret u m;。t emp= P o p Re a dy ();叩c b A r eatem p . s t at u s =Run n i ng;。T IME=SLOT;/恢复 C PU 现场»AX=p c bAreatemp.ax;BX = pcbA r e a t e mpj. b x; CX=pcbArea temp, ex;X = p cb A r e a tempj. dx;«PC=pcbA r e a tem p . pc; PSW=pcbAr e a temp.psw;。r un = t emp;将选中的进程赋给运营指针叩rin tf("当前运营的程序:n");/输出调度结果叩 r intf ("进程号:d n ", p c bA r earun.name);p rintf("进程状态:%d n " ,pcbA r e a r un.s t atus);pr i ntf( H 寄存器内容:WAX' t BXtCX t DXtP C tPS Wn "); oprintf (M%dt%dt%d t %dt%dt%dn",® p c b Are a runj. a x, pcbArea run, b x,p c bA r e aru n J.cx, p c b Are a r un.dx,pc bA r e ar u n. p c,p c bA r e a r u n.psw);)vo i d m a i n ()(i n t temp;PCB tmp_pcb;prin tf(”请输入进程号,以负数为结束(进程号应保持唯一)。nn按任意键进入输入模 式:”);gctchar();InitRun();InitReadyO;In i tFr e e ();P rinlf("请开始输入进程号:n”);wh i 1 e(l)。wscanf("%dH,&t e m p );if(temp<0)b r eak;tmp_ p c b. n ame = t em p ;t mp_p c b .a x =temp;t mp_pc b . bx= t emp;0tmp_p c b.cx = t e mp;0tm p _pcb. d x = temp;tmp_ p cb.pc= t emp;tmp_ p c b .psw=temp;«Crcatc(tmp_pcb);“)叩 rint f ( " n");S ch e dule();