FCFS scheduling ( Improve this code ) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

FCFS scheduling ( Improve this code )

#include <stdio.h> int main() { int n; printf("Enter number of jobs to schedule - \n"); scanf("%d",&n); int burst[n],arrival[n],index[n]; int i,tot=0; printf("Enter the burst time of the jobs - \n"); for(i=0;i<n;i++) { index[i]=i+1; scanf("%d",&burst[i]); } printf("Enter the arrival time of the jobs - \n"); for(i=0;i<n;i++) { scanf("%d",&arrival[i]); } int j; for(i=0;i<n;i++) { int min=arrival[i]; int pos=i; for(j=i+1;j<n;j++) { if(arrival[j]<min) { min=arrival[j]; pos=j; } } int temp=arrival[i]; arrival[i]=min; arrival[pos]=temp; temp=burst[i]; burst[i]=burst[pos]; burst[pos]=temp; temp=index[i]; index[i]=index[pos]; index[pos]=temp; } int time=0; double avgtat=0; for(i=0;i<n;i++) { while(time<arrival[i]) time++; time+=burst[i]; avgtat+=(time-arrival[i]); } avgtat/=n; printf("The jobs will be processed in this order - "); for(i=0;i<n;i++) printf("%d\t",index[i]); printf("\nThe total time taken to complete all the jobs will be - %d",time); printf("\nAverage Turnaround Time is - %lf",avgtat); return 0; }

11th Jan 2022, 4:55 AM
MRIDUL CHANDRAWANSHI
4 Answers
+ 2
FCFS: First come first served In this problem we have to take arrival time(which can be in any order) along with respective burst time as input from user, then we have to sort the arrival time in ascending order and their respective burst time. After sorting we have to find completion time, turnaround time, waiting time(of each process). And at the last calculate average turnaround time and waiting time according to the FCFS CPU scheduling. If you can than try to print the process, arrival time, burst time, turnaround time and waiting time in a tabular format, and also the Gantt chart.
12th Jan 2022, 5:25 AM
MRIDUL CHANDRAWANSHI
+ 1
G'day MRIDUL CHANDRAWANSHI That is some nice & simple C code. Could you add some more comments? Also does FCFS mean "first come first served"? What is burst time? What format does arrival time need to be?? I would create a loop for entering each job, and get the burst and arrival during that loop. But I understand that you want to know how many jobs before creating your arrays. I see you used index[i] as a unique job_id, do you also need a job_name (or customer)? What about job_description[256] Could the output have the ordered jobs with all their details, maybe a new line between each? I think I'm gunna have a play with your idea and see what I can do!!! Thanks mate, & good luck with your coding.
12th Jan 2022, 5:17 AM
HungryTradie
HungryTradie - avatar
+ 1
Arrival time is the sequence of jobs that are coming to the CPU to get executed. So, the job with minimum arrival time will execute first and then other. I will recommend you to watch a FCFS schedule algorithm (Operation system) video on YouTube so that you can get better understanding.
12th Jan 2022, 5:40 AM
MRIDUL CHANDRAWANSHI
0
Oh, it's for CPU jobs, not customers... Woops. If arrival time is sequential, how do you know when the next job starts waiting? Eg job1 takes 15, job2 starts after job1, so no waiting time? Or do all the jobs come in at the same time, so job4 waiting time is job1 + job2 + job3 This is far beyond my knowledge & abilities at the moment. Good luck.
12th Jan 2022, 5:37 AM
HungryTradie
HungryTradie - avatar