0% found this document useful (0 votes)
17 views6 pages

FCFS

first come first serve

Uploaded by

yash yadav
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)
17 views6 pages

FCFS

first come first serve

Uploaded by

yash yadav
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/ 6

FCFS

#include <stdio.h>

int main() {

int n, i, j;

float total_wait_time = 0, total_turnaround_time = 0;

// Input the number of processes

printf("Enter the number of processes: ");

scanf("%d", &n);

int process[n], burst_time[n], arrival_time[n], wait_time[n], turn_around_time[n],


completion_time[n];

// Input the arrival and burst times for each process

for (i = 0; i < n; i++) {

process[i] = i + 1; // Process number

printf("Enter arrival time for process %d: ", i + 1);

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

printf("Enter burst time for process %d: ", i + 1);

scanf("%d", &burst_time[i]);

// Sort the processes based on arrival time, maintaining the original order for same arrival times

for (i = 0; i < n - 1; i++) {

for (j = i + 1; j < n; j++) {

// Sort by arrival time, and if they are the same, keep the original order
if (arrival_time[i] > arrival_time[j] ||

(arrival_time[i] == arrival_time[j] && process[i] > process[j])) {

// Swap arrival time

int temp = arrival_time[i];

arrival_time[i] = arrival_time[j];

arrival_time[j] = temp;

// Swap burst time

temp = burst_time[i];

burst_time[i] = burst_time[j];

burst_time[j] = temp;

// Swap process number

temp = process[i];

process[i] = process[j];

process[j] = temp;

// Initialize current_time to track CPU's time

int current_time = 0;

// Calculate completion, waiting, and turnaround times for each process

for (i = 0; i < n; i++) {

// If the current time is less than the arrival time, CPU will be idle

if (current_time < arrival_time[i]) {


current_time = arrival_time[i]; // The process starts at its arrival time

// Completion time is the current time + burst time

completion_time[i] = current_time + burst_time[i];

current_time = completion_time[i]; // Update current time to when this process finishes

// Turnaround time = completion time - arrival time

turn_around_time[i] = completion_time[i] - arrival_time[i];

// Waiting time = turnaround time - burst time

wait_time[i] = turn_around_time[i] - burst_time[i];

// Add to the totals for averaging later

total_wait_time += wait_time[i];

total_turnaround_time += turn_around_time[i];

// Print process details

printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\tCompletion Time\


n");

for (i = 0; i < n; i++) {

printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", process[i], arrival_time[i], burst_time[i],


wait_time[i], turn_around_time[i], completion_time[i]);

// Calculate and display averages

printf("\nAverage Waiting Time: %.2f", total_wait_time / n);


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

return 0;

Code Overview

1. Input Data:
o The program starts by asking for the number of processes.
o It then collects the arrival time and burst time for each process.
2. Sorting Processes:
o The processes are sorted based on their arrival times. If two processes have the
same arrival time, they maintain their input order. This is achieved using a
nested loop that compares arrival times and, if necessary, process indices.
3. Time Calculations:
o The program tracks the current time as it processes each job.
o For each process, it calculates:
 Completion Time: When the process finishes executing.
 Turnaround Time: The total time taken from arrival to completion.
 Waiting Time: The time the process spends waiting in the queue (i.e.,
turnaround time minus burst time).
4. Output:
o Finally, it prints the details of each process, including its arrival time, burst
time, waiting time, turnaround time, and completion time.
o It also calculates and prints the average waiting time and average turnaround
time for all processes.

Detailed Breakdown

1. Input Section:

printf("Enter the number of processes: ");


scanf("%d", &n);
o This part prompts the user to enter the number of processes, which is stored in
variable n.
2. Data Collection:

for (i = 0; i < n; i++) {


process[i] = i + 1; // Process number
printf("Enter arrival time for process %d: ", i + 1);
scanf("%d", &arrival_time[i]);
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &burst_time[i]);
}

o This loop collects arrival and burst times for each process and assigns a unique
process number.
3. Sorting Logic:

for (i = 0; i < n - 1; i++) {


for (j = i + 1; j < n; j++) {
if (arrival_time[i] > arrival_time[j] ||
(arrival_time[i] == arrival_time[j] && process[i] >
process[j])) {
// Swap logic
}
}
}

o The sorting mechanism checks if one arrival time is greater than the other. If
they are equal, it uses the process index to maintain the order of input.
o This ensures that processes are sorted first by arrival time and then by their
order of input when arrival times are the same.
4. Calculating Times:

for (i = 0; i < n; i++) {


if (current_time < arrival_time[i]) {
current_time = arrival_time[i]; // The process starts at its
arrival time
}
completion_time[i] = current_time + burst_time[i];
current_time = completion_time[i];
turn_around_time[i] = completion_time[i] - arrival_time[i];
wait_time[i] = turn_around_time[i] - burst_time[i];
}

o The current_time variable is updated based on when the CPU is free to


execute the next process.
o If the current time is less than the process's arrival time, the CPU will be idle
until that process arrives.
5. Output Section:

printf("\nProcess\tArrival Time\tBurst Time\tWaiting Time\tTurnaround


Time\tCompletion Time\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", process[i],
arrival_time[i], burst_time[i], wait_time[i], turn_around_time[i],
completion_time[i]);
}

o This section displays a table with all relevant information about each process
and calculates the averages.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy