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

📄 cpu.m

📁 3个CPU调度算法的simulation. 分别是Short Job First,High Return Ration Next, Round Robin. 模拟得到R,U,S等重要指标。
💻 M
字号:
%--------------------------------------------------------------------------
clc;
clear;
%--------------------------------------------------------------------------
TQ=3; % 3 or 5 or 7                                                              %========>
GlobalCycle=0;               %initial the system 0 time;This is the current emulating time!nothing to do with cycle!
EndTime=5000;                %emulation end time
DiskTime=4; %4 or 11
Ts=0;

NProcess=8;
Qlen=NProcess;
TB=[3;5;3;5;3;5;105;5];

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

QReady=[1 0 1;               %1 col is the process name; 
        2 0 1;               %2 col is the Remaining Bust time?
        3 0 1;
        4 0 1;
        5 0 1;
        6 0 1;
        7 0 1;
        8 0 1];              %3 is the flag for new beginning of a process(flag=1)====>
QReady(:,2)=TB;

QReady_History=zeros(2*NProcess+4,EndTime); %16 col is for the newstartFlag
                                            %17 col is for the current process(running) 
                                            %18 col is for the current how much to be used in THIS Quatum.
                                            
QDisk=zeros(NProcess,4);  % 1 col is the process name;2 col is the endtime; 
                          % 3 col is for remaining time 4 for new beginningflag=>
QDisk(:,2)=EndTime+100;   % initial the endtime to the EndTime(Very Important and Useful!)
%--------------------------------------------------------------------------
while GlobalCycle<EndTime
  %fprintf('------before check and RR at Time=%d---------\n',GlobalCycle);
  %   QDisk
  %   QReady
  
  newStartFlag=0;
  curP=0;  %make sure not store the old state, 0 mean the cpu is idle or empty readyqueue  

  [QDisk,QReady]=CheckDiskQueue_RR(QDisk,QReady,Qlen,GlobalCycle,TB,EndTime); %if p finished its disktime, move to the readyQ
  
  %--------------------if CPU Idle, Queue empty-----------------------------
  if sum(QReady(:,1))==0                              % check before pick up,to make sure the readyQ is not empty when applying the RR 
      fprintf('CPU idle at T=%d....\n',GlobalCycle);
      
      QReady_History(:,GlobalCycle+1)=-1;             % Idle state,-1 is special,you can use it to trace.
      GlobalCycle=GlobalCycle+1;                      % time forward
      continue;
  end
  
  %--------------------Now,we know there are processes in the ReadQueue------------------
  %disp('----before RR');
  
  [curP,QReady,rmTime,curstartFlag]=RR(QReady,Qlen);       %pick up a process,it become the curP.=====>>
 
  % disp('----after RR');
    
  %---------------------At the begin of the curP running on CPU,just after pickup, We want to record the Queues states!
  if  rmTime>TQ
      useTime=TQ;
  else
      useTime=rmTime;
  end
  QReady_History(:,GlobalCycle+1)=[QReady(:,1);0;QDisk(:,1);curstartFlag;curP;useTime];% the current state, 16 col say whethere curP is a newstart p
 
  %---------------------Time move forward-----------------------------
   if  rmTime>TQ
       GlobalCycle=GlobalCycle+TQ;      %
       rmTime=rmTime-TQ;
       newStartFlag=0;
   else
       GlobalCycle=GlobalCycle+rmTime;  %
       rmTime=TB(curP);                 % prepare for the next run 
       newStartFlag=1;                   %end and it will start again from the beginning!!
   end
   
   GlobalCycle=GlobalCycle+Ts;
   
   %fprintf('p%d will start again(%d).....\n',curP,newStartFlag);


%    %---------------------Serve on the(move to )DiskQueue------------------------
%    ServeEnd=GlobalCycle+DiskTime;                                               %when the p should move back to ReadyQ
%    [QDisk,pos]=InQueue4Disk(curP,ServeEnd,rmTime,newStartFlag,QDisk,Qlen,'D');  %put into the DiskQueue
%       
%----------------------If only after the TB, the p go to disk, use the followings   
   %move to DiskQueue
   if newStartFlag==1
      ServeEnd=GlobalCycle+DiskTime;
      [QDisk,pos]=InQueue4Disk(curP,ServeEnd,rmTime,newStartFlag,QDisk,Qlen,'D'); %put into the DiskQueue
   else
      %+++++++++++++++++++++++++++++++++When you want to update the ReadyQ,must check() as well++++++++++++++ 
      [QDisk,QReady]=CheckDiskQueue_RR(QDisk,QReady,Qlen,GlobalCycle,TB,EndTime); %if p finished its disktime, move to the readyQ
      %move into ReadyQueue Immediately
      [QReady,pos]=InQueue3QReady(curP,rmTime,newStartFlag,QReady,Qlen,'R');  
   end    
end

%----------------------Present the result--------------------------------------------------
OutputResult(QReady_History,EndTime,NProcess);
Analysis_RR(QReady_History,TB,cpubP,TQ,Qlen);

⌨️ 快捷键说明

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