logo资料库

优先级和时间片轮转调度实验算法(c语言).docx

第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
资料共4页,全文预览结束
实验4 优先级和时间片轮转调度
实验 4 优先级和时间片轮转调度 一、实验要求 通过编写程序实现进程高优先权和按时间片轮转调度算法,使学生进一步掌握进程调度 的概念和算法,加深对处理机分配的理解。 二、实验内容 进程调度算法有优先数调度算法,时间片轮转调度算法,分级调度算法。选两种种算法 实现。进程调度算法的数据结构主要有:进程函数定义,建立进程函数,进程调度函数。 源程序代码部分: #include "stdio.h" #include #define max 100 #define etime 4 #define pfree 0 #define running 1 #define aready 2 #define blocking 3 typedef struct node { char name; int status; int pri; int ptime; int ax,bx,cx,dx; int pc; int psw; }pcb; pcb *processarray[max]; static int processnum; pcb *createprocess(pcb *processarray[]) 组 { pcb *p,*q; int a,b,c,d,m,n,r,t; char ID; q=NULL;
printf("input the first nine status pcb:"); scanf("\n%c%d%d%d%d%d%d%d%d",&ID,&a,&b,&c,&d,&m,&n,&r,&t); for(int i=0;iname=ID; p->ax=a; p->bx=b; p->cx=c; p->dx=d; p->pc=m; p->psw=n; p->pri=r; p->ptime=t; p->status=aready; processnum++; processarray[i]=p; printf("input the next seven status pcb: "); scanf("\n%c",&ID); if (ID == '*') break; scanf("%d%d%d%d%d%d%d%d",&a,&b,&c,&d,&m,&n,&r,&t); } for(int j=0;jname,q->status,q->ax,q->bx,q->c ptime\n "); x,q->dx,q->pc,q->psw,q->pri,q->ptime); } return *processarray; } void processRR(pcb *processarray[]) {int i; int j; pcb *q; int totaltime=0; for(i=0;i
{ if(processarray[i]->ptime%4==0) totaltime+=processarray[i]->ptime; else totaltime+=(processarray[i]->ptime/4+1)*4;} printf("\nUsing RR method:"); printf("\n process name. status.ax. bx. cx. dx. pc. psw. pri. for(i=0;iptime==0) ptime\n "); j=0; else if(q->ptime<=etime) { q->status=pfree; q->ptime=0; printf("\n%10c%5d%8d%5d%5d%5d%5d%5d%5d%5d",q->name,q->status,q->ax,q->bx,q->cx,q ->dx,q->pc,q->psw,q->pri,q->ptime); /*check process running status */ totaltime-=etime; } else if(q->ptime>etime) {q->status=running; q->ptime=q->ptime-etime; printf("\n%10c%5d%8d%5d%5d%5d%5d%5d%5d%5d",q->name,q->status,q->ax,q->bx,q-> cx,q->dx,q->pc,q->psw,q->pri,q->ptime); /*check process running status */ q->status=aready; totaltime-=etime;} if(i
for(j=0;jpri>processarray[j+1]->pri) {q=processarray[j]; processarray[j]=processarray[j+1]; processarray[j+1]=q;}} for(i=0;istatus=running; q=processarray[i]; printf("\n process name. status.ax. bx. cx. dx. pc. psw. pri. ptime\n "); printf("\n%10c%5d%8d%5d%5d%5d%5d%5d%5d%5d",q->name,q->status,q->ax,q->bx,q-> cx,q->dx,q->pc,q->psw,q->pri,q->ptime); /*check process running status */ q->status=pfree; } for(j=0;jname,q->status); } printf("\ngame is over!\n"); } void main() { *processarray=createprocess(processarray); processRR(processarray); processStaPri(processarray); } 三、实验报告 1.在源程序上写出注释。 2.画出程序流程图。 3.调试程序并写出运行结果。
分享到:
收藏