#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 ) {
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论































收起资源包目录





































































































共 92 条
- 1
资源评论

- KR10932014-01-14很多地方都不能用,错误比较多.总而言之,不是很好

lantygaga
- 粉丝: 0
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 印刷品订货合同.doc
- 版通用合同条款.doc
- 机房搭建方案基本原理.doc
- 基于EMTP的GIS中避雷器防雷特性分析.docx
- 云南某风电场工程监理周报第二十期.doc
- 工程细目措施费变更探讨.doc
- 电力工程项目管理的三要素分析.docx
- 企业预算信息化软件系统发展趋势研究.docx
- SVN使用指南——Windows环境版.ppt
- 某800tlm顶管施工方案.doc
- 嵌板缝工程分包合同书.doc
- 公路后侧地质灾害勘察设计招标文件.doc
- 大数据时代高校档案信息安全性建设探讨.docx
- 中职计算机教学中微课的应用探究.docx
- 浅析大数据时代高校教务管理.docx
- 工程量计算的基本方法教学总结.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
