# 实验一 进程调度算法

用代码模拟进程状态的转变、进程调度的策略

(1)    输入的形式和输入值的范围；

已在程序中预置好进程数据，只需输入数字1/2选择算法

(2)    输出的形式；

根据时刻输出每个时刻的调度过程

(3)    程序所能达到的功能；

模拟SJF,FCFS调度

(4)    测试数据，包括正确的输入及其输出结果和含有错误的输入及其输出结果。

正确输入：1 或者  2

错误输入：不等于1 、2的字符

Process

ArrivalTime

ServiceTime

FinishTime

WholeTime

WeightWholeTime

void FCFS(process(&processList)[MAXNUM])

void SJF(process(&processList)[MAXNUM])

(1)    调试过程中遇到的问题以及解决方法，设计与实现的回顾讨论和分析；

附录

<pre name="code" class="cpp">#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
//进程等待了
//进程执行了
// 空闲了

static const int MAXNUM = 5;

class process
{
public:

string ID;
double ArrivalTime, ServiceTime, FinishTime, WholeTime, WeightWholeTime;
process(double arrivalTime, double serviceTime, string id);
~process();
//static double Get_AverageWT_FCFS(process(&processList)[MAXNUM]);
private:
};

process::process(double arrivalTime, double serviceTime, string id)
{
ID = id;
ArrivalTime = arrivalTime;
ServiceTime = serviceTime;
}

process::~process()
{
}

double Get_AverageWT(process(&processList)[MAXNUM])
{
double sum = 0;
for (int i = 0; i < MAXNUM; i++)
{
sum += processList[i].WholeTime;
}
return sum / MAXNUM;
}

double Get_AverageWWT(process(&processList)[MAXNUM])
{
double sum = 0;
for (int i = 0; i < MAXNUM; i++)
{
sum += processList[i].WeightWholeTime;
}
return sum / MAXNUM;
}

void FCFS(process(&processList)[MAXNUM])//先来后到
{
for (int i = 0; i<MAXNUM; i++)
{
for (int j = i; j<MAXNUM; j++)
{
if (processList[i].ArrivalTime>processList[j].ArrivalTime)
{
process temp = processList[i];
processList[i] = processList[j];
processList[j] = temp;
}
}

}
}

void SJF(process(&processList)[MAXNUM])//小的优先
{

for (int k = 1; k<MAXNUM; k++)//把最先到达的提前
{
if (processList[0].ArrivalTime>processList[k].ArrivalTime)
{
process temp = processList[0];
processList[0] = processList[k];
processList[k] = temp;
}
}

for (int i = 1; i<MAXNUM; i++)//余下的 短时间优先
{
for (int j = i; j<MAXNUM; j++)
{
if (processList[i].ServiceTime>processList[j].ServiceTime)
{
process temp = processList[i];
processList[i] = processList[j];
processList[j] = temp;
}
}
}

}

int _tmain(double argc, _TCHAR* argv[])
{
cout << "-------------------------------进程调度算法:" << endl;
int time = 0;//计时器
double q = 1;//时间片
process processList[MAXNUM] = { process(0, 1, "A"), process(1, 3, "B"), process(2, 4, "C"), process(3, 2, "D"), process(4, 4, "E") };
cout << "--------进程列表如下:" << endl;
for (int i = 0; i < MAXNUM; i++)
{
cout << "进程ID:" << processList[i].ID << " 到达时间:" << processList[i].ArrivalTime << " 服务时间:" << processList[i].ServiceTime << endl;
}

int flag = 1;// 1 SJF  2FCFS
cout << "请选择进程处理方式: 1-SJF 2-FCFS" << endl;
cin >> flag;
while (!(flag == 1 || flag == 2))
{
cout << "输入错误，请重新输入！" << endl;
cin >> flag;
}
if (flag == 1)
{
SJF(processList);
}
else
{
FCFS(processList);
}

int i = 0;//进程索引
while (i<MAXNUM)
{

if (processList[i].ArrivalTime == time)//无等待 处理进程
{
cout << "当前时刻:" << time << " [开始]进程" << processList[i].ID << endl;
processList[i].FinishTime = time + processList[i].ServiceTime;
time = processList[i].FinishTime;
processList[i].WholeTime = processList[i].FinishTime - processList[i].ArrivalTime;
processList[i].WeightWholeTime = processList[i].WholeTime / processList[i].ServiceTime;
cout << "          " << " [结束]进程" << processList[i].ID << " 耗时" << processList[i].ServiceTime << "秒" << " 周转时间:" << processList[i].WholeTime << " 带权周转时间:" << processList[i].WeightWholeTime << endl;

i++;//处理完后移到下一个任务
}

else if (time>processList[i].ArrivalTime)//执行等待的进程
{
cout << "当前时刻:" << time << " [开始]进程" << processList[i].ID << " 排队等待" << time - processList[i].ArrivalTime << "秒" << endl;
processList[i].FinishTime = time + processList[i].ServiceTime;
time = processList[i].FinishTime;
processList[i].WholeTime = processList[i].FinishTime - processList[i].ArrivalTime;
processList[i].WeightWholeTime = processList[i].WholeTime / processList[i].ServiceTime;
cout << "          " << " [结束]进程" << processList[i].ID << " 耗时" << processList[i].ServiceTime << "秒" << " 周转时间:" << processList[i].WholeTime << " 带权周转时间:" << processList[i].WeightWholeTime << endl;
i++;//处理完后移到下一个任务
}

else
{
cout << "当前时刻:" << time << " [空闲]" << endl;
time += q;;
}

}

if (flag == 1)
{
cout << "当前时刻:" << time << " 【SJF】 已处理所有进程！" << " 平均周转时间:" << Get_AverageWT(processList) << " 带权平均周转时间:" << Get_AverageWWT(processList) << endl;

}
else
{
cout << "当前时刻:" << time << " 【FCFS】 已处理所有进程！" << " 平均周转时间:" << Get_AverageWT(processList) << " 带权平均周转时间:" << Get_AverageWWT(processList) << endl;
}
return 0;
}



• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120