急用,谢谢:用C++编程实现时间片轮转调度算法

sherry_ws 2007-05-31 06:29:07
用C++编程实现时间片轮转调度算法
那位能够帮助我啊,小妹定当虚心求教,万分感激
谢谢
...全文
5927 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
CJ_star 2012-05-13
  • 打赏
  • 举报
回复
xiaoxiaojinoo 2012-01-09
  • 打赏
  • 举报
回复
要是包含最高响应比优先的内容就好了,我用得着
shy_stone666 2011-12-07
  • 打赏
  • 举报
回复
build没错,可我那个输进去是错误的呢 他会弹出一个 press any key to continue 点一下就关闭了 悲催
libraboy 2011-11-24
  • 打赏
  • 举报
回复
好程序。。
success_bra 2011-11-22
  • 打赏
  • 举报
回复
太谢谢了
喵咪先生lx 2010-11-23
  • 打赏
  • 举报
回复
很好,很强大!
Sakura162 2010-11-07
  • 打赏
  • 举报
回复
http://download.csdn.net/source/2814374
wuqing19890805 2010-11-06
  • 打赏
  • 举报
回复
功能看起来挺简单的,实际上算法比较复杂,考虑的也相当全面,要是在输出的时候加一些小技巧,给人看很有水准了
狂妄Beyond 2010-10-30
  • 打赏
  • 举报
回复
楼主强大!!
sherry_ws 2007-06-02
  • 打赏
  • 举报
回复
xiexie
谢谢谢谢谢谢
真不知道说什么好了
ayw215 2007-06-01
  • 打赏
  • 举报
回复
ls的ls太强了
yuyunliuhen 2007-06-01
  • 打赏
  • 举报
回复
LS很强啊
iu_81 2007-06-01
  • 打赏
  • 举报
回复
void Fcfs(LinkQueue& Q, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable)
{
totalTimeSum = 0;
weightTotalTimeSum = 0;//平均周转时间
QueueNode* p;
QueueNode* q;

p = Q.head->next;
if (p !=NULL )
{
ProcessTable[p->ID].startTime = ProcessTable[p->ID].arriveTime;
ProcessTable[p->ID].finishTime = ProcessTable[p->ID].arriveTime + ProcessTable[p->ID].reqTime;
}

for(q=p->next; q!=NULL; q=q->next)
{

if (ProcessTable[q->ID].arriveTime < ProcessTable[p->ID].finishTime)
{
ProcessTable[q->ID].startTime = ProcessTable[p->ID].finishTime;
ProcessTable[q->ID].finishTime = ProcessTable[p->ID].finishTime + ProcessTable[q->ID].reqTime;
}
else//下个进程到达时间较晚
{
ProcessTable[q->ID].startTime = ProcessTable[q->ID].arriveTime;
ProcessTable[q->ID].finishTime = ProcessTable[q->ID].arriveTime + ProcessTable[q->ID].reqTime;
}
p = q;
}

for(q=Q.head->next; q!=NULL; q=q->next)
{
ProcessTable[q->ID].totalTime = ProcessTable[q->ID].finishTime - ProcessTable[q->ID].arriveTime;
ProcessTable[q->ID].weightTotalTime = ProcessTable[q->ID].totalTime/ProcessTable[q->ID].reqTime;
totalTimeSum += ProcessTable[q->ID].totalTime;
weightTotalTimeSum += ProcessTable[q->ID].weightTotalTime;
}

int t = 0;
for(q=Q.head->next; q!=NULL; q=q->next)
{
cout<<"*********************"<<endl;
while ( t<ProcessTable[q->ID].finishTime )
{
cout<<"时刻"<<t<<": 进程"<<q->ID<<"活动"<<endl;
t++;
}
if (q->next != NULL)
{
cout<<"时刻"<<t<<": 进程"<<q->ID<<"结束活动,开始下一个进程."<<endl;
cout<<"进程"<<q->ID<<"的周转时间为: "<<ProcessTable[q->ID].totalTime<<endl;
cout<<"进程"<<q->ID<<"的带权周转时间为: "<<ProcessTable[q->ID].weightTotalTime<<endl<<endl;
}
else
{
cout<<"时刻"<<t<<": 进程"<<q->ID<<"结束活动."<<endl<<endl;
cout<<"进程"<<q->ID<<"的周转时间为: "<<ProcessTable[q->ID].totalTime<<endl;
cout<<"进程"<<q->ID<<"的带权周转时间为: "<<ProcessTable[q->ID].weightTotalTime<<endl<<endl;
}
}
cout<<"所有进程结束活动."<<endl<<endl;

p = Q.head;
for(q=p->next; q!=NULL; q=q->next)
{
delete p;
p = q;
}
}


