[go: up one dir, main page]

0% found this document useful (0 votes)
8 views11 pages

FCFS2

The document provides a C program that implements the First-Come-First-Served (FCFS) CPU scheduling algorithm. It calculates various metrics for jobs including start time, finish time, turnaround time, and normalized turnaround time based on user input for arrival and service times. The program also displays the results in a formatted table and computes the average turnaround time for all jobs.

Uploaded by

rajitha.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

FCFS2

The document provides a C program that implements the First-Come-First-Served (FCFS) CPU scheduling algorithm. It calculates various metrics for jobs including start time, finish time, turnaround time, and normalized turnaround time based on user input for arrival and service times. The program also displays the results in a formatted table and computes the average turnaround time for all jobs.

Uploaded by

rajitha.cse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

#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;

// Input number of jobs

printf("Enter the number of jobs:\n");

scanf("%d", &num_of_jobs);

// Input arrival and service times for each job

for (j = 0; j < num_of_jobs; j++) {

printf("Enter the arrival time of job[%d]: ", j + 1);

scanf("%d", &arrival_time[j]);

printf("Enter the service time of job[%d]: ", j + 1);

scanf("%d", &service_time[j]);

// Calculate start time, finish time, turnaround time, and normalized turnaround time

for (j = 0; j < num_of_jobs; j++) {

if (j == 0) {
start_time[j] = arrival_time[j]; // First job starts at its arrival time

} else {

start_time[j] = (finish_time[j - 1] > arrival_time[j]) ? finish_time[j - 1] : arrival_time[j];

finish_time[j] = start_time[j] + service_time[j];

turnaround_time[j] = finish_time[j] - arrival_time[j];

normalised_turnaround_time[j] = (float)turnaround_time[j] / service_time[j];

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]);

// Display average turnaround time

avg_turnaround_time /= num_of_jobs;

printf("\nAverage Turnaround Time: %.2f\n", avg_turnaround_time);

return 0;

}
Share

You said:

/* 1.write a c program using FCFS cpu scheduling algorithm */ #include<stdio.h> #include<conio.h>


#define max 20 void 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_turn=0.0; printf("enter the no of jobs:\n"); scanf("%d",&num_of_jobs);
for(j=0;j<num_of_jobs;j++) { printf("enter the arrival_time of j[%d] ",j+1); scanf("%d",&arrival_time[j]);
printf("enter the service_time of j[%d] ",j+1); scanf("%d",&service_time[j]); } //caluculate turnaround
time ,finish time and normalised turnaround time for(j=0;j<num_of_jobs;j++) { if(0>j-1) start_time[j]=0;
start_time[j]=finish_time[j-1]; finish_time[j]=start_time[j]+service_time[j];
turnaround_time[j]=finish_time[j]-arrival_time[j];
normalised_turnaround_time[j]=(float)turnaround_time[j]/service_time[j];
avg_turn+=turnaround_time[j]; } clrscr(); for(j=0;j<num_of_jobs;j++) { printf("j[%d]:\t",j+1); printf("%d\
t",arrival_time[j]); printf("%d\t",service_time[j]); printf("%d\t",start_time[j]); printf("%d\
t",finish_time[j]); printf("%d\t",turnaround_time[j]); printf("%f\n",normalised_turnaround_time[j]);
getch(); } } EXPLAIN THIS PROGRAM

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

 arrival_time[max]: Array to store the arrival times of jobs.

 service_time[max]: Array to store the CPU burst times (service times) for jobs.

 start_time[max]: Array to store the times at which jobs start execution.

 finish_time[max]: Array to store the times at which jobs finish execution.

 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

 normalised_turnaround_time[max]: Array to store normalized turnaround times for each job.


Normalized turnaround time is given by:
Normalized Turnaround Time=Turnaround TimeService Time\text{Normalized Turnaround Time}
= \frac{\text{Turnaround Time}}{\text{Service
Time}}Normalized Turnaround Time=Service TimeTurnaround Time
 avg_turn: Float variable to compute the average turnaround time.

2. Input

1. The program first asks for the number of jobs (num_of_jobs).

2. For each job, the user is prompted to input:

o Arrival Time: When the job arrives at the system.

o Service Time: The amount of CPU time required to complete the job.

3. Processing

The program calculates key metrics for FCFS scheduling:

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:

o Calculated as: Finish Time[j]=Start Time[j]+Service Time[j]\text{Finish Time[j]} = \


text{Start Time[j]} + \text{Service Time[j]}Finish Time[j]=Start Time[j]+Service Time[j]

3. Turnaround Time:

o Calculated as: Turnaround Time[j]=Finish Time[j]−Arrival Time[j]\text{Turnaround


Time[j]} = \text{Finish Time[j]} - \text{Arrival Time[j]}Turnaround Time[j]=Finish Time[j]
−Arrival Time[j]

4. Normalized Turnaround Time:

o Calculated as: Normalized Turnaround Time[j]=Turnaround Time[j]Service Time[j]\


text{Normalized Turnaround Time[j]} = \frac{\text{Turnaround Time[j]}}{\text{Service
Time[j]}}Normalized Turnaround Time[j]=Service Time[j]Turnaround Time[j]

5. Average Turnaround Time:

