round robin

4 views (last 30 days)
Ayda
Ayda on 7 Apr 2012
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

Answers (1)

Walter Roberson
Walter Roberson on 7 Apr 2012
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 Comments
Ayda
Ayda on 8 Apr 2012
why should the tolalCPUtime recomputed
Walter Roberson
Walter Roberson on 8 Apr 2012
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!