void InitialQueue(LinkQueue& Q, PCBNode * ProcessTable,const int processnum)
{
//初始化

for (int i=0;i<processnum;i++)
{
ProcessTable[i].processID=i;
ProcessTable[i].reqTime=ProcessTable[i].remainTime;
ProcessTable[i].finishTime=0;
ProcessTable[i].startTime=0;
ProcessTable[i].status=WAIT;
ProcessTable[i].totalTime=0;
ProcessTable[i].weightTotalTime=0;
}

Q.head = new QueueNode;
Q.head->next = NULL;
QueueNode * p;
QueueNode * q;
for (i=0;i<processnum;i++)
{
p = new QueueNode;
p->ID = i;
p->next = NULL;
if (i == 0)
{
Q.head->next = p;
}
else
q->next = p;

q = p;
}
}


void Input(PCBNode * ProcessTable, const int processnum)
{
FILE *fp; //读入线程的相关内容
if((fp=fopen("input.txt","r"))==NULL)
{
cout<<"can not open file!"<<endl;
exit(0);
}
for(int i=0;i<processnum;i++)
{
fscanf(fp,"%d %d %d",&ProcessTable[i].arriveTime,&ProcessTable[i].remainTime,&ProcessTable[i].priorityNum);
}
fclose(fp);
}



建议输入数据:input.txt
0 4 0
1 35 1
2 10 2
3 5 3
6 9 4
7 21 5
9 35 6
11 23 7
12 42 8
13 1 9
14 7 10
20 5 11
23 3 12
24 22 13
25 31 14
26 1 15
iu_81 2007-06-01
  • 打赏
  • 举报
回复
// 时间片轮转调度算法
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>

using namespace std;
enum STATUS {RUN,READY,WAIT,FINISH};

struct PCBNode
{
int processID; //进程ID
STATUS status; //进程状态
int priorityNum; //优先数
int reqTime; //总的需要运行时间
int remainTime; //剩下需要运行时间
int arriveTime; //进入就绪队列时间
int startTime; //开始运行时间
int finishTime; //结束运行时间
int totalTime; //周转时间
float weightTotalTime; //带权周转时间
};

struct QueueNode
{
int ID; //进程ID
struct QueueNode * next; //队列中下一个进程指针
};

struct LinkQueue
{
QueueNode *head;//队首
};
void Fcfs(LinkQueue& Q, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable);
bool RR_Run(LinkQueue& Q,QueueNode* q, QueueNode* p, const int Round,int& currentTime,PCBNode * ProcessTable);
//分配时间片给q所指进程,p为刚退出的进程
void RoundRobin(LinkQueue& Q,const int Round, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable);
//时间片轮转调度,调用RR_Run(),时间片大小设为Round
void InitialQueue(LinkQueue& Q,PCBNode * ProcessTable,const int processnum);
//初始化就绪队列
void Input(PCBNode * ProcessTable, const int processnum);
//从input.txt文件输入数据

