round robin
6 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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 个评论
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!