#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cmath>
#include <ctime>
#include <QThread>
#include <QDebug>
#include <algorithm>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("进程调度");
srand((unsigned)time(0));
//初始化未分分区表
Partition* partition = new Partition;
partition->PID = "";
partition->front = nullptr;
partition->next = nullptr;
partition->size = MAXPARTITIONSIZE-20;
partition->state = 0;
partition->address = 20;//系统内存为20
Partition_Queue.append(partition);
print_Partition_Queue();
autothread = new AutoThread();
connect(this,SIGNAL(PCB_in_CPU_finished()),this,SLOT(deal_PCB_in_CPU_finished()));
connect(this,SIGNAL(PCB_state_changed_from_job_to_ready()),this,SLOT(deal_PCB_state_changed_from_job_to_ready()));
connect(autothread,SIGNAL(create_random_PCB_signal()),this,SLOT(deal_create_random_PCB_signal()));
connect(autothread,SIGNAL(ask_for_next_signal()),this,SLOT(on_Time_next_Button_clicked()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_Schdule_add_button_clicked()
{
//创建一个PCB
PCB* tmpPCB = new PCB;
//将输入的信息赋给新建的PCB
tmpPCB->PID = ui->SATF_name->toPlainText();
tmpPCB->time = atoi(ui->SATF_time->toPlainText().toStdString().c_str());//QString转int
tmpPCB->priority = atoi(ui->SATF_priority->toPlainText().toStdString().c_str());
tmpPCB->requiredMemory = atoi(ui->SATF_requiredAddress->toPlainText().toStdString().c_str());
tmpPCB->statement = "candidate";
//使上一个后备队列的PCB指向新建的PCB
if(PCB_Job_Queue.size()!=0)
{
PCB_Job_Queue.at(PCB_Job_Queue.size()-1)->next = tmpPCB;
}
//将新建的PCB加入队列
PCB_Job_Queue.append(tmpPCB);
emit PCB_state_changed_from_job_to_ready();
}
void MainWindow::print_PCB_Job_Queue(int k)
{
for(int i=0;i<PCB_Job_Queue.size();i++)
{
if(k==1) ui->QueueTF_job_name->insertPlainText(PCB_Job_Queue[i]->PID+'\n');
else if(k==2) ui->QueueTF_job_time->insertPlainText(QString::number(PCB_Job_Queue[i]->time)+'\n');
else ui->QueueTF_job_priority->insertPlainText(QString::number(PCB_Job_Queue[i]->priority)+'\n');
}
}
void MainWindow::print_PCB_Ready_Queue(int k){
for(int i=0;i<PCB_Ready_Queue.size();i++)
{
if(k==1) ui->QueueTF_ready_name->insertPlainText(PCB_Ready_Queue[i]->PID+'\n');
else if(k==2) ui->QueueTF_ready_time->insertPlainText(QString::number(PCB_Ready_Queue[i]->time)+'\n');
else if(k==3) ui->QueueTF_ready_priority->insertPlainText(QString::number(PCB_Ready_Queue[i]->priority)+'\n');
else ui->QueueTF_ready_size->insertPlainText(QString::number(PCB_Ready_Queue[i]->requiredMemory)+'\n');
}
}
void MainWindow::print_PCB_Pending_Queue(int k)
{
for(int i=0;i<PCB_pended_Queue.size();i++)
{
if(k==1) ui->QueueTF_pending_name->insertPlainText(PCB_pended_Queue[i]->PID+'\n');
else if(k==2) ui->QueueTF_pending_time->insertPlainText(QString::number(PCB_pended_Queue[i]->time)+'\n');
else ui->QueueTF_pending_priority->insertPlainText(QString::number(PCB_pended_Queue[i]->priority)+'\n');
}
}
void MainWindow::print_Partition_Queue()
{
ui->Partition_address->clear();
ui->Partition_size->clear();
ui->Partition_state->clear();
ui->Partition_id->clear();
for(int i=0;i<Partition_Queue.size();i++)
{
ui->Partition_address->insertPlainText(QString::number(Partition_Queue[i]->address)+'\n');
ui->Partition_size->insertPlainText(QString::number(Partition_Queue[i]->size)+'\n');
ui->Partition_state->insertPlainText(QString::number(Partition_Queue[i]->state)+'\n');
ui->Partition_id->insertPlainText(Partition_Queue[i]->PID+'\n');
}
}
void MainWindow::on_Schedule_clear_button_clicked()
{
ui->SATF_name->clear();
ui->SATF_time->clear();
ui->SATF_priority->clear();
ui->SATF_requiredAddress->clear();
}
void MainWindow::deal_PCB_in_CPU_finished()
{
emit PCB_state_changed_from_job_to_ready();
}
void MainWindow::on_Time_next_Button_clicked()
{
//每进行一个时间单元
emit PCB_state_changed_from_job_to_ready();
//判断目前的调度算法
if(Scheduling_algorithm == "优先权调度")
{
//获取就绪队列中优先级最大的PCB下标(若有重复则选择下标最小的,并储存在index中)
if(PCB_Ready_Queue.size()>0)
{
int index = 0;
int MaxPriority = -1;
if(PCB_Ready_Queue[0]->statement!="pended")
{
MaxPriority = PCB_Ready_Queue[0]->priority;
}
for(int i =1;i<PCB_Ready_Queue.size();i++)
{
if(PCB_Ready_Queue[i]->priority>MaxPriority&&PCB_Ready_Queue[i]->statement!="pended")
{
index = i;
MaxPriority = PCB_Ready_Queue[i]->priority;
}
}
//将就绪队列中下标为index的PCB送入CPU中处理
PCB_in_CPU = PCB_Ready_Queue.takeAt(index);
}
//CPU中PCB时间-1,优先权-1,如果剩余时间为0则进程完成,并发送信号,否则将其又送回就绪队列中
if(PCB_in_CPU!=nullptr)
{
PCB_in_CPU->time--;
if(PCB_in_CPU->priority>1)
{
PCB_in_CPU->priority--;
}
if(PCB_in_CPU->time<=0)
{
for(int i=0;i<Partition_Queue.size();i++)
{
if(Partition_Queue[i]->address==PCB_in_CPU->MemoryAddress)
{
Partition_Queue[i]->state = 0;
Partition_Queue[i]->PID = "";
//合并分区 注意内存始址越大的Partition的下标越小
if(i>0&&Partition_Queue[i-1]->state==0)
{
Partition_Queue[i]->size+=Partition_Queue[i-1]->size;
Partition_Queue.takeAt(i-1);
i-=1;
}
if(i<Partition_Queue.size()-1&&Partition_Queue[i+1]->state==0)
{
Partition_Queue[i+1]->size+=Partition_Queue[i]->size;
Partition_Queue.takeAt(i);
}
break;
}
}
emit PCB_in_CPU_finished();
// PCB_in_CPU = nullptr;
}
else
{
PCB_Ready_Queue.append(PCB_in_CPU);
}
}
}
else
{
PCB_in_CPU = PCB_Ready_Queue.takeAt(0);
//CPU中PCB时间-1,优先权-1,如果剩余时间为0则进程完成,并发送信号,否则将其又送回就绪队列中
if(PCB_in_CPU!=nullptr)
{
PCB_in_CPU->time--;
if(PCB_in_CPU->priority>1)
{
PCB_in_CPU->priority--;
}
if(PCB_in_CPU->time<=0)
{
for(int i=0;i<Partition_Queue.size();i++)
{
if(Partition_Queue[i]->address==PCB_in_CPU->MemoryAddress)
{
Partition_Queue[i]->state = 0;
Partition_Queue[i]->PID = "";
//合并分区 注意内存始址越大的Partition的下标越小
if(i>0&&Partition_Queue[i-1]->state==0)
- 1
- 2
前往页