#include "m_sched.h"
#include "Compare.h"
int m_sched::cur_run = -1;
m_sched::m_sched ( void )
{
}
void m_sched::run ( strategy algo ) {
event_style event = NOP;
list< PCBs* >::iterator proc;
list< PCBs* >::iterator wproc;
int run_t = 0;
switch ( algo ) {
case FCFS:
printf ( "Process\t\tArri_Time\tBurst_Time\tIO_Gap\tIO_Burst_Time\n" );
for ( list< PCBs* >::iterator itr = m_queue.begin(); itr != m_queue.end(); ++itr ) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", (*itr)->pid_t, (*itr)->arri,
(*itr)->burst, (*itr)->io_gap, (*itr)->io_burst );
}
while ( !( m_queue.empty() && w_queue.empty() ) ) {
if ( !w_queue.empty() ) {
for ( list< PCBs* >::iterator itr = w_queue.begin();
itr != w_queue.end(); ++itr ) {
if ( cur_run >= ( ( *itr )->arri + ( *itr )->io_burst ) ) {
( *itr )->io = false;
( *itr )->arri = ( ( *itr )->arri + ( *itr )->io_burst );
printf ( "\nPid %d finish IO operation at CPU time unit %d.\n", ( *itr )->pid_t, ( *itr )->arri );
m_queue.push_back ( *itr );//shift to ready queue
m_queue.sort ( Compare() );//sort by arrive time
w_queue.erase ( itr );//remove from waiting queue
break;
}
}
}
if ( m_queue.empty() ){
++ cur_run;
continue;
}
proc = m_queue.begin();//get the first one of ready queue
while ( cur_run < ( *proc )->arri ) {//waiting the process arrive
Sleep ( 1000 );
++ cur_run;
}
printf ( "\nEvent Process_Start at CPU time unit %d.\n", cur_run );
printf ( "Pid %d execute...", ( *proc )->pid_t );
stat.add_wait ( cur_run - ( ( *proc )->arri ) );//add up the waiting time
while ( run_t < ( *proc )->burst ) {//execute process
Sleep ( 1000 );
printf( "..." );
++ run_t;
++ cur_run;
if ( ( *proc )->io ) {//do io operation
if ( run_t == ( *proc )->io_gap ) {
printf ( "\nPid %d request for IO operation at CPU time unit %d.\n", ( *proc )->pid_t, cur_run );
( *proc )->arri = cur_run;//modify process arrival time
( *proc )->burst -= run_t;//remain burst time
( *proc )->sta = waiting;//ready to waiting
w_queue.push_back( *proc );//shift to waiting queue
printf ( "Have been run %d\n", run_t );
break;
}
}
}
if ( !( *proc )->io ) {
printf ( "\nRunning time is %d\n", run_t );
stat.add_turn ( ( *proc )->pid_t, cur_run );
}
m_queue.pop_front();//pop the the first one of ready queue
run_t = 0;//reset
}
stat.ave_wait ( MAX_PRO );
stat.ave_turn ( MAX_PRO );
break;
case RR:
printf ( "TimeSlice is %d\n", TimeSlice );
printf ( "Process\t\tArri_Time\tBurst_Time\tIO_Gap\tIO_Burst_Time\n" );
for ( list< PCBs* >::iterator itr = m_queue.begin(); itr != m_queue.end(); ++itr ) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", (*itr)->pid_t, (*itr)->arri,
(*itr)->burst, (*itr)->io_gap, (*itr)->io_burst );
}
while ( !( m_queue.empty() && w_queue.empty() ) ) {
if ( !w_queue.empty() ) {
for ( list< PCBs* >::iterator itr = w_queue.begin();
itr != w_queue.end(); ++itr ) {
if ( cur_run >= ( ( *itr )->arri + ( *itr )->io_burst ) ) {
( *itr )->io = false;
( *itr )->arri = ( ( *itr )->arri + ( *itr )->io_burst );//back to ready queue time
printf ( "\nPid %d finish IO operation at CPU time unit %d\n", ( *itr )->pid_t, ( *itr )->arri );
m_queue.push_back ( *itr );//shift to ready queue
m_queue.sort();
w_queue.erase ( itr );//remove from waiting queue
break;
}
}
}
if ( m_queue.empty() ){
++ cur_run;
continue;
}
proc = m_queue.begin();
while ( cur_run < ( *proc )->arri ) {//waiting the process arrive
Sleep ( 1000 );
++ cur_run;
}
printf ( "\nEvent Process_Start at CPU time unit %d.\n", cur_run );
printf ( "Pid %d execute...", ( *proc )->pid_t );
stat.add_wait ( cur_run - ( ( *proc )->arri ) );//add up the waiting time
while ( run_t < ( *proc )->burst && run_t < TimeSlice ) {//execute process
Sleep ( 1000 );
printf( "..." );
++ run_t;
++ cur_run;
if ( ( *proc )->io ) {//do io operation
if ( run_t == ( *proc )->io_gap ) {
printf ( "\nPid %d request for IO operation at CPU time unit %d.\n",( *proc )->pid_t , cur_run );
( *proc )->arri = cur_run;//modify process arrival time
( *proc )->burst -= run_t;//remain burst time
( *proc )->sta = waiting;//ready to waiting
w_queue.push_back( *proc );//shift to waiting queue
printf ( "Have been run %d\n", run_t );
break;
}
}
}
if ( run_t == TimeSlice && ( *proc )->burst != TimeSlice ) {
printf ( "\nSwap out Pid %d\n", ( *proc )->pid_t );
( *proc )->arri = cur_run;//modify process arrival time
( *proc )->sta = waiting;//ready to waiting
m_queue.push_back( *proc );//shift to the end of queue
}
if ( !( *proc )->io ) ( *proc )->burst -= run_t;//remain burst time
if ( !( *proc )->burst ) {
printf ( "\nRunning time is %d\n", run_t );
stat.add_turn ( ( *proc )->pid_t, cur_run );
}
m_queue.pop_front();//pop from ready queue
run_t = 0;//reset
}
stat.ave_wait ( MAX_PRO );
stat.ave_turn ( MAX_PRO );
break;
}
}
m_sched::~m_sched ( void ) {
}
![avatar](https://profile-avatar.csdnimg.cn/0dd6adc7bfef43659fd8d0a0c32d1eae_lantygaga.jpg!1)
lantygaga
- 粉丝: 0
- 资源: 6
最新资源
- 锂电池建模与热管理仿真系统:精细化模拟电池串联并联连接、散热与负载分析,锂电池建模与热管理仿真系统:精细化模拟电池系统性能与热行为,锂电池建模与热管理仿真 主要贡献: 1、 对并联或串联连接的任意所需
- 燃料电池汽车参数匹配与能量管理策略优化-涵盖动力源功率选型、电机与蓄电池匹配及仿真模型构建,燃料电池汽车参数匹配与能量管理策略优化-涵盖动力源功率选型、电机与蓄电池匹配及仿真模型构建,燃料电池汽车
- vue-vben-admin-Typescript资源
- goploy-Go资源
- 基于S-S拓扑结构的无线电能传输仿真模型:电路设计与移相控制方法详解,闭环输出电压400v展现优秀效果,无线电能传输仿真模型:基于S-S拓扑结构电路、闭环输出电压与移相控制参数设计过程研究,无线电能传
- Cyss.Net-C#资源
- webman-PHP资源
- COMSOL有限元仿真模型:超声相控阵聚焦功能详解,频域参数任意调整,COMSOL有限元仿真模型:超声相控阵聚焦模拟,参数灵活调整频域研究,COMSOL有限元仿真模型-超声相控阵聚焦仿真,参数可任意改
- excelize-wasm-JavaScript资源
- 毕业设计-毕业设计资源
- 台达DVP 16ES2 PLC与三台DT3温控器通讯程序(TDES-3):基于Modbus协议的温控系统设计与实现,台达PLC与DT3温控器通信程序设计,Modbus协议实现温控自动化管控,台达DVP
- lanqiao-蓝桥杯资源
- 磁通切换电机模型:12槽10极全参数化模型与磁场调制原理的探索与实践,基于Maxwell 2021r1的应用及扩展其他槽极配合电机设计,磁通切换电机模型:12槽10极全参数化模型及其在Maxwell
- MATLAB-6轴机械臂仿真-matlab仿真资源
- Comsol激光抛光技术:全面适用平顶、连续与高斯激光,公式有据可循,激光抛光技术:多种激光类型(包括Comsol激光)的抛光方法及公式文献参考研究,comsol 激光抛光, 平顶激光,连续激光,高斯
- oops-framework-cocos资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)