FCFS2
FCFS2
h>
#define MAX 20
int main() {
int arrival_time[MAX];
int service_time[MAX];
int start_time[MAX];
int finish_time[MAX];
int turnaround_time[MAX];
float normalised_turnaround_time[MAX];
int num_of_jobs, j;
scanf("%d", &num_of_jobs);
scanf("%d", &arrival_time[j]);
scanf("%d", &service_time[j]);
// Calculate start time, finish time, turnaround time, and normalized turnaround time
if (j == 0) {
start_time[j] = arrival_time[j]; // First job starts at its arrival time
} else {
avg_turnaround_time += turnaround_time[j];
// Display results
printf("\nJob\tArrival\tService\tStart\tFinish\tTurnaround\tNormalized\n");
printf("J[%d]\t%d\t%d\t%d\t%d\t%d\t\t%.2f\n",
j + 1,
arrival_time[j],
service_time[j],
start_time[j],
finish_time[j],
turnaround_time[j],
normalised_turnaround_time[j]);
avg_turnaround_time /= num_of_jobs;
return 0;
}
Share
You said:
ChatGPT said:
ChatGPT
This program is designed to implement the First-Come-First-Served (FCFS) CPU scheduling algorithm in
C. Let's go through its components step by step:
1. Key Variables
service_time[max]: Array to store the CPU burst times (service times) for jobs.
turnaround_time[max]: Array to store the turnaround time for each job. Turnaround time is
calculated as: Turnaround Time=Finish Time−Arrival Time\text{Turnaround Time} = \text{Finish
Time} - \text{Arrival Time}Turnaround Time=Finish Time−Arrival Time
2. Input
o Service Time: The amount of CPU time required to complete the job.
3. Processing
1. Start Time:
o The first job starts at time 0 (if its arrival time is 0).
o Each subsequent job starts after the previous job finishes, i.e.:
Start Time[j]=Finish Time[j-1]\text{Start Time[j]} = \text{Finish Time[j-
1]}Start Time[j]=Finish Time[j-1]
2. Finish Time:
3. Turnaround Time:
4. Output
o Service Time
o Start Time
o Finish Time
o Turnaround Time
o These are specific to Turbo C/C++ and may not work in modern compilers. Replace
clrscr() with a newline or screen-clearing function like system("clear") or system("cls").
o Remove getch() unless you want to wait for a key press after each output.
o The condition if(0 > j-1) is unnecessary and incorrect. The first job's start time should
always be: Start Time[0]=Max(0, Arrival Time[0])\text{Start Time[0]} = \text{Max(0,
Arrival Time[0])}Start Time[0]=Max(0, Arrival Time[0])
3. finish_time[j-1] for j = 0:
o In the loop, finish_time[j-1] will cause an error for j = 0. This needs a special case for the
first job: Start Time[0]=0\text{Start Time[0]} = 0Start Time[0]=0
CopyEdit
o Simple but can result in convoy effect (long jobs delaying shorter ones).
Example: Jobs arrive at times [0,2,4][0, 2, 4][0,2,4] with service times [3,5,2][3, 5, 2][3,5,2]:
4o
You said:
YES
ChatGPT said:
ChatGPT
Here’s the corrected and improved version of your FCFS scheduling algorithm program in C:
CopyEdit
#include <stdio.h>
#define MAX 20
int main() {
int arrival_time[MAX];
int service_time[MAX];
int start_time[MAX];
int finish_time[MAX];
int turnaround_time[MAX];
float normalised_turnaround_time[MAX];
int num_of_jobs, j;
float avg_turnaround_time = 0.0;
scanf("%d", &num_of_jobs);
scanf("%d", &arrival_time[j]);
scanf("%d", &service_time[j]);
// Calculate start time, finish time, turnaround time, and normalized turnaround time
if (j == 0) {
} else {
avg_turnaround_time += turnaround_time[j];
// Display results
printf("\nJob\tArrival\tService\tStart\tFinish\tTurnaround\tNormalized\n");
for (j = 0; j < num_of_jobs; j++) {
printf("J[%d]\t%d\t%d\t%d\t%d\t%d\t\t%.2f\n",
j + 1,
arrival_time[j],
service_time[j],
start_time[j],
finish_time[j],
turnaround_time[j],
normalised_turnaround_time[j]);
avg_turnaround_time /= num_of_jobs;
return 0;
o The first job starts at its arrival time. Other jobs start after the previous one finishes or
when they arrive, whichever is later.
Example Run
Input:
less
CopyEdit
Output:
css
CopyEdit
J[1] 0 3 0 3 3 1.00
J[2] 2 5 3 8 6 1.20
J[3] 4 2 8 10 6 3.00
4o
You said:
ChatGPT said:
ChatGPT
To explain the First-Come-First-Served (FCFS) CPU Scheduling Algorithm to your students, follow these
steps:
Imagine a queue at a ticket counter. The person who arrives first is served first, followed by the
next person, and so on. No one can skip the queue, and service is provided in the order of
arrival.
Similarly, in FCFS scheduling, jobs are processed in the order they arrive.
5. Turnaround Time: The total time a job spends in the system, from arrival to completion.
Turnaround Time=Finish Time−Arrival Time\text{Turnaround Time} = \text{Finish Time} - \
text{Arrival Time}Turnaround Time=Finish Time−Arrival Time
6. Normalized Turnaround Time: How efficiently the job was executed relative to its service time.
Normalized Turnaround Time=Turnaround TimeService Time\text{Normalized Turnaround Time}
= \frac{\text{Turnaround Time}}{\text{Service
Time}}Normalized Turnaround Time=Service TimeTurnaround Time
o For the first job, the start time is its arrival time.
3. Calculate Finish Time: Finish Time=Start Time+Service Time\text{Finish Time} = \text{Start Time}
+ \text{Service Time}Finish Time=Start Time+Service Time