-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
409 lines (367 loc) · 14.8 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_pScheAlgo = nullptr;
m_Timer = new QTimer;
InitWidget();
InitConnect();
}
MainWindow::~MainWindow()
{
delete m_pScheAlgo;
delete m_Timer;
delete ui;
}
void MainWindow::InitWidget()
{
QStringList header;
header << "进程名" << "需要运行时间(s)";
ui->tableWidget->setHorizontalHeaderLabels(header);
ui->tableWidget->setItem(0,0, new QTableWidgetItem("Job1"));
ui->tableWidget->setItem(0,1, new QTableWidgetItem(QString::number(10)));
ui->tableWidget->setItem(1,0, new QTableWidgetItem("Job2"));
ui->tableWidget->setItem(1,1, new QTableWidgetItem(QString::number(20)));
ui->tableWidget->setItem(2,0, new QTableWidgetItem("Job3"));
ui->tableWidget->setItem(2,1, new QTableWidgetItem(QString::number(30)));
ui->tableWidget->setRowCount(3);
}
void MainWindow::InitConnect()
{
connect(m_Timer, SIGNAL(timeout()), this, SLOT(Exec()));
}
void MainWindow::Update()
{
SCHE_STATE state = m_pScheAlgo->GetState();
if (state == SCHE_ANOTHER_JOB)
{
/* move run to finish */
int rows = ui->TABLE_FINISH->rowCount();
ui->TABLE_FINISH->setRowCount(rows + 1);
ui->TABLE_FINISH->setItem(rows,0, new QTableWidgetItem(ui->TABLE_RUN->item(0,0)->text()));
ui->TABLE_FINISH->setItem(rows,1, new QTableWidgetItem(ui->TABLE_RUN->item(0,1)->text()));
ui->TABLE_FINISH->setItem(rows,2, new QTableWidgetItem(ui->TABLE_RUN->item(0,2)->text()));
ui->TABLE_FINISH->setItem(rows,3, new QTableWidgetItem(QString::number(ui->TABLE_RUN->item(0,3)->text().toInt()+1)));
if (m_Status == STATUS_LEVEL || m_Status == STATUS_HRRN)
{
ui->TABLE_FINISH->setItem(rows,4, new QTableWidgetItem(ui->TABLE_RUN->item(0,4)->text()));
}
if (m_Status == STATUS_SJF || m_Status == STATUS_HRRN)
{
const std::list<PCB> *pf = m_pScheAlgo->GetFinishList();
ui->TABLE_FINISH->setItem(rows,5, new QTableWidgetItem(pf->front().finish_time.toString("hh:mm:ss")));
ui->TABLE_FINISH->setItem(rows,6, new QTableWidgetItem(QString::number(pf->front().arround_time,'f',6)));
ui->TABLE_FINISH->setItem(rows,7, new QTableWidgetItem(QString::number(pf->front().weight_arround_time,'f',6)));
}
/* renew run */
const std::list<PCB>* p = m_pScheAlgo->GetRunList();
ui->TABLE_RUN->item(0, 0)->setText(p->front().job_name);
ui->TABLE_RUN->item(0, 1)->setText(QString::number(p->front().need_time));
ui->TABLE_RUN->item(0, 2)->setText(p->front().arrive_time.toString("hh:mm:ss"));
ui->TABLE_RUN->item(0, 3)->setText(QString::number(p->front().exec_time));
if (m_Status == STATUS_LEVEL)
ui->TABLE_RUN->item(0, 4)->setText(QString::number(p->front().level));
if (m_Status == STATUS_HRRN)
ui->TABLE_RUN->item(0,4)->setText( QString::number(p->front().wait_time));
/* renew wait */
QString jobname(p->front().job_name);
rows = ui->TABLE_WAIT->rowCount();
for (int i = 0; i < rows; ++i)
{
if (jobname == ui->TABLE_WAIT->item(i,0)->text())
{
ui->TABLE_WAIT->removeRow(i);
break;
}
}
}
else if (state == SCHE_NEW_JOB)
{
const std::list<PCB>* p = m_pScheAlgo->GetRunList();
QString jobname(p->front().job_name);
int rows = ui->TABLE_RUN->rowCount();
ui->TABLE_RUN->setRowCount(rows + 1);
ui->TABLE_RUN->setItem(rows,0, new QTableWidgetItem(p->front().job_name));
ui->TABLE_RUN->setItem(rows,1, new QTableWidgetItem(QString::number(p->front().need_time)));
ui->TABLE_RUN->setItem(rows,2, new QTableWidgetItem(p->front().arrive_time.toString("hh:mm:ss")));
ui->TABLE_RUN->setItem(rows,3, new QTableWidgetItem(QString::number(p->front().exec_time)));
if (m_Status == STATUS_LEVEL)
ui->TABLE_RUN->setItem(rows,4, new QTableWidgetItem(QString::number(p->front().level)));
if (m_Status == STATUS_HRRN)
ui->TABLE_RUN->setItem(rows,4, new QTableWidgetItem(QString::number(p->front().wait_time)));
/*renew table wait*/
rows = ui->TABLE_WAIT->rowCount();
for (int i = 0; i < rows; ++i)
{
if (jobname == ui->TABLE_WAIT->item(i,0)->text())
{
ui->TABLE_WAIT->removeRow(i);
break;
}
}
}
else if (state == SCHE_RENEW_TIME)
{
ui->TABLE_RUN->item(0,3)->setText(QString::number(ui->TABLE_RUN->item(0,3)->text().toInt() + 1));
}
else if (state == SCHE_COMPLETE_JOB)
{
int rows = ui->TABLE_FINISH->rowCount();
ui->TABLE_FINISH->setRowCount(rows + 1);
ui->TABLE_FINISH->setItem(rows,0, new QTableWidgetItem(ui->TABLE_RUN->item(0,0)->text()));
ui->TABLE_FINISH->setItem(rows,1, new QTableWidgetItem(ui->TABLE_RUN->item(0,1)->text()));
ui->TABLE_FINISH->setItem(rows,2, new QTableWidgetItem(ui->TABLE_RUN->item(0,2)->text()));
ui->TABLE_FINISH->setItem(rows,3, new QTableWidgetItem(QString::number(ui->TABLE_RUN->item(0,3)->text().toInt()+1)));
if (m_Status == STATUS_LEVEL || m_Status == STATUS_HRRN)
{
ui->TABLE_FINISH->setItem(rows,4, new QTableWidgetItem(ui->TABLE_RUN->item(0,4)->text()));
}
if (m_Status == STATUS_SJF || m_Status == STATUS_HRRN)
{
const std::list<PCB> *pf = m_pScheAlgo->GetFinishList();
ui->TABLE_FINISH->setItem(rows,5, new QTableWidgetItem(pf->front().finish_time.toString("hh:mm:ss")));
ui->TABLE_FINISH->setItem(rows,6, new QTableWidgetItem(QString::number(pf->front().arround_time,'f',6)));
ui->TABLE_FINISH->setItem(rows,7, new QTableWidgetItem(QString::number(pf->front().weight_arround_time,'f',6)));
/* 计算并打印这组作业的平均周转时间、带权平均周转时间 */
auto iter = pf->begin();
size_t idx;
size_t size = pf->size();
double arround_time = 0;
double weight_arround_time = 0;
for (idx = 0; idx < size; ++idx)
{
arround_time += iter->arround_time;
weight_arround_time += iter->weight_arround_time;
++iter;
}
arround_time /= size;
weight_arround_time /= size;
ui->lineEdit_2->setText(QString::number(arround_time,'f',6));
ui->lineEdit_3->setText(QString::number(weight_arround_time,'f',6));
}
ui->TABLE_RUN->removeRow(0);
}
else if (state == SCHE_RRTIME_ARRI)
{
int rows = ui->TABLE_WAIT->rowCount();
ui->TABLE_WAIT->setRowCount(rows + 1);
ui->TABLE_WAIT->setItem(rows,0, new QTableWidgetItem(ui->TABLE_RUN->item(0,0)->text()));
ui->TABLE_WAIT->setItem(rows,1, new QTableWidgetItem(ui->TABLE_RUN->item(0,1)->text()));
ui->TABLE_WAIT->setItem(rows,2, new QTableWidgetItem(ui->TABLE_RUN->item(0,2)->text()));
ui->TABLE_WAIT->setItem(rows,3, new QTableWidgetItem(QString::number(ui->TABLE_RUN->item(0,3)->text().toInt()+1)));
if (m_Status == STATUS_LEVEL || m_Status == STATUS_HRRN)
{
ui->TABLE_WAIT->setItem(rows,4, new QTableWidgetItem(ui->TABLE_RUN->item(0,4)->text()));
}
ui->TABLE_RUN->item(0, 0)->setText(ui->TABLE_WAIT->item(0,0)->text());
ui->TABLE_RUN->item(0, 1)->setText(ui->TABLE_WAIT->item(0,1)->text());
ui->TABLE_RUN->item(0, 2)->setText(ui->TABLE_WAIT->item(0,2)->text());
ui->TABLE_RUN->item(0, 3)->setText(ui->TABLE_WAIT->item(0,3)->text());
if (m_Status == STATUS_LEVEL || m_Status == STATUS_HRRN)
{
ui->TABLE_RUN->item(0, 4)->setText(ui->TABLE_WAIT->item(0,4)->text());
}
ui->TABLE_WAIT->removeRow(0);
}
if (m_Status == STATUS_HRRN)
{
int rows = ui->TABLE_WAIT->rowCount();
for (int i = 0; i < rows; ++i)
{
ui->TABLE_WAIT->item(i,4)->setText(
QString::number(ui->TABLE_WAIT->item(i,4)->text().toInt() + 1));
}
}
}
void MainWindow::Exec()
{
m_pScheAlgo->Exec();
m_Timer->start(1000);
}
void MainWindow::on_BTN_START_clicked()
{
ui->BTN_START->setEnabled(false);
ui->BTN_ADD->setEnabled(true);
if (m_pScheAlgo)
{
delete m_pScheAlgo;
m_pScheAlgo = nullptr;
}
QStringList header;
header << "" << "" << "" << "" << "";
ui->TABLE_RUN->setHorizontalHeaderLabels(header);
ui->TABLE_WAIT->setHorizontalHeaderLabels(header);
header << "" << "" << "";
ui->TABLE_FINISH->setHorizontalHeaderLabels(header);
header.clear();
header << "进程名" << "需要运行时间(s)" << "到达时间" << "已运行时间(S)";
ui->TABLE_RUN->setHorizontalHeaderLabels(header);
ui->TABLE_WAIT->setHorizontalHeaderLabels(header);
ui->TABLE_FINISH->setHorizontalHeaderLabels(header);
if (ui->FCFS->isChecked())
{
m_pScheAlgo = new FCFS;
m_Status = STATUS_FCFS;
}
else if (ui->LEVEL->isChecked())
{
m_pScheAlgo = new LEVEL;
m_Status = STATUS_LEVEL;
QStringList header;
header << "" << "" << "" << "" << "";
ui->TABLE_RUN->setHorizontalHeaderLabels(header);
ui->TABLE_WAIT->setHorizontalHeaderLabels(header);
header << "" << "" << "";
ui->TABLE_FINISH->setHorizontalHeaderLabels(header);
header.clear();
header << "进程名" << "需要运行时间(s)" << "到达时间" << "已运行时间(S)" << "优先级";
ui->TABLE_RUN->setHorizontalHeaderLabels(header);
ui->TABLE_WAIT->setHorizontalHeaderLabels(header);
ui->TABLE_FINISH->setHorizontalHeaderLabels(header);
}
else if(ui->RR->isChecked())
{
m_pScheAlgo = new RR(ui->lineEdit->text().toInt());
m_Status = STATUS_RR;
}
else if(ui->SJF->isChecked())
{
m_pScheAlgo = new SJF;
m_Status = STATUS_SJF;
}
else if(ui->HRRN->isChecked())
{
m_pScheAlgo = new HRRN;
m_Status = STATUS_HRRN;
QStringList header;
header << "" << "" << "" << "" << "" ;
ui->TABLE_RUN->setHorizontalHeaderLabels(header);
ui->TABLE_WAIT->setHorizontalHeaderLabels(header);
header << "" << "" << "";
ui->TABLE_FINISH->setHorizontalHeaderLabels(header);
header.clear();
header << "进程名" << "需要运行时间(s)" << "到达时间" << "已运行时间(S)" << "已等待时间(S)";
ui->TABLE_RUN->setHorizontalHeaderLabels(header);
ui->TABLE_WAIT->setHorizontalHeaderLabels(header);
header << "完成时刻" << "周转时间(S)" << "带权周转时间(S)";
ui->TABLE_FINISH->setHorizontalHeaderLabels(header);
}
m_pScheAlgo->AddWidget(this);
m_Timer->start(1000);
}
void MainWindow::on_RR_clicked()
{
ui->lineEdit->setEnabled(true);
QStringList header;
header << "进程名" << "需要运行时间(s)" << "";
ui->tableWidget->setHorizontalHeaderLabels(header);
}
void MainWindow::on_FCFS_clicked()
{
ui->lineEdit->setEnabled(false);
QStringList header;
header << "进程名" << "需要运行时间(s)" << "";
ui->tableWidget->setHorizontalHeaderLabels(header);
}
void MainWindow::on_LEVEL_clicked()
{
ui->lineEdit->setEnabled(false);
QStringList header;
header << "进程名" << "需要运行时间(s)" << "优先数";
ui->tableWidget->setHorizontalHeaderLabels(header);
}
void MainWindow::on_SJF_clicked()
{
ui->lineEdit->setEnabled(false);
QStringList header;
header << "进程名" << "需要运行时间(s)" << "";
ui->tableWidget->setHorizontalHeaderLabels(header);
}
void MainWindow::on_HRRN_clicked()
{
ui->lineEdit->setEnabled(false);
QStringList header;
header << "进程名" << "需要运行时间(s)" << "";
ui->tableWidget->setHorizontalHeaderLabels(header);
}
void MainWindow::on_BTN_RESUME_clicked()
{/* back to default config */
m_Timer->stop();
ui->BTN_ADD->setEnabled(false);
ui->BTN_START->setEnabled(true);
ui->tableWidget->setItem(0,0, new QTableWidgetItem("Job1"));
ui->tableWidget->setItem(0,1, new QTableWidgetItem(QString::number(10)));
ui->tableWidget->setItem(1,0, new QTableWidgetItem("Job2"));
ui->tableWidget->setItem(1,1, new QTableWidgetItem(QString::number(20)));
ui->tableWidget->setItem(2,0, new QTableWidgetItem("Job3"));
ui->tableWidget->setItem(2,1, new QTableWidgetItem(QString::number(30)));
/* INitialize default jobs */
if (m_pScheAlgo)
{
m_pScheAlgo->ClearJobs();
delete m_pScheAlgo;
m_pScheAlgo = nullptr;
}
int rows = ui->TABLE_RUN->rowCount();
for (int i = 0; i < rows;++i)
{
ui->TABLE_RUN->removeRow(0);
}
rows = ui->TABLE_WAIT->rowCount();
for (int i = 0; i < rows;++i)
{
ui->TABLE_WAIT->removeRow(0);
}
rows = ui->TABLE_FINISH->rowCount();
for (int i = 0; i < rows;++i)
{
ui->TABLE_FINISH->removeRow(0);
}
}
void MainWindow::on_BTN_ADD_clicked()
{
int rows = ui->tableWidget->rowCount();
for (int i = 0; i < rows; ++i)
{
/* add to wait table */
int row = ui->TABLE_WAIT->rowCount();
ui->TABLE_WAIT->setRowCount(row + 1);
ui->TABLE_WAIT->setItem(row,0,new QTableWidgetItem(ui->tableWidget->item(i,0)->text()));
ui->TABLE_WAIT->setItem(row,1,new QTableWidgetItem(ui->tableWidget->item(i,1)->text()));
ui->TABLE_WAIT->setItem(row,2,new QTableWidgetItem(QDateTime::currentDateTime().toString("hh:mm:ss")));
ui->TABLE_WAIT->setItem(row,3,new QTableWidgetItem(QString::number(0)));
PCB pcb;
pcb.arrive_time = QDateTime::currentDateTime();
pcb.job_name = ui->tableWidget->item(i,0)->text();
pcb.need_time = ui->tableWidget->item(i,1)->text().toInt();
pcb.exec_time = 0;
pcb.wait_time = 0;
if (m_Status == STATUS_LEVEL)
{
pcb.level = ui->tableWidget->item(i,2)->text().toInt();
ui->TABLE_WAIT->setItem(row,4,new QTableWidgetItem(ui->tableWidget->item(i,2)->text()));
}
if (m_Status == STATUS_HRRN)
{
ui->TABLE_WAIT->setItem(row,4,new QTableWidgetItem(QString::number(pcb.wait_time)));
}
else
pcb.level = 32767;
m_pScheAlgo->AddNewJob(pcb);
}
}
void MainWindow::on_pushButton_clicked()
{
//qDebug() << ui->tableWidget->rowCount();
ui->tableWidget->setRowCount(ui->tableWidget->rowCount()+1);
//ui->tableWidget->removeRow(0);
}
void MainWindow::on_pushButton_2_clicked()
{
int rowIndex = ui->tableWidget->currentRow();
if (rowIndex != -1)
ui->tableWidget->removeRow(rowIndex);
}