0% found this document useful (0 votes)
6 views

FCFS Implementation with explaination

FCFS (First-Come-First-Serve) is the simplest scheduling algorithm. The basic idea is the process that comes first is scheduled first. It follows the principle of FIFO (First-in-First-Out). It is a non-preemptive algorithm.

Uploaded by

hgull8490
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

FCFS Implementation with explaination

FCFS (First-Come-First-Serve) is the simplest scheduling algorithm. The basic idea is the process that comes first is scheduled first. It follows the principle of FIFO (First-in-First-Out). It is a non-preemptive algorithm.

Uploaded by

hgull8490
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

This C program implements the First-Come, First-Served (FCFS) scheduling algorithm, which is a simple,

non-preemptive CPU scheduling algorithm used in operating systems. The program calculates and
displays the waiting time (wt) and turnaround time (tt) for each process, and also computes the
average waiting time and average turnaround time for all processes.

Structure Definition

struct process

int pid; // Process ID

int bt; // Burst Time (the time the process requires for execution)

int wt; // Waiting Time (time a process has to wait before execution starts)

int tt; // Turnaround Time (total time spent by the process from arrival to completion)

p[10]; // Array of process structures (maximum 10 processes)

 pid: Process ID to uniquely identify each process.


 bt: Burst Time, the amount of CPU time required by the process for execution.
 wt: Waiting Time, the total time a process has to wait before it starts executing.
 tt: Turnaround Time, the total time taken by the process from its arrival in the ready
queue to its completion.

Main Function

int main(){

int i, n, totwt, tottt, avg1, avg2;

printf("enter the no of process \n");

scanf("%d", &n); // Reading the number of processes


 n: Number of processes.
 totwt: Total waiting time for all processes.
 tottt: Total turnaround time for all processes.
 avg1: Average waiting time for all processes.
 avg2: Average turnaround time for all processes.

Input Burst Time for Each Process

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

p[i].pid = i; // Assign process ID starting from 1

printf("enter the burst time \n");

scanf("%d", &p[i].bt); // Read burst time for each process

The program asks the user to input the burst time for each process. The pid is automatically assigned as
the loop index, starting from 1.

FCFS Calculation (Waiting Time and Turnaround Time)

p[1].wt = 0; // The first process has no waiting time

p[1].tt = p[1].bt + p[1].wt; // Turnaround time = burst time + waiting time (for the first process,
waiting time = 0)

i = 2;

while(i <= n){

p[i].wt = p[i-1].bt + p[i-1].wt; // Waiting time for process i = waiting time of previous process +
burst time of previous process

p[i].tt = p[i].bt + p[i].wt; // Turnaround time = burst time + waiting time

i++;

}
 First Process: The waiting time for the first process is 0 because it starts execution
immediately.
 Subsequent Processes: For each subsequent process, the waiting time is calculated as the
sum of the burst time of all previous processes. The turnaround time is simply the sum
of burst time and waiting time for each process.

Displaying Results and Calculating Averages

i = 1;

totwt = tottt = 0; // Initialize total waiting time and total turnaround time

printf("\n processid \t bt\t wt\t tt\n");

while(i <= n){

printf("\n\t%d \t%d \t%d \t%d", p[i].pid, p[i].bt, p[i].wt, p[i].tt); // Print each process's PID,
burst time, waiting time, and turnaround time

totwt = p[i].wt + totwt; // Add waiting time of the current process to total waiting time

tottt = p[i].tt + tottt; // Add turnaround time of the current process to total turnaround time

i++;

avg1 = totwt / n; // Calculate average waiting time

avg2 = tottt / n; // Calculate average turnaround time

printf("\navg1=%d \t avg2=%d \t", avg1, avg2); // Print the averages

return 0;

 The program prints the process ID, burst time, waiting time, and turnaround time for
each process.
 It also calculates the total waiting time (totwt) and total turnaround time (tottt).
 The averages are calculated by dividing the total waiting and turnaround times by the
number of processes (n), and the averages are printed.

You might also like