0% found this document useful (0 votes)
10 views2 pages

FCFS Scheduling Program in C

This C program implements a scheduling algorithm for processes, allowing users to input arrival and burst times. It sorts the processes by arrival time, calculates completion, turnaround, and waiting times, and then displays the results along with average waiting and turnaround times. The program is designed to handle up to 20 processes.

Uploaded by

ayushkhatai10
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)
10 views2 pages

FCFS Scheduling Program in C

This C program implements a scheduling algorithm for processes, allowing users to input arrival and burst times. It sorts the processes by arrival time, calculates completion, turnaround, and waiting times, and then displays the results along with average waiting and turnaround times. The program is designed to handle up to 20 processes.

Uploaded by

ayushkhatai10
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

#include <stdio.

h>

struct Process {
int pid; // Process ID
int at; // Arrival Time
int bt; // Burst Time
int ct; // Completion Time
int wt; // Waiting Time
int tat; // Turnaround Time
};

int main() {
int n, i, j;
struct Process p[20], temp;
float total_wt = 0, total_tat = 0;

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


scanf("%d", &n);

// Input arrival and burst times


for (i = 0; i < n; i++) {
p[i].pid = i + 1;
printf("Enter arrival time of Process %d: ", i + 1);
scanf("%d", &p[i].at);
printf("Enter burst time of Process %d: ", i + 1);
scanf("%d", &p[i].bt);
}

// Sort by arrival time


for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].at > p[j].at) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}

// Calculate completion, turnaround and waiting time


int current_time = 0;
for (i = 0; i < n; i++) {
if (current_time < p[i].at)
current_time = p[i].at;
p[i].ct = current_time + p[i].bt;
current_time = p[i].ct;
p[i].tat = p[i].ct - p[i].at;
p[i].wt = p[i].tat - p[i].bt;
total_wt += p[i].wt;
total_tat += p[i].tat;
}

// Display results
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n",
p[i].pid, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt);
}

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


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

return 0;
}

You might also like