【水】【晒代码】苦逼的我表示代码有问题
拒绝悲伤之仙剑传奇吧
全部回复
仅看楼主
level 13
我知道星辰在这上面造诣颇深,所以一楼就召唤了他。
@乱星辰
2L开始放代码
2012年04月14日 12点04分 1
level 9
咱们能换一个排版么? 还有这是做什么的? 哪有问题 一次性玩清楚 回来帮你看~
2012年04月14日 12点04分 3
正在换各种浏览器发帖- -!因为网速不行,IE太烂发不出来。。Opera和Chrome都不换行。。
2012年04月14日 12点04分
这个是模拟操作系统的四种进程调度算法,FCFS, SJF, RR, MLFS
2012年04月14日 12点04分
level 13
//FCFS.h
//FCFS algorithm inherited from SchedulingAlgo
#ifndef FCFS_H
#define FCFS_H
#include "SchedulingAlgo.h"
class FCFS : public SchedulingAlgo
{
public:
FCFS(int arriveTime[], int burstTime[]);
void simulation();
};
#endif
2012年04月14日 12点04分 5
level 13
//RR.h
//RR algorithm inherited from SchedulingAlgo
#ifndef RR_H
#define RR_H
#include "SchedulingAlgo.h"
class RR : public SchedulingAlgo
{
public:
RR(int arriveTime[], int burstTime[]);
void simulation();
};
#endif
2012年04月14日 12点04分 6
level 13
//SJF.h
//SJF algorithm inherited from SchedulingAlgo
#ifndef SJF_H
#define SJF_H
#include "SchedulingAlgo.h"
class SJF : public SchedulingAlgo
{
public:
SJF(int arriveTime[], int burstTime[]);
void simulation();
};
#endif
2012年04月14日 12点04分 7
level 13
//MLFS.h
//MLFS algorithm inherited from SchedulingAlgo
#ifndef MLFS_H
#define MLFS_H
#include "SchedulingAlgo.h"
class MLFS : public SchedulingAlgo
{
public:
MLFS(int arriveTime[], int burstTime[]);
void simulation();
};
#endif
2012年04月14日 12点04分 8
level 13
//SchedulingAlgo.cpp
//Implementation of class SchedulingAlgo
#include "SchedulingAlgo.h"
SchedulingAlgo::SchedulingAlgo(int arriveTime[], int burstTime[])
{
for (int i=0; i<JOB_AMOUNT; i++)
{
setArriveTime(i, arriveTime[i]);
setBurstTime(i, burstTime[i]);
setFinishTime(i, 0);
}
}
void SchedulingAlgo::setArriveTime(int offset, int value)
{
m_iArriveTime[offset] = value;
}
int SchedulingAlgo::getArriveTime(int offset) const
{
return m_iArriveTime[offset];
}
void SchedulingAlgo::setBurstTime(int offset, int value)
{
m_iBurstTime[offset] = value;
}
int SchedulingAlgo::getBurstTime(int offset) const
{
return m_iBurstTime[offset];
}
void SchedulingAlgo::setFinishTime(int offset, int value)
{
m_iFinishTime[offset] = value;
}
int SchedulingAlgo::getFinishTime(int offset) const
{
return m_iFinishTime[offset];
}
SchedulingAlgo::~SchedulingAlgo()
{
for (int j=0; j<JOB_AMOUNT; j++)
{
setArriveTime(j, 0);
setBurstTime(j, 0);
}
}
2012年04月14日 12点04分 9
level 13
//FCFS.cpp
//Implementation of class FCFS
#include "FCFS.h"
#include <queue>
#include <iostream>
#include <iomanip>
using namespace std;
FCFS::FCFS(int arriveTime[], int burstTime[])
: SchedulingAlgo(arriveTime, burstTime)
{
}
void FCFS::simulation()
{
int remainingJobAmount = JOB_AMOUNT;//未完成的任务数
int time = 0;//计时器
int i = 0;
queue <int> readyQueue;//就绪队列
while (remainingJobAmount > 0)
{
for (i=0; i<JOB_AMOUNT; i++)
{
if (m_iArriveTime[i] == time)
readyQueue.push(i);//把一项任务加入就绪队列
}
if (!readyQueue.empty())
m_iBurstTime[readyQueue.front()]--;
if (!readyQueue.empty() && m_iBurstTime[readyQueue.front()] == 0)//一项任务已完成
{
m_iFinishTime[readyQueue.front()] = time;
remainingJobAmount--;
readyQueue.pop();
}
time++;
}
//输出平均周转时间
int turnaroundTime[JOB_AMOUNT];
int totalTime = 0;
cout << "FCFS:" << endl;
//cout << "The turnaround times under FCFS are as follows:" << endl;
for (i=0; i<JOB_AMOUNT; i++)
{
turnaroundTime[i] = m_iFinishTime[i] - m_iArriveTime[i];
totalTime += turnaroundTime[i];
//cout << char('A' + i) << ":" << setw(6) << turnaroundTime[i] << endl;
}
cout << "The average turnaround time is:" << setprecision(4) << static_cast<double>(totalTime) / JOB_AMOUNT << endl;
}
2012年04月14日 12点04分 10
level 13
//SJF.cpp
//Implementation of class SJF
#include "SJF.h"
#include <iostream>
#include <iomanip>
#include <queue>
using namespace std;
SJF::SJF(int arriveTime[], int burstTime[])
: SchedulingAlgo(arriveTime, burstTime)
{
}
void SJF::simulation()
{
int remainingJobAmount = JOB_AMOUNT;//未完成的任务数
int time = 0;//计时器
int i = 0;
int running = -1;//-1表示当前任务已经执行完,自然数表示正在执行的任务编号
priority_queue<int, vector<int>, greater<int> > readyQueue;
while (remainingJobAmount > 0)
{
for (i=0; i<JOB_AMOUNT; i++)
{
if (m_iArriveTime[i] == time)
readyQueue.push(i);//把一项任务加入就绪优先级队列
}
if (!readyQueue.empty() && running == -1)//除正在运行任务外的就绪队列不空且没有正在运行的任务则从队列中取出一个任务
{
running = readyQueue.top();
readyQueue.pop();
}
if (running != -1)//有正在运行的任务
m_iBurstTime[running]--;
if (m_iBurstTime[running] == 0)//一项任务已完成
{
m_iFinishTime[running] = time;
remainingJobAmount--;
running = -1;
}
time++;
}
//输出平均周转时间
int turnaroundTime[JOB_AMOUNT];
int totalTime = 0;
cout << "SJF (Non-preemptive):" << endl;
//cout << "The turnaround times under SJF are as follows:" << endl;
for (i=0; i<JOB_AMOUNT; i++)
{
turnaroundTime[i] = m_iFinishTime[i] - m_iArriveTime[i];
totalTime += turnaroundTime[i];
//cout << char('A' + i) << ":" << setw(6) << turnaroundTime[i] << endl;
}
cout << "The average turnaround time is:" << setprecision(4) << static_cast<double>(totalTime) / JOB_AMOUNT << endl;
}
2012年04月14日 12点04分 11
level 13
//RR.cpp
//Implementation of class RR
#include "RR.h"
#include <iostream>
#include <iomanip>
#include <queue>
using namespace std;
RR::RR(int arriveTime[], int burstTime[])
: SchedulingAlgo(arriveTime, burstTime)
{
}
void RR::simulation()
{
int remainingJobAmount = JOB_AMOUNT;//未完成的任务数
int time = 0;//计时器
int i = 0;
queue <int> readyQueue;//就绪队列
while (remainingJobAmount > 0)
{
for (i=0; i<JOB_AMOUNT; i++)
{
if (m_iArriveTime[i] == time)
readyQueue.push(i);//把一项任务加入就绪队列
}
m_iBurstTime[readyQueue.front()]--;
if (m_iBurstTime[readyQueue.front()] != 0)//当前任务尚未完成,取出置队尾
{
readyQueue.push(readyQueue.front());
readyQueue.pop();
}
else//当前任务完成
{
m_iFinishTime[readyQueue.front()] = time;
readyQueue.pop();
remainingJobAmount--;
}
time++;
}
//输出平均周转时间
int turnaroundTime[JOB_AMOUNT];
int totalTime = 0;
cout << "RR (time slice is 1):" << endl;
//cout << "The turnaround times under RR are as follows:" << endl;
for (i=0; i<JOB_AMOUNT; i++)
{
turnaroundTime[i] = m_iFinishTime[i] - m_iArriveTime[i];
totalTime += turnaroundTime[i];
//cout << char('A' + i) << ":" << setw(6) << turnaroundTime[i] << endl;
}
cout << "The average turnaround time is:" << setprecision(4) << static_cast<double>(totalTime) / JOB_AMOUNT << endl;
}
2012年04月14日 12点04分 12
level 13
//MLFS.cpp
//Implementation of class MLFS
#include "MLFS.h"
#include <queue>
#include <iostream>
#include <iomanip>
using namespace std;
MLFS::MLFS(int arriveTime[], int burstTime[])
: SchedulingAlgo(arriveTime, burstTime)
{
}
void MLFS::simulation()
{
int remainingJobAmount = JOB_AMOUNT;//未完成的任务数
int time = 0;//计时器
int i = 0;
int j = 0;//q_RR2队列中的任务的已执行时间,2个单位为一个循环
int runningQueue = 0;//当前执行的任务所在的队列,下面三个队列对应的分别为0, 1, 2
queue <int> q_RR1;//时间片为1的RR就绪队列
queue <int> q_RR2;//时间片为2的RR就绪队列
queue <int> q_FCFS;//FCFS就绪队列
while (remainingJobAmount > 0)
{
for (i=0; i<JOB_AMOUNT; i++)
{
if (m_iArriveTime[i] == time)
q_RR1.push(i);//把一项任务加入q_RR1队列
}
if (runningQueue == 0 && j == 0)//q_RR1队列
{
if (!q_RR1.empty())
{
m_iBurstTime[q_RR1.front()]--;
if (m_iBurstTime[q_RR1.front()] != 0)//当前任务尚未完成,取出置下一级队列队尾
{
q_RR2.push(q_RR1.front());
q_RR1.pop();
}
else//当前任务完成
{
m_iFinishTime[q_RR1.front()] = time;
q_RR1.pop();
remainingJobAmount--;
}
}
runningQueue = 1;
}
else if (runningQueue == 1)//q_RR2队列
{
if (!q_RR2.empty())
{
j = (j + 1) % 2;
m_iBurstTime[q_RR2.front()]--;
if (m_iBurstTime[q_RR2.front()] != 0)//当前任务尚未完成,取出置下一级队列队尾
{
q_FCFS.push(q_RR2.front());
q_RR2.pop();
}
else//当前任务完成
{
m_iFinishTime[q_RR2.front()] = time;
q_RR2.pop();
remainingJobAmount--;
j = 0;
}
}
if (j == 0)
runningQueue = 2;
}
else//FCFS队列
{
if (!q_FCFS.empty())
m_iBurstTime[q_FCFS.front()]--;
if (!q_FCFS.empty() && m_iBurstTime[q_FCFS.front()] == 0)//一项任务已完成
{
m_iFinishTime[q_FCFS.front()] = time;
remainingJobAmount--;
q_FCFS.pop();
runningQueue = 0;
}
}
time++;
}
//输出平均周转时间
int turnaroundTime[JOB_AMOUNT];
int totalTime = 0;
cout << "MLFS (time slice 1, 2 and FCFS):" << endl;
//cout << "The turnaround times under MLFS are as follows:" << endl;
for (i=0; i<JOB_AMOUNT; i++)
{
turnaroundTime[i] = m_iFinishTime[i] - m_iArriveTime[i];
totalTime += turnaroundTime[i];
//cout << char('A' + i) << ":" << setw(6) << turnaroundTime[i] << endl;
}
cout << "The average turnaround time is:" << setprecision(4) << static_cast<double>(totalTime) / JOB_AMOUNT << endl;
}
2012年04月14日 12点04分 13
level 13
//TurnAroundTime.cpp
//The testing file including main function
#include <iostream>
#include <iomanip>
#include "SJF.H"
#include "FCFS.H"
#include "MLFS.H"
#include "RR.H"
using namespace std;
int main()
{
int i, j;
/*
*仿真因素定义及初始化
*/
int arriveTime1[JOB_AMOUNT];
int arriveTime2[JOB_AMOUNT];
int arriveTime3[JOB_AMOUNT];
int burstTime1[JOB_AMOUNT];
int burstTime2[JOB_AMOUNT];
for (i=0, j=0; i<JOB_AMOUNT; i++, j+=2)
{
arriveTime1[i] = j;
burstTime1[i] = (i < (JOB_AMOUNT >> 1)) ? 3 : 9;
burstTime2[i] = (i < (JOB_AMOUNT >> 1)) ? 8 : 4;
arriveTime2[JOB_AMOUNT - i - 1] = j;
arriveTime3[i] = 3 * i;
}
/*
*以下开始模拟
*按长短作业不同比例
*用arriveTime1, burstTime1, burstTime2
*/
cout << "Jobs of different lengths of CPU burst time:" << endl;
//FCFS算法
cout << "Circumstance1:" << endl;
FCFS f_length1(arriveTime1, burstTime1);
f_length1.simulation();
cout << "Circumstance2:" << endl;
FCFS f_length2(arriveTime1, burstTime2);
f_length2.simulation();
cout << endl;
//SJF算法
cout << "Circumstance1:" << endl;
SJF s_length1(arriveTime1, burstTime1);
s_length1.simulation();
cout << "Circumstance2:" << endl;
SJF s_length2(arriveTime1, burstTime2);
s_length2.simulation();
cout << endl;
//RR算法
cout << "Circumstance1:" << endl;
RR r_length1(arriveTime1, burstTime1);
r_length1.simulation();
cout << "Circumstance2:" << endl;
RR r_length2(arriveTime1, burstTime2);
r_length2.simulation();
cout << endl;
//MLFS算法
cout << "Circumstance1:" << endl;
MLFS m_length1(arriveTime1, burstTime1);
m_length1.simulation();
cout << "Circumstance2:" << endl;
MLFS m_length2(arriveTime1, burstTime2);
m_length2.simulation();
cout << endl;
/*
*按作业不同达到顺序
*用arriveTime1, arriveTime2, burstTime1
*/
cout << "Jobs of different arriving sequence:" << endl;
//FCFS算法
cout << "Circumstance1:" << endl;
FCFS f_length3(arriveTime1, burstTime1);
f_length3.simulation();
cout << "Circumstance2:" << endl;
FCFS f_length4(arriveTime2, burstTime1);
f_length4.simulation();
cout << endl;
//SJF算法
cout << "Circumstance1:" << endl;
SJF s_length3(arriveTime1, burstTime1);
s_length3.simulation();
cout << "Circumstance2:" << endl;
SJF s_length4(arriveTime2, burstTime1);
s_length4.simulation();
cout << endl;
//RR算法
cout << "Circumstance1:" << endl;
RR r_length3(arriveTime1, burstTime1);
r_length3.simulation();
cout << "Circumstance2:" << endl;
RR r_length4(arriveTime2, burstTime1);
r_length4.simulation();
cout << endl;
//MLFS算法
cout << "Circumstance1:" << endl;
MLFS m_length3(arriveTime1, burstTime1);
m_length3.simulation();
cout << "Circumstance2:" << endl;
MLFS m_length4(arriveTime2, burstTime1);
m_length4.simulation();
cout << endl;
/*
*按作业不同达到频率
*用arriveTime1, arriveTime3, burstTime1
*/
cout << "Jobs of different arriving frequency:" << endl;
//FCFS算法
cout << "Circumstance1:" << endl;
FCFS f_length5(arriveTime1, burstTime1);
f_length5.simulation();
cout << "Circumstance2:" << endl;
FCFS f_length6(arriveTime3, burstTime1);
f_length6.simulation();
cout << endl;
//SJF算法
cout << "Circumstance1:" << endl;
SJF s_length5(arriveTime1, burstTime1);
s_length5.simulation();
cout << "Circumstance2:" << endl;
SJF s_length6(arriveTime3, burstTime1);
s_length6.simulation();
cout << endl;
//RR算法
cout << "Circumstance1:" << endl;
RR r_length5(arriveTime1, burstTime1);
r_length5.simulation();
cout << "Circumstance2:" << endl;
RR r_length6(arriveTime3, burstTime1);
r_length6.simulation();
cout << endl;
//MLFS算法
cout << "Circumstance1:" << endl;
MLFS m_length5(arriveTime1, burstTime1);
m_length5.simulation();
cout << "Circumstance2:" << endl;
MLFS m_length6(arriveTime3, burstTime1);
m_length6.simulation();
cout << endl;
return 0;
}
2012年04月14日 12点04分 14
level 13
=================完~=====================
2012年04月14日 12点04分 15
level 9
这么长啊。。。看起来很麻烦么 哪有问题? 你这个调度算法算啥? 这么老长。。。。我真想告诉你我什么都不懂。。。
2012年04月14日 12点04分 16
这里面有四个算法,最后一个就是测试文件,模拟作业不同长度、不同到达次序、不同到达频率下四种算法得到的任务周转时间。问题就是,MLFS算法里面如果把所有出现变量j的地方去掉,运行结果和FCFS是一样的,显然是错的;后来加上了j这个东西,跑到一半就死循环了。。我只是晒晒代码哈,星辰不用在意。
2012年04月14日 12点04分
level 9
我粗略看了下 好像确实没问题……待我研究一二。。。
2012年04月14日 12点04分 17
其实我只要一个goto语句就可以搞定了。。只是不想用goto。。
2012年04月14日 12点04分
@贴吧用户_057UtZe 我也没办法 你看着办呗。。。我哪能玩转那么多东西。。看着都累。。
2012年04月14日 12点04分
1