#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QTimer"
#include <QFileDialog>
#include <QTextStream>
#include <typeinfo>
#include <iostream>
#include <QDebug>
#include <QMessageBox>
using namespace std;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init()
{
//下拉框
ui->comboBox->addItem("先来先服务");
ui->comboBox->addItem("时间片轮转");
ui->comboBox->addItem("短作业优先");
ui->comboBox->addItem("静态优先权优先调度");
ui->comboBox->addItem("高响应比调度");
//表格
ui->tableWidget->setColumnCount(7);
ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"进程号"<<"进入时间"<<"服务时间"<<"优先级"<<"响应时间"<<"结束时间"<<"状态");
ui->tableWidget->verticalHeader()->setVisible(false);
ui->label->setVisible(false);
ui->timeEdit->setVisible(false);
// 测试数据
// ui->tableWidget->setRowCount(2);
// QStringList pidList;
// pidList<<"124"<<"534"<<"267";
// QStringList enterTime;
// enterTime<<"0"<<"2"<<"3";
// QStringList serveTime;
// serveTime<<"3"<<"5"<<"1";
// QStringList priority;
// priority<<"3"<<"2"<<"1";
// for(int i=0;i<2;i++){
// int col = 0;
// ui->tableWidget->setItem(i,col++,new QTableWidgetItem(pidList[i]));
// ui->tableWidget->setItem(i,col++,new QTableWidgetItem(enterTime[i]));
// ui->tableWidget->setItem(i,col++,new QTableWidgetItem(serveTime[i]));
// ui->tableWidget->setItem(i,col++,new QTableWidgetItem(priority[i]));
// ui->tableWidget->setItem(i,6,new QTableWidgetItem("就绪"));
// }
// //让tableWidget内容中的每个元素居中
// for (int i=0;i<2;i++){
// for (int j=0;j<4;j++){
// ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
// }
// }
}
//初始化PCB
void MainWindow::initPcb()
{
int rows = ui->tableWidget->rowCount();
for(int i = 0;i<rows;i++)
{
pcbArr[i].pid = (ui->tableWidget->item(i,0)->text()).toInt();
pcbArr[i].arriveTime = (ui->tableWidget->item(i,1)->text()).toInt();
pcbArr[i].serviceTime = (ui->tableWidget->item(i,2)->text()).toInt();
pcbArr[i].priority = (ui->tableWidget->item(i,3)->text()).toInt();
memset(pcbArr[i].state,0,sizeof(pcbArr[i].state));
}
}
//清除PCB
void MainWindow::clearPcb(int rows)
{
for(int i = 0;i<rows;i++)
{
pcbArr[i].pid = -1;
pcbArr[i].arriveTime = -1;
pcbArr[i].serviceTime = -1;
pcbArr[i].priority = -1;
memset(pcbArr[i].state,0,sizeof(pcbArr[i].state));
}
}
//对到达时间从小到大排序
void MainWindow::sort()
{
int rows = ui->tableWidget->rowCount();
for(int i=0;i<rows;i++){
temp[i] = pcbArr[i];
temp[i].startTime = -1;
}
int i,j;
for(i = 1; i < rows; i++)
{
PCB t = temp[i];
for(j = i - 1; j >= 0 && t.arriveTime < temp[j].arriveTime; j--)
{
temp[j+1] = temp[j];
}
temp[j+1] = t;
}
}
//将pcb信息复制
void MainWindow::copyPcb()
{
int rows = ui->tableWidget->rowCount();
for(int a = 0;a<rows;a++)
{
for(int b = 0;b<rows;b++)
{
if(pcbArr[a].pid == temp[b].pid)
{
pcbArr[a].startTime = temp[b].startTime;
pcbArr[a].finishTime = temp[b].finishTime;
for(int i = 0;i<20;i++)
{
pcbArr[a].state[i] = temp[b].state[i];
}
a++;
}
}
}
}
//先来先服务
void MainWindow::fcfs()
{
sort();
int rows = ui->tableWidget->rowCount();
int sum = 0;
for(int k = 0;k<rows;k++)
{
if(k == 0)
{
temp[k].startTime = temp[k].arriveTime;
temp[k].finishTime = temp[k].startTime + temp[k].serviceTime;
temp[k].turnaroundTime = temp[k].finishTime - temp[k].arriveTime;
}
else
{
if(temp[k].arriveTime <= temp[k-1].finishTime)
{
temp[k].startTime = temp[k-1].finishTime;
}
else//第一个进程完成而第二个进程还未到达
{
temp[k].startTime = temp[k].arriveTime;
}
temp[k].finishTime = temp[k].startTime + temp[k].serviceTime;
temp[k].turnaroundTime = temp[k].finishTime - temp[k].arriveTime;
}
sum += temp[k].turnaroundTime;
}
fcfsAverageTurnaroundTime = sum / rows;
copyPcb();
}
//短作业优先
void MainWindow::sjf()
{
sort();
int rows = ui->tableWidget->rowCount();
int current = temp[0].arriveTime;//当前时间等于第一个进程的到达时间
int min_p,lastmin_p;
int flag[rows];//标记进程是否执行结束
int f = 0;//标记是否有上一次的下标
for(int i = 0;i<rows;i++)
{
flag[i] = 0;
}
while(1)
{
int compare[rows];//用于存放当前已到达但并未结束进程的剩余服务时间
for(int i = 0;i<rows;i++)
{
compare[i] = 0;
}
for(int i = 0;i<rows;i++)
{
if(temp[i].arriveTime <= current && flag[i] == 0)
{
compare[i] = temp[i].serviceTime;
}
}
int min = 100;
for(int i = 0;i<rows;i++)//找出当前进程最小剩余服务时间
{
if(compare[i] < min && compare[i] > 0)
{
min_p = i;//标记该进程
min = compare[i];
}
}
if(f == 1)
{
if((min_p != lastmin_p) && (temp[lastmin_p].state[current] != 3))//如果上一个进程被抢占
{
temp[lastmin_p].state[current] = 1;//就绪状态
}
}
if(temp[min_p].startTime == -1)
{
temp[min_p].startTime = current;
}
temp[min_p].serviceTime--;
temp[min_p].state[current] = 2;//执行状态
current++;
if(temp[min_p].serviceTime == 0)//判断当前进程是否结束
{
flag[min_p] = 1;
temp[min_p].finishTime = current;
temp[min_p].state[current] = 3;//结束状态
}
if(temp[min_p].serviceTime < 0)//判断所有进程是否结束
{
break;
}
lastmin_p = min_p;
f = 1;
}
int sum = 0;
for(int i = 0;i<rows;i++)
{
temp[i].turnaroundTime = temp[i].finishTime - temp[i].arriveTime;
sum += temp[i].turnaroundTime;
}
sjfAverageTurnaroundTime = sum / rows;
copyPcb();
}
//时间片轮转
void MainWindow::rr()
{
sort();
int time;
time = (ui->timeEdit->text()).toInt();
int rows = ui->tableWidget->rowCount();
int curNum;
int f;
int current = temp[0].arriveTime;//当前时间等于第一个进程的到达时间
int flag[rows];//标记进程是否执行结束
for(int j = 0;j<rows;j++)
{
flag[j] = 0;
}
while(1)
{
f = 0;
for(int i = 0; i<rows; i++)
{
if(temp[i].startTime == -1)
{
temp[i].startTime = current;
}
if(temp[i].serviceTime <= time && flag[i] == 0)//时间片内可以执行完毕
{
temp[i].state[current] = 2;//执行状态
current += temp[i].serviceTime;
temp[i].finishTime = current;
temp[i].state[current] = 3;//结束状态
flag[i] = 1;
}
curNum = 0;
if(temp[i].serviceTime > time && flag[i] == 0)//时间片内不能执行完毕
{ //cout<<i<<endl;
temp[i].state[cu
评论30