FCFS code

7 views (last 30 days)
Ayda
Ayda on 7 Apr 2012
Answered: Sanjana on 5 Mar 2024
Good Morning\ Evening
I have number of processes and each process has its arrival time the question require to simulate processes using First Come First Serve.
I wrote this code for FCFS but did not return a correct values the result should display the order of FCFS
numOfJobs=input('Enter the number of jobs to run = ');
for i=1:numOfJobs
job(i)=i;
end
job1=job;
arrivalTime=input('Enter the arrival time of each job = ,[in vector form] ');
for i=1:numOfJobs-1
y=i+1;
for j=y:numOfJobs
if(arrivalTime(j)<arrivalTime(i))
firstcome=job(j);
job(j)=job(i);
job(i)=firstcome;
end
end
end
job;
out2=[out1 arrivalTime']
Also I have to figure out the starting time, the completion time and total waiting Time for each process

Answers (2)

Walter Roberson
Walter Roberson on 7 Apr 2012
You can replace most of what you have written with
[sortedArrivalTimes, job] = sort(arrivalTime);
It is not possible to work out the starting and completion and waiting times without knowing how long each job takes. Other than that you can say that the starting time for the first job to arrive will be sortedArrivalTimes(1)
  2 Comments
Ayda
Ayda on 7 Apr 2012
thank you very much
but if I want to use the for loop,, where is the mistake
Walter Roberson
Walter Roberson on 7 Apr 2012
Using bubble sort is a mistake more often than not...

Sign in to comment.


Sanjana
Sanjana on 5 Mar 2024
numOfJobs=input('Enter the number of jobs to run = ');
for i=1:numOfJobs
job(i)=i;
end
job1=job;
arrivalTime=input('Enter the arrival time of each job = ,[in vector form] ');
for i=1:numOfJobs-1
y=i+1;
for j=y:numOfJobs
if(arrivalTime(j)<arrivalTime(i))
firstcome=job(j);
job(j)=job(i);
job(i)=firstcome;
end
end
end
job;
out2=[out1 arrivalTime']

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!