int main()
{
LinkQueue Q;//就绪队列
Q.head = NULL;
const int processnum = 16;//进程数
const int Round = 1; //时间片大小
int totalTimeSum = 0; //周转时间
int WeightTotalTimeSum = 0;//带权周转时间
PCBNode * ProcessTable=new PCBNode[processnum]; //进程表

Input(ProcessTable, processnum);
InitialQueue(Q, ProcessTable, processnum);
RoundRobin(Q, Round, totalTimeSum,WeightTotalTimeSum,ProcessTable);
cout<<"时间片轮调度的平均周转时间为:"<<totalTimeSum/processnum<<endl;
cout<<"时间片轮调度的平均带权周转时间为:"<<WeightTotalTimeSum/processnum<<endl;

Input(ProcessTable, processnum);
InitialQueue(Q, ProcessTable, processnum);
Fcfs(Q, totalTimeSum,WeightTotalTimeSum,ProcessTable);
cout<<"先来先服务的平均周转时间为:"<<totalTimeSum/processnum<<endl;
cout<<"先来先服务的平均带权周转时间为:"<<WeightTotalTimeSum/processnum<<endl;

delete [] ProcessTable;
return 0;
}

void RoundRobin(LinkQueue& Q,const int Round, int& totalTimeSum, int& weightTotalTimeSum,PCBNode * ProcessTable)
{
totalTimeSum = 0; //总的周转时间
weightTotalTimeSum = 0;//平均周转时间
int currentTime = 0; //当前时间
QueueNode* p;
QueueNode* q;
QueueNode* r;
bool finish = false;//调用RR_Run()后,该进程是否已经做完退出

p = Q.head;
q = p->next;
while (q != NULL)//从队首开始依次分配时间片
{
do
{
cout<<"**********************"<<endl;
cout<<"在时间片"<<(currentTime+1)/Round<<"内,活动进程为: "<<q->ID<<endl;
cout<<"进程"<<q->ID<<" 现在需要的时间片为: "<<ProcessTable[q->ID].remainTime<<endl;
finish = RR_Run(Q, q, p, Round, currentTime, ProcessTable);//分配时间片给q进程
cout<<endl;

if (!finish)//若是进程在本时间片内做完,则跳出do…while循环
{
if (q->next == NULL)
{
r = Q.head->next;
}
else
{
r = q->next;
}
}
else //否则计算周转时间和带权周转时间
{
totalTimeSum += ProcessTable[q->ID].totalTime;
weightTotalTimeSum += ProcessTable[q->ID].weightTotalTime;

delete q; //从队列中删除q进程
q = p;
}
}while (!finish && (ProcessTable[r->ID].arriveTime > currentTime + Round));
//下一个进程很晚才来,则继续给当前进程分配时间片

p = q;
q = q->next;

if (q == NULL && Q.head->next!=NULL)
{
p = Q.head;
q = p->next;
}
}
delete Q.head;
Q.head = NULL;
}

bool RR_Run(LinkQueue& Q,QueueNode* q, QueueNode* p, const int Round,int& currentTime,PCBNode * ProcessTable)
{
if (ProcessTable[q->ID].remainTime <= Round)//在此时间片内能够做完,之后退出进程调度
{
ProcessTable[q->ID].finishTime = currentTime + ProcessTable[q->ID].remainTime;
ProcessTable[q->ID].totalTime += ProcessTable[q->ID].remainTime;
ProcessTable[q->ID].weightTotalTime = ProcessTable[q->ID].totalTime/ProcessTable[q->ID].reqTime;

currentTime = ProcessTable[q->ID].finishTime;

p->next = q->next;
cout<<endl;
cout<<"进程"<<q->ID<<"完成!"<<endl;

return true;
}
else//此时间片内做不完
{
ProcessTable[q->ID].remainTime = ProcessTable[q->ID].remainTime - Round;
ProcessTable[q->ID].totalTime += Round;
currentTime += Round;

return false;
}
}


sherry_ws 2007-06-01
  • 打赏
  • 举报
回复
在VC 里面运行的
sherry_ws 2007-06-01
  • 打赏
  • 举报
回复
就用C++做啊,大哥,帮帮忙啊
leibniz_zsu 2007-05-31
  • 打赏
  • 举报
回复
不好意思,乱码了。
是在windows平台上做吗?
leibniz_zsu 2007-05-31
  • 打赏
  • 举报
回复
涓嶇煡閬撴槸鍦ㄤ粈涔堝钩鍙颁笂瀹炵幇鍛

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