1、进程调度 实验报告实验一 进程调度 实验报告一、实验目的多道程序设计中,经常是若干个进程同时处于就绪状态,必须依照某种策略来决定那个进程优先占有处理机。因而引起进程调度。本实验模拟在单处理机情况下的处理机调度问题,加深对进程调度的理解。二、实验内容1优先权法、轮转法简化假设1)进程为计算型的(无I/O)2)进程状态:ready、running、finish3)进程需要的CPU时间以时间片为单位确定2算法描述1)优先权法动态优先权当前运行进程用完时间片后,其优先权减去一个常数。2)轮转法三、流程图四、实验要求1产生的各种随机数的取值范围加以限制,如所需的CPU时间限制在120之间。2进程数n不要
2、太大通常取48个3使用动态数据结构4独立编程5至少三种调度算法6若有可能请在图形方式下,将PCB的调度用图形成动画显示。五 实验源程序主函数 控制台Main.c#include #include#include fcfs.h#include privilege.h#include round.hvoid main() int n; cout输入随机产生的进程数目n; int choice=1; while (choice!=5) cout请输入你的选择endl; cout1:FCFS调度算法n2:动态优先权调度算法n3:时间片轮转法n4:清屏n5:退出choice; switch (choic
3、e) case 1: struct process *first=creatlist(n);print(first);fcfs(first);break; case 2: struct process1 *first1=creatlist1(n);print1(first1);privilege(first1);break; case 3:struct process2 *first=creatlist2(n);print2(first);round(first);break; case 4: system(cls);break; case 5: break; default: break;
4、/*/先来先服务调度算法:fcfs.h#ifndef FCFS_H#define FCFS_H#include#include#include#include#include #define random(x) (rand()%x)struct process int num; /进程号 int attime,servtime; /进程到达时间,服务时间 process *next; ;struct process*creatlist(int); /新建一个链表void insert(struct process *first,struct process *s); /插入一个节点(尾插法)v
5、oid print(struct process *first); /打印函数struct process*creatlist(int n) srand(int)time(0); struct process*first=new process; first-next=NULL; for (int i=0;inum=i; s-attime=i; s-servtime=random(20); insert(first,s); return first;void insert(struct process *first,struct process *s) struct process *r=fi
6、rst; struct process *p; while(r)p=r;r=r-next; p-next=s;p=s; p-next=NULL;void print(struct process *first) /打印函数 struct process *p; p =first-next; cout随机产生的进程的信息如下endl; cout进程名 进程到达时间 进程服务时间endl; while (p) coutnumtattimesetw(14)servtimenext; void fcfs(struct process *first) int startime=0;/开始执行时间 int
7、 finishtime=0;/完成时间 int wasttime=0;/周转时间 int weighttime=0;/带权周转时间 struct process *p=first-next; struct process *r; cout/*/endl; coutFCFS调度算法基本信息nendl; cout进程名 到达时间 服务时间 开始执行时间 完成时间周转时间 带权周转时间servtime + startime; wasttime= finishtime-p-attime; weighttime = wasttime / p-servtime; coutnumsetw(8)attimes
8、etw(10)servtimesetw(10)startimesetw(14)finishtimesetw(8)wasttimetweighttimenext; startime +=r-servtime; #endif动态优先权调度算法 privilege.h#ifndef PRIVILEGE_H#define PRIVILEGE_Hstruct process1 int pcb; /进程号PCB int privilege,cpu; /进程优先权,所需CPU时间 process1 *next; ;/*优先权调度算法所需函数声明*/struct process1*creatlist1();
9、/新建链表(就绪队列)void insert1(struct process1 *first,struct process1 *s); /增加一个进程或将一个进程查入到队列中void del1(struct process1 *first,struct process1 *s); /删除(撤销)一个进程void print1(struct process1 *first); /打印函数struct process1* search(struct process1 *head,struct process1 *s); /*优先权调度算法所需函数*/struct process1*creatlis
10、t1(int n) srand(int)time(0); struct process1 *first =new process1; first -next=NULL; for (int i=0;ipcb =i; s- privilege=random(20)+5; s-cpu=random(20)+1; insert1(first,s ); return first;void insert1(struct process1 *first,struct process1 *s) /插入节点 struct process1 *p=search(first,s); s-next=p-next; p
11、-next=s; /return;struct process1* search(struct process1 *first,struct process1 *s) /查找第一个到达时间大于等于AT的节点,返回其前一个指针 struct process1 *p,*q; p=first; q=first-next; while(q!=NULL&q-privilege=s-privilege) p=q; q=q-next; return p;void del1(struct process1 *first,struct process1 *s) struct process1 *p,*r; p=
12、first-next; r=first; int a=1; while (a) if (p=s) r-next=p-next; /free(p) ; a=0; else r=p; p=p-next; void print1(struct process1 *first) /打印函数 struct process1 *p; p =first-next; cout进程号 进程优先权 进程所需CPU片数endl; while (p) coutpcbsetw(8)privilegesetw(12)cpunext; void privilege(struct process1 *first) struc
13、t process1* p,*r=first,*t; p=first-next; int b=0; while (first-next) r=first; p=first-next; cout/*b*/endl; cout *进程运行前*cpu =p-cpu-1; p-privilege =p-privilege-3; if (p-privilegeprivilege =0; cout *进程运行后*endl; print1(r); cout/*/ncpu = 0) del1(r,p); else t=p; del1(r,p); insert1(r,p); b+; #endif时间片轮转调度算
14、法 round.h#ifndef ROUND_H#define ROUND_Hstruct process2 int pcb; /进程号PCB int roundpiece; /轮转时间片数 int needpiece; /进程所需时间片数 int cpupiece; /占用CPU时间片数 process2 *next; ;struct process2*creatlist2(int); /新建一个链表void print2(struct process2 *first); /打印函数void insert2(struct process2 *first,struct process2 *s)
15、; /插入节点void del2(struct process2 *first,struct process2 *s); /删除一个进程struct process2*creatlist2(int n) srand(int)time(0); struct process2*first=new process2; first-next=NULL; for (int i=0;ipcb=i; s-roundpiece=random(2)+1; s-needpiece=random(20); s-cpupiece=0; insert2(first,s); return first;void inser
16、t2(struct process2 *first,struct process2 *s) struct process2 *r=first; struct process2 *p; while(r)p=r;r=r-next; p-next=s;p=s; p-next=NULL;void del2(struct process2 *first,struct process2 *s) struct process2 *p,*r; p=first-next; r=first; int a=1; while (a) if (p=s) r-next=p-next; /free(p) ; a=0; el
17、se r=p; p=p-next; void print2(struct process2 *first) /打印函数 struct process2 *p; p =first-next; /cout随机产生的进程的信息如下endl; cout进程名 轮转时间片数 所需时间片数 占用时间片数endl; while (p) coutpcbsetw(8)roundpiecesetw(14)needpiecesetw(14)cpupiecesetw(5)next; void round(struct process2 *first) struct process2* p,*r=first; p=fi
18、rst-next; int b=0; while (first-next) /r=first; p=first-next; cout/*b*/endl; cout -进程运行前-needpiece =p-needpiece-1; p-cpupiece =p-cpupiece+1; cout -进程运行后-endl; print2(r); cout/*/nneedpiece cpupiece=p-roundpiece) p-cpupiece=0; del2(r,p); insert2(r,p); b+; #endif六 实验结果输入随机产生的进程数目 4输入选择调度算法 1输入选择调度算法 2输入选择调度算法 3七 实验总结 通过次实验模拟在单处理机情况下的处理机调度问题,加深了对进程调度的理解。 在次试验中,由于各个调度算法用到的数据结构不同,所以采用了不同的结构体来存放各个调度算法所用的信息。 在动态优先权调度算法中,当前进程用完时间片后,其优先权减去一个常数3,有可能出现优先权低于0的情况,在此情况下没有采取其它措施,后来经老师提醒改为低于0时 ,就将其优先权置为0,这样就好了。
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2