使用动态优先权的进程调度算法的模拟实验Word格式文档下载.docx
《使用动态优先权的进程调度算法的模拟实验Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《使用动态优先权的进程调度算法的模拟实验Word格式文档下载.docx(19页珍藏版)》请在冰豆网上搜索。
(5)为了清楚地观察诸进程的调度过程,程序应将每个时间片内的进程的情况显示出来,参照的具体格式如下:
RUNNINGPROG:
i
READY_QUEUE:
->id1->
id2
BLOCK_QUEUE:
->
id3->
id4
======================================================================
ID ﻩ0ﻩﻩ12ﻩﻩ3ﻩ4
PRIORITYﻩﻩP0P1 P2P3 P4
CPUTIME C0ﻩﻩC1ﻩC3ﻩﻩC4ﻩﻩC5
ALLTIMEﻩA0ﻩA1ﻩA2ﻩﻩA3ﻩﻩA4
STARTBLOCKT0ﻩT1ﻩT2ﻩT3ﻩT4
BLOCKTIMEﻩB0ﻩﻩB1ﻩB2B3ﻩB4
STATES0S1ﻩﻩS2ﻩS3ﻩS4
开始
创建就绪队列
Alltime>
就绪→执行
显示状态
改变优先数
P.alltime-1
P.cuptime+1
P.alltime==0
P.startblock>
P.startblock-1
P.startblock==0
执行→阻塞
执行→就绪
BLK==NULL
P.blocktime-1
P.blocktime==0
阻塞→就绪
结束
是
否
3.过程(流程图)
4.代码
#include<
stdio.h>
#include<stdlib.h>
#include<
string.h>
typedefstructnode
{
intid;
//进程标识数
ﻩintpriority;
//进程优先数,优先数越大优先级越高
intcputime;
//进程已占用的CPU时间
ﻩintalltime;
//进程还需占用的CPU时间
ﻩint startblock;
//进程的阻塞时间
ﻩintblocktime;
//进程被阻塞的时间
charstate[10];
//进程状态
structnode*next;
//队列指针
}PCB;
PCB*CreatQueue(intnum) //创建一个就绪队列
inti;
//i为循环计数器
PCB*head, *temp1,*temp2,*temp3;
//head为就绪队列的头指针,temp1为创建进程结点的指针,temp2、temp3分别为比较结点的前驱结点和比较结点
for(i=0;
i<
num;
i++) //根据进程的个数创建结点并按从大到小的顺序进行排序
ﻩ{
ﻩﻩtemp1=(PCB*)malloc(sizeof(PCB));
ﻩprintf("输入第%d个进程的(id…state)\n",i);
ﻩscanf("
%d%d%d%d%d%d%s"
,&temp1->
id,&
temp1->
priority,&
temp1->cputime,&
alltime,&
temp1->startblock,&
temp1->
blocktime,temp1->
state);
ﻩif(i==0) //如果创建的是第一个结点
ﻩﻩ{
ﻩﻩhead=temp1;
head->next=NULL;
continue;
ﻩ}
ﻩﻩif(head->
priority<
temp1->
priority) //如果创建结点中所保存的数比头结点所保存的数要大,则直接把该结点插入到头结点之前
temp1->
next=head;
ﻩhead=temp1;
continue;
ﻩﻩ}
ﻩtemp2=head;
//temp2为比较结点的直接前驱结点
ﻩtemp3=temp2->
next;
//temp3为比较的结点
ﻩﻩwhile(temp3!
=NULL&&
temp3->
priority>=temp1->
priority)//实现查找的功能
{
ﻩﻩﻩtemp2=temp3;
ﻩﻩﻩtemp3=temp2->
next;
}
ﻩﻩtemp2->
next=temp1;
temp1->
next=temp3;
ﻩ}
ﻩreturnhead;
}
PCB*InsertQueue(PCB*head,PCB*run)//在就绪队列中插入一个结点
PCB*temp1,*temp2;
//temp1和temp2分别为比较结点的前驱和比较结点
ﻩif(head==NULL) //如果就绪队列为空
ﻩhead=run;
head->
next=NULL;
}
elseif(head->
priority <
run->priority) //如果插入结点中所保存的数比头结点所保存的数要大,则直接把该结点插入到头结点之前
{
ﻩrun->
next=head;
ﻩhead=run;
}
ﻩelse
temp1=head;
//temp1为比较结点的直接前驱结点
ﻩtemp2=temp1->
next;
//temp2为比较的结点
ﻩwhile(temp2!
=NULL&
&
temp2->priority>
=run->priority) //实现查找的功能
ﻩ{
ﻩﻩ temp1=temp2;
ﻩtemp2=temp1->next;
}
temp1->next=run;
run->
next=temp2;
return head;
main()
intnum;
//num为进程的个数
ﻩint alltime=0;
//用来保存所有进程需要占用的CPU时间
PCB*head;
//head为就绪队列的头指针
PCB*run=NULL;
//run为执行进程结点的指针
PCB*block=NULL;
//block为阻塞进程的结点
ﻩPCB*temp;
ﻩprintf("
请输入进程的个数:
"
);
ﻩscanf("
%d",&num);
ﻩhead=CreatQueue(num);
ﻩgetchar();
ﻩtemp=head;
ﻩwhile(temp!
=NULL)
ﻩﻩalltime+=temp->alltime;
ﻩtemp=temp->
ﻩwhile(alltime>
0)
ﻩ{
if(head!
ﻩﻩ{
run=head;
//把就绪队列中的第一个进程取出来执行
ﻩ head=head->next;
//就绪队列的头指针指向下一个结点
strcpy(run->
state,"run");
//状态改为执行
ﻩ run->
next=NULL;
ﻩ /*显示状态*/
printf("RUNNINGPROG:
%d\n"
,run->
id);
//显示执行进程
printf("READY_QUEUE:
");
//显示就绪进程
ﻩ temp=head;
while(temp!
ﻩ{
ﻩ printf("
->%d"
temp->
id);
ﻩﻩ temp=temp->
next;
ﻩﻩﻩ}
printf("
\n"
);
printf("
BLOCK_QUEUE:
//显示阻塞进程
ﻩﻩif(block!
=NULL)
ﻩﻩ{
ﻩ printf("
%d"
block->
ﻩ printf("
\n"
ﻩprintf("
============================================================================\n"
ﻩﻩﻩprintf("IDﻩPRIORITYﻩCPUTIMEﻩALLTIMEﻩSTARTBLOCKBLOCKTIMEﻩSTATE\n"
ﻩﻩprintf("
%d%dﻩﻩ%dﻩ%dﻩ%dﻩ%dﻩ%s\n"
run->
id,run->
priority,run->
cputime,run->
alltime,run->startblock,run->blocktime,run->
state);
ﻩtemp=head;
ﻩwhile(temp!
=NULL)
ﻩﻩ{
ﻩﻩprintf("
%d%dﻩ%d%dﻩ%dﻩﻩ%dﻩ%s\n"
temp->id,temp->priority,temp->cputime,temp->
alltime,temp->
startblock,temp->
blocktime,temp->state);
ﻩﻩtemp=temp->
next;
if(block!
=NULL)
ﻩﻩ{
ﻩﻩ printf("
%d%dﻩ%dﻩ%dﻩ%dﻩ%d%s"
,block->
id,block->
priority,block->
cputime,block->
alltime,block->
startblock,block->
blocktime,block->
state);
ﻩ}
printf("