⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpu.m

📁 3个CPU调度算法的simulation. 分别是Short Job First,High Return Ration Next, Round Robin. 模拟得到R,U,S等重要指标。
💻 M
字号:
%--------------------------------------------------------------------------
clc;
clear;
%--------------------------------------------------------------------------
GlobalCycle=0;
EndTime=1000;                                                       %========>
DiskTime=11; %4 or 11

Ts=0;

NProcess=8;
Qlen=NProcess;
TB=[3;5;3;5;3;5;105;5];  % the no.8 process is actually No. 0

cpubP=7;                      %who is the CPU-bound process

QReady=[1 0;
        2 0;
        3 0;
        4 0;
        5 0;
        6 0;
        7 0;
        8 0]; %  1 col is the process name;2 col is the Tb
QReady(:,2)=TB;

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+100;       %  initial the endtime to the EndTime

%--------------------------------------------------------------------------
while GlobalCycle<EndTime
  curP=0;  %make sure not store the old state, 0 mean the cpu is idle  
%   disp('------before check and SJF at Time %d ---',GlobalCycle);
%   QDisk
%   QReady
  [QDisk,QReady]=CheckDiskQueue_FCFSorSJF(QDisk,QReady,Qlen,GlobalCycle,TB,EndTime); %......
  
  
  if sum(QReady(:,1))==0  % check before pick up 
      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------------------
%   disp('----before SJF');
  [curP,QReady]=SJF(QReady,Qlen);                                     %pick up a process=====>>
%   disp('----after');
   QReady_History(:,GlobalCycle+1)=[QReady(:,1);0;QDisk(:,1);0;curP];% the current state
  
    
   GlobalCycle=GlobalCycle+TB(curP);                                   %TB is GOOD for debug....
   GlobalCycle=GlobalCycle+Ts;
   
   value=GlobalCycle+DiskTime;
   [QDisk,pos]=InQueue2(curP,value,QDisk,Qlen,'D'); %put into the DiskQueue
   
end

OutputResult(QReady_History,EndTime,NProcess);
Analysis(QReady_History,TB,cpubP,Qlen);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -