时间片轮转调度算法Word格式文档下载.docx
《时间片轮转调度算法Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《时间片轮转调度算法Word格式文档下载.docx(15页珍藏版)》请在冰豆网上搜索。
![时间片轮转调度算法Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2022-12/30/b32d3147-9353-4b2f-835f-9f83a4dc673d/b32d3147-9353-4b2f-835f-9f83a4dc673d1.gif)
CInstruction(InstructionSetiid,intrt)
m_nInstructionID=iid;
m_nRunTime=rt;
private:
CInstruction*m_pNextInstruction;
//用于链接一个进程的所有指令成为链表(指令序列)
intm_nRunTime;
//本指令需要运行的时间长度(定时器时间间隔的个数)
InstructionSetm_nInstructionID;
//指令类型标识
};
//进程控制块类
classPCB
PCB()
m_nPID=0;
m_csProcessName="
"
;
m_nRemainedTime=0;
//
m_pRuningInstruction=NULL;
m_pInstructionList=NULL;
m_pNextPCB=NULL;
//构造或创建一个进程
PCB(intpid,CStringpname)
m_nPID=pid;
m_csProcessName=pname;
~PCB()
CInstruction*pTemp;
while(m_pInstructionList)
m_pInstructionList=m_pInstructionList->
m_pNextInstruction;
pTemp=m_pInstructionList;
deletepTemp;
//本进程添加一条指令
voidAppendInstruction(CInstruction*pInstruction)
CInstruction*pTempInstruction;
if(m_pInstructionList==NULL)
//empty
m_pInstructionList=pInstruction;
else
//morethanonenode
pTempInstruction=m_pInstructionList;
while(pTempInstruction->
m_pNextInstruction!
=NULL)
pTempInstruction=pTempInstruction->
pTempInstruction->
m_pNextInstruction=pInstruction;
PCB*m_pNextPCB;
//进程队列的指针
intm_nPID;
//进程标识符
CStringm_csProcessName;
//进程名字
intm_nRemainedTime;
//当前运行指令运行还需要的时间
CInstruction*m_pRuningInstruction;
//指向正在运行或将要运行的指令
CInstruction*m_pInstructionList;
//指向本进程的指令序列(线性表)的第一条指令
PCB*m_pReadyPCBs;
//就绪队列
PCB*m_pBackupReadyPCBs;
//后备就绪队列
PCB*m_pInputWaittingPCBs;
//输入等待队列
PCB*m_pOutputWaittingPCBs;
//输出等待队列
PCB*m_pPureWaittingPCBs;
//其他等待队列
intm_nTimeSlice;
//时间片大小(定时器时间间隔的倍数)
voidLoadPCBs(CStringcsFileName);
//从文件中加载要试验的进程信息
voidRemoveProcess(PCB*pPCB);
//删除进程
voidDoSchedule();
voidRunOneTimeRange(PCB*pPCB,intnTime);
//运行一个时间段
voidTreadWaittingQueue(PCB*pWaittingPCBs);
//处理某个等待队列,适时将完成进程移出到后备就绪队列。
voidTreadAllWaittingQueues();
voidAppendReadyQueue(PCB*pPCB);
//pPCB所指节点添加到就绪队尾
voidAppendBackupReadyQueue(PCB*pPCB);
//pPCB所指节点添加到后备就绪队尾
voidAppendWaitQueue(PCB*pPCB);
//pPCB所指节点添加到等待队尾
voidAppendInputQueue(PCB*pPCB);
//pPCB所指节点添加到Input队尾
voidAppendOutputQueue(PCB*pPCB);
//pPCB所指节点添加到Output队尾
(2)#include<
stdio.h>
structpcb
charname;
inttime;
voidmain()
intn,i,j,flag=1;
structpcba[100];
printf("
输入程序个数:
);
scanf("
%d"
&
n);
getchar();
for(i=0;
i<
n;
i++)
输入程序的名字:
如ABC...\n"
%c"
a[i].name);
输入占用的时间片:
a[i].time);
i=0;
while(flag&
&
n>
0)
if(a[i].time!
=0)
a[i].name);
a[i].time--;
for(j=0;
j<
j++)
if(a[j].time)
flag=1;
break;
flag=0;
i=(++i)%n;
}
(3)时间片轮转调度算法
设计目的:
熟悉各种作业/进程调度算法的原理。
设计要求:
用高级语言编写和调试多个实现不同的作业/进程调度算法的程序。
设计内容:
基于时间片的轮转调度的模拟。
设计一个系统,提供一个界面,供用户输入等待调度的作业信息,显示调度的结果。
//实验名称:
//实验日期:
2008/07/16
//实验者:
qhu-hh
//实验环境:
VisualC++2008ExPressEdition
//编程语言:
C语言
//程序思想:
先模拟建立进程就绪链表----置所有进程的到达时间Parrive_time均为0,依PCB链接顺序从第一个进程PCB开始,
////////////////使Pid依次为1,2,3,4……;
就绪链表中进程的数量,由常量MAXSIZE控制;
////////////再模拟建立调度函数--------取表头PCB,修改进程执行时间,得到的新时间,即为剩余执行时间,当剩余时间小于或等于0
////////////////时,将此进程的PCB取出,依完成的先后次序链到完成链表中,记录当前完成进程的完成时间Pend_time,
////////////////同时修改就绪链表表头;
////////////最后计算和打印里程调度信息-----计算出各进程周转时间及所有进程的平均周转时间。
#include"
stdio.h"
malloc.h"
#defineTIMESLICE2//时间片;
#defineMAXSIZE4//就绪链中进程数量;
typedefstructPCB//定义进程控制块的信息结构;
intPid;
//进程标识号;
intParrive_time;
//进程到达时间;
intPend_time;
//进程结束时间;
intPexe_time;
//进程执行时间;
structPCB*next;
//链向一下进程;
}Node,*PNode;
PNodePCurrent=NULL;
//定义全局变量,指向当前进程的PCB;
PNodePLast=NULL;
//定义全局变量,指向上一进程PCB;
PNodePHead=NULL;
//定义全局变量,指向第一个进程的PCB;
PNodePTail=NULL;
//全局变量,指向就绪进程链的最后一个进程的PCB;
intExe_time[MAXSIZE];
booljudge=0;
//建立进程就绪链表
intCreate()
inti=1;
if(PCurrent==NULL)//就绪链表为空时;
{
PCurrent=(PNode)malloc(sizeof(Node));
PCurrent->
Pid=i;
i++;
//标识新建进程;
Parrive_time=0;
//初始化;
printf("
输入执行用时:
scanf("
(PCurrent->
Pexe_time));
//新进程执行用时;
Exe_time[i-2]=PCurrent->
Pexe_time;
//进程执行用时备份;
\n"
Pend_time=0;
next=NULL;
PHead=PTail=PCurrent;
//改变链头、链尾指向;
};
do
//标识新进程;
//初始化
PTail->
next=PCurrent;
//链到链尾
PTail=PCurrent;
//改变链尾
if(i>
MAXSIZE)judge=1;
}while(judge==0);
%d,%d\n"
PHead,PTail);
return0;
intPrin(PNodeP)//检验就绪链表中各进程PCB储存信息;
打印就绪链表:
while(P!
=NULL)//就绪链空时,检验完;
标识号:
%d,执行时间:
%d,到达时间:
%d\n"
P->
Pid,P->
Pexe_time,P->
Parrive_time);
P=P->
next;
}
打印结束:
//时间片轮转调度
PNodeTime_slice_dispatch(PNodePH,PNodePT)
I'
mTime_slice_dispatch
(1);
PNodeP_Head=NULL;
PNodeP_Tail=NULL;
//处理完成的进程组成的链表头结点和尾结点;
//PCurrent=PLast=NULL;
//PCurrent=PH;
intTime_count=0;
//计时器;
计时器初始值:
%d\n"
Time_count);
//printf("
PH);
while(PH!
=NULL)//就绪进程链表未处理完时,继续处理;
Time_count+=TIMESLICE;
//当前时间;
计时器值——————————————%d\n"
进程%d要求执行时间:
PH->
Pid,PH->
Pexe_time);
PH->
Pexe_time-=TIMESLICE;
//当前处理进程剩余时间;
进程%d剩余执行时间:
if(PH->
Pexe_time<
=0)//当前目标进程执行完时的处理方式;
if(P_Head==NULL)//
P_Head=P_Tail=PH;
//处于完成态的进程的链表的表头与表尾;
P_Head->
Pend_time=Time_count;
//记录第一个结束的进程的结束时间;
firstovertime:
………………%d\n"
PH=PH->
//改变就绪进程链表的表头;
PT->
P_Tail->
//结束进程的表尾next的域置空;
else
next=PH;
//链到结束进程的表尾;
P_Tail=PH;
//记录结束时间;
//P_Tail=PCurrent;
//改变结束进程的表尾;
//PCurrent=PCurrent->
/*PLast->
next=PCurrent->
*/
else//当前目标进程未处理完时的处理方式;
//将当前未完成的进程链到就绪链表表尾;
PT=PH;
///////////////////////////////////////////////**************///////////////////
//PLast=PCurrent;
//PCurrent;
//这两句是改变就绪进程链表的表头;
/*if(P_Head==NULL)
kkkkkkkkkkkkk"
intb;
b);
returnP_Head;
intPrint(PNodeP_H)
PCurrent=P_H;
charc;
doublea=0;
while(PCurrent!
\n进程标识号:
%d,执行时间:
%d,结束时间%d,周转时间:
PCurrent->
Pid,Exe_time[PCurrent->
Pid-1],PCurrent->
Pend_time,(PCurrent->
Pend_time)-(PCurrent->
Parrive_time));
a+=(PCurrent->
PCurrent=PCurrent->
平均周转时间:
%f"
a/MAXSIZE);
c);
c);
//////////////////////////////
intmain()
PNodehead;
Create();
Prin(PHead);
head=Time_slice_dispatch(PHead,PTail);
Print(head);
4
#include<
stdlib.h>
#defineMAX5//进程数量
#defineRR2//时间片大小
/*时间片轮转算法*/
structpro
intnum;
intarriveTime;
intburst;
intrt;
//记录进程被运行的次数
structpro*next;
intTOTALTIME;
//记录所有进程的总时间
//函数声明
structpro*creatList();
voidinsert(structpro*head,structpro*s);
structpro*searchByAT(structpro*head,intAT);
voiddel(structpro*p);
intgetCount(structpro*head,inttime);
structpro*searchEnd(structpro*head);
voidmove(structpro*headF,structpro*headT,intn);
structpro*creatList()//创建链表,按照进程的到达时间排列,记录所有进程的信息
structpro*head=(structpro*)malloc(sizeof(structpro));
head->
structpro*s;
inti;
TOTALTIME=0;
MAX;
s=(structpro*)malloc(sizeof(structpro));
请输入进程名:
(s->
num));
请输入到达时间:
arriveTime));
请输入运行时间:
burst));
TOTALTIME+=s->
burst;
//计算总时间
s->
rt=1;
//rt的初始值为1
insert(head,s);
returnhead;
//到达队列中的进程按照其到达时间的先后顺序排列
voidinsert(structpro*head,structpro*s)//插入节点
structpro*p=searchByAT(head,s->
arriveTime);
s->
next=p->
p->
next=s;
return;
structpro*searchByAT(structpro*head,intAT)//查找第一个到达时间大于等于AT的节点,返回其前一个指针
structpro*p,*q;
p=head;
q=head->
while(q!
=NULL&
q->
arriveTime<
=AT)
p=q