round robin

7 ビュー (過去 30 日間)
Ayda
Ayda 2012 年 4 月 7 日
Good Morning\evening i have to write a code for round robin(example 5 processes) i have number of processes and each process has its CPUtime(10,6,2,4 and 8 respectively)
i could not know why the CPUtime for process 4 and 5 do not go to zero
numOfJobs=input('Enter the number of jobs to run = ');
for i=1:numOfJobs
job(i)=i;
end
CPUtime=input('Enter the CPU time required by each job = ,[in vector form] ');
totalCPUtime= sum(CPUtime)
while (totalCPUtime ~= 0)
for i=1:numOfJobs
if (CPUtime(i)== 0)
break
else
CPUtime(i)=CPUtime(i)-1;
end
job(i)
end
totalCPUtime=totalCPUtime-1;
end

回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 4 月 7 日
TotalCPUtime needs to be recomputed as sum(CPUtime). Your "for i" loop can end up decrementing more than one CPUtime entry, so your total does not get decremented by exactly 1 under most circumstances.
  2 件のコメント
Ayda
Ayda 2012 年 4 月 8 日
why should the tolalCPUtime recomputed
Walter Roberson
Walter Roberson 2012 年 4 月 8 日
Your present code initializes totalCPUtime as the sum of the CPUtime . You then change what could be several of the CPUtime (all the non-zero ones), but your code only decreases totalCPUtime by 1 rather than by 1 per CPUtime that was decreased.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by