南昌大学实验报告
---(3)进程调度算法的实现
学生姓名:张皓然学号:5501215001专业班级:本硕151
实验类型:□ 验证 □ 综合 ■ 设计□ 创新 实验日期: 2017.5.31实验成绩:
一、实验目的
通过实验加强对进程调度算法的理解和掌握。
二、实验内容
编写程序实现进程调度算法,具体可以编写程序实现优先度高者调度算法或先来先服务算法。
三、实验要求
1、需写出设计说明; 2、设计实现代码及说明 3、运行结果;
四、主要实验步骤
1优先度高者调度算法实验代码: ○
#include
enum sta { run, wait, finished };//枚举类型sta,标志三种状态 time_t t; //system clock struct block {
int time_used; int prior;
};//定义结构体,使用时间,优先级 typedef struct pro {
char name[20]; enum sta status; int time_required;
int arrive_time;
struct block switch_block; struct pro *next; }*pcb;//定义pcb块 pcb ready; void menu();
void insertpcb(pcb newp) //插入至就绪队列 {
pcb p;
p = (pcb)malloc(sizeof(struct pro)); if (!p)exit(overflow); p = ready;
if (p->next == null) {
newp->next = ready->next; ready->next = newp; } else {
while (newp->switch_block.prior
p = p->next;
newp->next = p->next; p->next = newp; } }
void creat_pcb() //创建一个进程 {
pcb newp;
newp = (pcb)malloc(sizeof(struct pro)); if (!newp) exit(overflow); newp->status = wait; time(&t);
newp->arrive_time = t; printf(\ scanf(\ printf(\
scanf(\ printf(\
scanf(\ printf(\
scanf(\ newp->switch_block.time_used = 0; insertpcb(newp);
menu(); }
void printpcb() //打印现状 {
pcb p;
if (ready->next) {
printf(\ status priority server_time arrive_time cputime_used\\n\
for (p = ready->next; p != null; p = p->next) {
printf(\ switch (p->status) {
case wait:printf(\ case run:printf(\
case finished:printf(\ }
printf(\ printf(\ printf(\
printf(\ putchar('\\n'); } }
else printf(\}
void switchprocess() //将内存中的进程切换至外存 {
char temp[20]; pcb p, q;
if (ready->next) {
printf(\ scanf(\
for (p = ready; p != null&&strcmp(temp, p->next->name) != 0; p = p->next); if (p != null) {
q = p->next;
p->next = q->next;
printf(\ free(q); printpcb(); }
else printf(\
}
else printf(\ menu(); }
void runprocess() //进程调度 {
pcb p = ready->next; if (p) {
printf(\ p->status = run;
p->switch_block.prior--;
p->switch_block.time_used += runtime;
if (p->switch_block.time_used >= p->time_required) {
p->status = finished; printpcb();
ready->next = p->next;
printf(\ free(p); } else {
printpcb();
printf(\ p->status = wait;
ready->next = p->next; insertpcb(p); } }
else printf(\ menu(); }
void menu()//选择菜单 {
int coos;
printf(\ printf(\ printf(\ printf(\ printf(\ scanf(\ switch (coos) {
case 1:creat_pcb(); break; case 2:runprocess(); break; case 3:switchprocess(); break; case 4:exit(0); break; } }
int main() {
//clrscr();
ready = (pcb)malloc(sizeof(struct pro)); if (!ready) exit(overflow); ready->next = null; menu(); return 0;
实验过程:
1优先度高者调度算法实验结果截图: ○
先创建进程a、b、c,
设a进程优先级为2,到达时间为2,cpu处理时间为1; 设b进程优先级为5,到达时间为0,cpu处理时间为6; 设c进程优先级为4,到达时间为2,cpu处理时间为2;
设定进程a、b、c
开始运行进程,首先运行0时刻到达的优先级最高的进程b
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库南昌大学操作系统实验报告在线全文阅读。
相关推荐: