// 多重プログラミング機構での巡回スケジューリングシミュレーション #include "stdafx.h" #define NumJob 4 #define MaxTime 16 #define MaxExecTime 300 static int iRound=0, cpuProc[MaxExecTime], cpuCount[NumJob], tCount=0, Exe=-1; static int cpuBurst[][MaxTime]={ {1,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0}, {1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0}, {1,0,1,0,1,0,1,0,0,1,0,1,0,1,0,0}, {1,0,1,1,1,1,0,1,0,1,0,1,0,1,0,0}}; // cpuBurstには,CPUバースト時間のとき1, CPU遊休時間のとき0を入れる。 int notEnd(){ // シミュレーション終了の判定 for(int i=0;i=0;n--) for(int i=0;i0)return n; return 0; } void countRound(){ iRound++;if(iRound>=NumJob) iRound=0; } void clearCount(){ for(int i=0;i=0 && cpuBurst[i][n]<=0; n--); for(int j=0;j<=n;j++) printf(cpuBurst[i][j]!=0? "=":"…"); } } void dspTimeChart(){//タイムチャート表示 int n, mm; for(n=tCount-1; n>=0 && cpuProc[n]<=0; n--);//後ろの0を無視して結果の表示 printf("\n\n ** Time Chart\n     ");for(int j=1;j<=n+1;j++)printf("%2d",j%10); for(int i=0;i=0 && cpuProc[mm]!= i;mm--); for(int j=0;j<=n;j++) printf( (j>mm) ? ((j%10)==9? " +" : " "): ((cpuProc[j]==i)? "=":((j%10)==9? "・+" : "…"))); } } void dspTurnaround(){//ターンアラウンド計算表表示 int T=0, ms, mm; printf("\n\n Start and End Time\n"); for(int i=0;i=0) && cpuProc[mm]!=i; mm--);//プロセス終了 mm++; printf("\n Job %C : Start Time = %3d End time = %3d ",'A'+ i,ms,mm); } printf("\n\n Average of Turnaround Time = %8.4lf\n\n", (double)T / (double)NumJob); } int saveProcNo(int Exe){//実行プロセス番号の保存。保存できたときTrue,できなかったときfalseを返却 if(tCount>=MaxExecTime){ printf("\n *** Error : Area Over Flow");return false;} cpuProc[tCount++]=Exe; return true; } int _tmain(int argc, _TCHAR* argv[]){ //シミュレーション実行 clearCount(); for(iRound=0;notEnd();countRound(),countTime()) if(!saveProcNo(Exe= exeProcess())) break; dspBurst(); dspTimeChart(); dspTurnaround();//結果表示 getchar(); return 0; }