操作系统模拟实验:单处理机系统进程调度实验报告
验报告
数学与计算机学院
单处理机系统的进程调度 实验报告
王硕
年级07 学号 207429023 姓名 成绩 专业 信计
实验地点 主楼402 指导教师
实验项目 单处理机系统的进程调度 实验报告要求: 一、 1、 2、 3、 二、 三、 1、 2、 3、 4、 四、
实验目的
实验日期
加深对进程概念的理解,明确进程和程序的区别。 深入了解系统如何组织进程、创建进程。 进一步认识如何实现处理机调度。 实验原理 实验要求
采用时间片轮转调度算法实现进程调度。 确定进程控制块的内容,进程控制块的组织方式。 完成进程创建原语和进程调度原语。 编写主函数对所做工作进行测试。 实验结果(程序)及分析^p
#i nclude #defi ne N 10 #defi ne SLOT 5 //进程状态枚举 typedef enum { }
〃系统中所允许的最大进程数量 //时间片大小
第 1 页 共 7 页
} } } Running, Aready, Block ing
〃运行状态 〃就绪状态
//阻塞状态 } ProStatus;
//进程控制块 typedef struct {
//进程标识符 //进程标识符 //进程状态 //通用寄存器 //程序计数器寄存器 〃程序状态字寄存器 //指向下一个进程的指针
ProStatus status; int a,b,c,d; int pc; int psw; int ; } PCB;
//就绪队列指针 typedef struct {
int head; int tail; } Ready; //模拟寄存器 //PCB的静态链表
//PCB的静态链表 PCB pcbArea[N]; int run;
第 2 页 共 7 页
// 头指针 // 尾指针
Ready ready; int pfree; 〃模拟PCB区域的数组 〃运行状态程序的指针 //就绪队列指针 〃空闲队列的指针 //初始化运行状态进程指针 void Ini tRu n { run=-1; }
//初始化就绪状态队列 void Ini tReady {
ready.head=ready.tail=-1; //初始化空闲队列 void In itFree {
int temp;
for(temp=0;temp pcbArea[temp].=-1; pfree=0; } //就绪队列出队 int PopReady 〃返回结点在PCB区域数组的编号 { 第 3 页 共 7 页 int temp; if(ready.head==-1) { printf(“就绪队列为空,不能出队。\\n”); return -1; } temp=ready.head; ready.head=pcbArea[temp].; if(ready.head==-1) ready.tail=-1; pcbArea[temp].=-1; return temp; } 〃空闲队列出队 int PopFree 〃返回结点在PCB区域数组的编号 { int temp; if(pfree==-1) { printf(“空闲队列为空,不能出队。\\n”); return -1; } temp=pfree; pfree=pcbArea[temp].; pcbArea[temp].=-1; return temp; //就绪队列入队 void PushReady(int ) { int temp; 第 4 页 共 7 页 //为入队结点的编号 if(ready.head==-1) { ready.head=; ready.tail=; } else { temp=ready.tail; ready.tail=; } pcbArea[ready.tail].=-1; } 〃创建PCB void CreatePCB(int ,PCB pcb) { pcbArea[].a=pcb.a; pcbArea[].b=pcb.b; pcbArea[].c=pcb.c; pcbArea[].d=pcb.d; pcbArea[].name=pcb .n ame; pcbArea[].=-1; pcbArea[].pc=pcb.pc; pcbArea[].psw=pcb.psw; pcbArea[].status=pcb.status; } //创建进程函数 void Create(PCB pcb) { 第 5 页 共 7 页 //为要创建PCB在PCB区域数组的编号 int temp; if(pfree==-1) { printf(“空闲队列为空,不能创建进程。\\n”); return; } temp=PopFree; pcb.status=Aready; CreatePCB(temp,pcb); PushReady(temp); //进程调度函数 void Schedule。 { int temp; if(ready.head==-1) { printf(“系统内没有进程可以调度。”); return; } temp=PopReady(); pcbArea[temp].status=R unning; TIME=SLOT; //恢复 现场 A=pcbArea[temp].a; B=pcbArea[temp].b; C=pcbArea[temp].c; D=pcbArea[temp].d; PC=pcbArea[temp].pc; PSW=pcbArea[temp].psw; run=temp; //将选中的进程赋给运行指针 第 6 页 共 7 页 printf(“当前运行的程序:\\n”); //输出调度结果 printf(“进程号:d\\n”,pcbArea[run].name); printf(“进程状态:d\\n”,pcbArea[ru n].status); printf(“寄存器内容:\\nA\B\C\D\PC\PSW\\n”); prin tf(“d\d\d\d\d\d\\n”, pcbArea[ru n].a,pcbArea[ru n].b,pcbArea[ru n].c,pcbArea[ru n].d,pcbArea[ru n].pc,pcbArea[ru n ].psw); } void mai n { int temp; PCB tmppcb; printf(“请输入进程号,以负数为结束(进程号应保持唯一)。\\n\\n按任意键进入输入模式: ”); getchar; In itRu n; In itReady; In itFree; \\n“);printf(”请开始输入进程号: while(1) \\n“); { scan f(”d“, temp); if(tempvO) break; tmppcb.name=temp; tmppcb.a=temp; tmppcb.b=temp; tmppcb.c=temp; tmppcb.d=temp; tmppcb.pc=temp; tmppcb.psw=temp; Create(tmppcb); } prin tf(”\\nt;); Schedule; } 第 7 页 共 7 页 因篇幅问题不能全部显示,请点此查看更多更全内容