cpu.m
来自「8个进程的改进版CPU schedule 算法-FCFS」· M 代码 · 共 59 行
M
59 行
%--------------------------------------------------------------------------
clc;
clear;
%--------------------------------------------------------------------------
GlobalCycle=0;
EndTime=2000;
DiskTime=5;
Ts=1;
NProcess=3;
Qlen=NProcess;
TB=[200;10;10];
QReady=[1 0;
2 0;
3 0]; % 1 col is the process name;2 col is the remaining bust time
QReady(:,2)=TB
QReady_WaitTime=zeros(NProcess,1);
QReady_History=zeros(2*NProcess+3,EndTime);
QDisk=zeros(NProcess,2); % 1 col is the process name;2 col is the endtime
QDisk(:,2)=EndTime; % initial the endtime to the EndTime
%--------------------------------------------------------------------------
while GlobalCycle<EndTime
curP=0; %make sure not store the old state, 0 mean the cpu is idle
[QDisk,QReady]=CheckDiskQueue_FCFSorSJF(QDisk,QReady,Qlen,GlobalCycle,TB,EndTime); %......
if sum(QReady(:,1))==0
QReady_History(:,GlobalCycle)=0;
fprintf('CPU idle at T=%d....\n',GlobalCycle);
QReady_History(:,GlobalCycle+1)=-1;% Idle state
GlobalCycle=GlobalCycle+1;
continue;
end
%--------------------we know there are processes in the ReadQueue------------------
[curP,QReady]=FCFS(QReady,Qlen); %pick up a process
QReady_History(:,GlobalCycle+1)=[QReady(:,1);0;QDisk(:,1);0;curP];% the current state
% for i=GlobalCycle:GlobalCycle+Ts
% QReady_History(:,i+1)=QReady(:,1);% the current state
% end
GlobalCycle=GlobalCycle+TB(curP);
GlobalCycle=GlobalCycle+Ts;
value=GlobalCycle+DiskTime;
[QDisk,pos]=InQueue2(curP,value,QDisk,Qlen); %put into the DiskQueue
end
OutputResult(QReady_History,EndTime,NProcess);
Analysis(QReady_History,TB,1);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?