#include"mainwindow.h"
#define TIME_RISE 1
/*函数*/
void OS::init_system(MainWindow& mw)/*初始化整个系统*/
{
/*创建系统第一个进程.idle process*/
PCB idle_task;
idle_task.pid = 0;
idle_task.need_time = 5;
idle_task.priority = 2;
idle_task.current_priority = -1;
idle_task.wait_time = 0;
idle_task.exce_time = -1;
idle_task.state = TASK_RUNNING;
idle_task.time = get_cur_time();
idle_task.task.length = 42;
idle_task.task.pslice = 3;
idle_task.task.device = 0;
idle_task.task.name = "idle";
/*记录初始进程的信息*/
in_well(&idle_task,mw);
/* 初始化进程队列 */
exce_task.clear();
exce_task.push_back(idle_task);
ready_task.clear();
blocked_task.clear();
current_task = &(*exce_task.begin());//current task pointer
wake_task = NULL;
out_task = NULL;
cur_blocked_task = NULL;
/* 初始化设备 */
for( int i=0;i<NUM_DEVICE;++i)
device[i] = DEVICE_OFF;
/* 初始化其他资源信息,*/
completed = 0;
slice = PSLICE_TIME;
max_task = MAX_RUNNING;
max_ready_task = MAX_READY;
// cur_run_task = 1;
for(int i=0;i<10001;++i)
pid_pool[i] = 0;
pid_pool[0]= 1;
/*初始化Job*/
JOB tmp;
tmp.device = 0;
tmp.length = 6;
tmp.name = "first";
tmp.priority = 2;
job_task.push_back(tmp);
tmp.device = 0;
tmp.length = 13;
tmp.name = "second";
tmp.priority = 3;
job_task.push_back(tmp);
tmp.device = 1;
tmp.length = 7;
tmp.priority = 5;
tmp.name = "third";
job_task.push_back(tmp);
tmp.device = 1;
tmp.length = 8;
tmp.priority = 4;
tmp.name = "forth";
job_task.push_back(tmp);
tmp.device = 1;
tmp.length = 9;
tmp.priority = 4;
tmp.name = "five";
job_task.push_back(tmp);
tmp.device = 0;
tmp.length = 7;
tmp.priority = 2;
tmp.name = "six";
job_task.push_back(tmp);
tmp.device = 0;
tmp.length = 9;
tmp.priority = 1;
tmp.name = "seven";
job_task.push_back(tmp);
tmp.device = 2;
tmp.length = 5;
tmp.priority = 8;
tmp.name = "eight";
job_task.push_back(tmp);
tmp.device = 0;
tmp.length = 12;
tmp.priority = 9;
tmp.name = "nine";
job_task.push_back(tmp);
tmp.device = 0;
tmp.length = 8;
tmp.priority = 5;
tmp.name = "ten";
job_task.push_back(tmp);
}
/*关闭系统,但并不退出*/
void OS::halt_system(MainWindow& mw)
{
ready_task.clear();
exce_task.clear();
blocked_task.clear();
}
/*时钟中断and更新pcb信息*/
void OS::update(MainWindow& mw){
list<PCB> ::iterator temp_task_iterator;//指向运行态进程的迭代器;
temp_task_iterator=exce_task.begin();
for(;temp_task_iterator!=exce_task.end();){
if(temp_task_iterator->pid==0)
{
++temp_task_iterator;
continue;
}
if((*temp_task_iterator).pid!=current_task->pid){
temp_task_iterator->wait_time+=TIME_RISE;
++temp_task_iterator;
}
else{
/*如果进程结束,输出井and空出设备and撤销进程*/
if((temp_task_iterator->exce_time +TIME_RISE)>=temp_task_iterator->need_time){
out_task=&(*temp_task_iterator);
out_well(out_task,mw);
if(temp_task_iterator->task.device>=1)
device[temp_task_iterator->task.device-1]=DEVICE_OFF;
exce_task.erase(temp_task_iterator);
// temp_task_iterator = exce_task.begin();
break;
}
else{
temp_task_iterator->exce_time+=TIME_RISE;
/*更新时间片数据*/
/*如果用完了,等待下次调度时充值*/
if(temp_task_iterator->task.pslice-TIME_RISE<0){
temp_task_iterator->task.pslice=0;
}
else{
temp_task_iterator->task.pslice-=TIME_RISE;
}
++temp_task_iterator;
}
}
}
temp_task_iterator=exce_task.begin();
/*更新各进程信息*/
/*公式:当前优先级=(等待时间+需要运行时间)/需要运行时间*基本优先级数*/
//cur_run_task=0;
for(;temp_task_iterator!=exce_task.end();++temp_task_iterator){
//cur_run_task++;
temp_task_iterator->current_priority
=(float)(temp_task_iterator->wait_time+temp_task_iterator->need_time)/temp_task_iterator->need_time*temp_task_iterator->priority;
}
temp_task_iterator=ready_task.begin();
for(;temp_task_iterator!=ready_task.end();++temp_task_iterator){
temp_task_iterator->wait_time+=TIME_RISE;
temp_task_iterator->current_priority
=(float)(temp_task_iterator->wait_time+temp_task_iterator->need_time)/temp_task_iterator->need_time*temp_task_iterator->priority;
}
temp_task_iterator=blocked_task.begin();
for(;temp_task_iterator!=blocked_task.end();++temp_task_iterator){
temp_task_iterator->wait_time+=TIME_RISE;
temp_task_iterator->current_priority
=(float)(temp_task_iterator->wait_time+temp_task_iterator->need_time)/temp_task_iterator->need_time*temp_task_iterator->priority;
}
}
/*检查是否有紧急进程就绪,有则加入运行,或选择某运行态进程阻塞*/
/*调度算法,维护执行进程调度*/
void OS::schedule(MainWindow& mw){
list<PCB>::iterator temp_task_iterator;
//cur_run_task = int(exce_task.size());
/*检查是否有结束进程*/
if(exce_task.size()<MAX_RUNNING){
temp_task_iterator=blocked_task.begin();
/*检查是否唤醒进程*/
for(;temp_task_iterator!=blocked_task.end();){
if(device[temp_task_iterator->task.device-1]==DEVICE_OFF){
wake_task=&(*temp_task_iterator);
wake_up();
temp_task_iterator = blocked_task.begin();
}
else{
++temp_task_iterator;
}
}
/*调度就绪进程进入运行进程队列*/
while(exce_task.size()<MAX_RUNNING&&ready_task.size()!=0){
temp_task_iterator=ready_task.begin();
list<PCB>::iterator ready_task_iterator=temp_task_iterator;
for(;temp_task_iterator!=ready_task.end();++temp_task_iterator){
if(temp_task_iterator->current_priority < ready_task_iterator->current_priority)
continue;
ready_task_iterator=temp_task_iterator;
}/*检查是否需要设备and设备是否可用*/
if(ready_task_iterator->task.device==0||device[ready_task_iterator->task.device-1]==DEVICE_OFF){
if(ready_task_iterator->task.device>0)
device[ready_task_iterator->task.device-1]=DEVICE_ON;