o Summed up in avg_turn: \text{avg_turn} += \text{Turnaround Time[j]}

4. Output

 The program displays the details for each job:


o Arrival Time

o Service Time

o Start Time

o Finish Time

o Turnaround Time

o Normalized Turnaround Time

 Additionally, the program calculates the average turnaround time.

Errors/Issues in the Code

1. clrscr() and getch() are non-standard functions:

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.

2. Incorrect Initialization for First Job Start Time:

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

o For other jobs: Start Time[j]=Max(Finish Time[j-1], Arrival Time[j])\text{Start Time[j]} = \


text{Max(Finish Time[j-1], Arrival Time[j])}Start Time[j]=Max(Finish Time[j-
1], Arrival Time[j])

4. No Average Turnaround Time Output:

o The program calculates avg_turn, but it doesn’t print it. Add:

CopyEdit

printf("Average Turnaround Time: %.2f\n", avg_turn / num_of_jobs);

Simplified FCFS Explanation

 First-Come-First-Served (FCFS) scheduling:


o Jobs are executed in the order they arrive.

o No preemption (interrupting running jobs).

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]:

 Job 1 starts at 0, finishes at 3.

 Job 2 starts at 3, finishes at 8.

 Job 3 starts at 8, finishes at 10.

Would you like the corrected version of this code?

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;

// Input number of jobs

printf("Enter the number of jobs:\n");

scanf("%d", &num_of_jobs);

// Input arrival and service times for each job

for (j = 0; j < num_of_jobs; j++) {

printf("Enter the arrival time of job[%d]: ", j + 1);

scanf("%d", &arrival_time[j]);

printf("Enter the service time of job[%d]: ", j + 1);

scanf("%d", &service_time[j]);

// Calculate start time, finish time, turnaround time, and normalized turnaround time

for (j = 0; j < num_of_jobs; j++) {

if (j == 0) {

start_time[j] = arrival_time[j]; // First job starts at its arrival time

} else {

start_time[j] = (finish_time[j - 1] > arrival_time[j]) ? finish_time[j - 1] : arrival_time[j];

finish_time[j] = start_time[j] + service_time[j];

turnaround_time[j] = finish_time[j] - arrival_time[j];

normalised_turnaround_time[j] = (float)turnaround_time[j] / service_time[j];

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]);

// Display average turnaround time

avg_turnaround_time /= num_of_jobs;

printf("\nAverage Turnaround Time: %.2f\n", avg_turnaround_time);

return 0;

Key Changes and Fixes

1. Special Case for First Job:

o The first job starts at its arrival time. Other jobs start after the previous one finishes or
when they arrive, whichever is later.

2. Avoided Undefined Behavior:

o Removed the direct access of finish_time[j - 1] when j = 0.

3. Corrected Output Formatting:

o Improved the table layout for better readability.

4. Average Turnaround Time Display:

o Added a line to display the calculated average turnaround time.

5. Removed clrscr() and getch():


o These are replaced with modern and portable implementations.

Example Run

Input:

less

CopyEdit

Enter the number of jobs:

Enter the arrival time of job[1]: 0

Enter the service time of job[1]: 3

Enter the arrival time of job[2]: 2

Enter the service time of job[2]: 5

Enter the arrival time of job[3]: 4

Enter the service time of job[3]: 2

Output:

css

CopyEdit

Job Arrival Service Start Finish Turnaround Normalized

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

Average Turnaround Time: 5.00

Let me know if you need further assistance! 😊

4o

You said:

HOW CAN I EXAPLIN THIS TO STUDENTS

ChatGPT said:

ChatGPT
To explain the First-Come-First-Served (FCFS) CPU Scheduling Algorithm to your students, follow these
steps:

1. Start with Real-Life Analogy

Use a relatable analogy to introduce the concept:

 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.

2. Explain the Key Terms

Before diving into the program, explain the key concepts:

1. Arrival Time: When a job (process) arrives in the system.

o Example: Job 1 arrives at time 0.

2. Service Time: The CPU time required to execute the job.

o Example: Job 1 requires 3 units of time to finish.

3. Start Time: When the CPU starts processing the job.

o Job 1 starts at time 0, and Job 2 starts after Job 1 finishes.

4. Finish Time: When the job finishes execution.

o Job 1 finishes at time 3.

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

3. Explain the Step-by-Step Algorithm

Break down the algorithm into steps:

1. Input the Arrival and Service Times:


o Ask for the number of jobs and their arrival and service times.

2. Calculate Start Time:

o For the first job, the start time is its arrival time.

o For subsequent jobs, the start time is the later of:

 Finish time of the previous job.

 Its own 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

4. Calculate Turnaround Time: Turnaround Time=Finish Time−Arrival Time\text{Turnaround Time}


= \text{Finish Time} - \text{Arrival Time}Turnaround Time=Finish Time−Arrival Time

5. Calculate Normalized Turnaround Time:


Normalized Turnaround Time=Turnaround TimeService Time\text{Normalized Turnaround Time}
= \frac{\text{Turnaround Time}}{\text{Service
Time}}Normalized Turnaround Time=Service TimeTurnaround Time

6. Compute Average Turnaround Time:

o Add all turnaround times and divide by the number of jobs.

4. Walk Through the Example

You might also like