Program to demonstrate FCFS CPU scheduling

Share

The most straightforward scheduling technique is first in, first out (FIFO), commonly referred to as first come, first served (FCFS). Processes are basically queued using FIFO in order to enter the ready queue. In this, the first process will be carried out first, and the subsequent process won’t begin until the first has been carried out thoroughly.

Here we consider

// Array processes[] = {1, 2, 3, 4};

// Array burst_time[] = {4, 6, 9, 1};

// Array int arrival_time[] = {1, 2, 3, 0};

At first, we sort the given arrival time array and with each shift in the array, we do the same for the processes array item at the corresponding index. Basically, we sort the processes array according to the given arrival time array. Here is a basic example:

for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			if(a[i]>a[j]){
				int temp = a[j];
				a[j] = a[i];
				a[i] = temp;
				int temp_b = b[j];
				b[j] = b[i];
				b[i] = temp_b; 
			}
		}	
	}

Here is a complete C program:

#include <stdio.h>

// Function to find the waiting time for all processes
void findWaitingTime(int processes[], int n, int bt[], int wt[], int at[])
{
	int service_time[n];
	service_time[0] = at[0];
	wt[0] = 0;

	// calculating waiting time
	for (int i = 1; i < n ; i++)
	{
		// Add burst time of previous processes
		service_time[i] = service_time[i-1] + bt[i-1];

		// Find waiting time for current process =
		// sum - at[i]
		wt[i] = service_time[i] - at[i];

		// If waiting time for a process is in negative
		// that means it is already in the ready queue
		// before CPU becomes idle so its waiting time is 0
		if (wt[i] < 0)
			wt[i] = 0;
	}
}

// Function to calculate turn around time
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
{
	// Calculating turnaround time by adding bt[i] + wt[i]
	for (int i = 0; i < n ; i++)
		tat[i] = bt[i] + wt[i];
}

// Function to calculate average waiting and turn-around
// times.
void findavgTime(int processes[], int n, int bt[], int at[])
{
	int wt[n], tat[n];
	findWaitingTime(processes, n, bt, wt, at);
	findTurnAroundTime(processes, n, bt, wt, tat);

	printf("Processes \t Burst Time \t Arrival Time \t Waiting Time \t Turn-Around Time \t Completion Time \n");
	int total_wt = 0, total_tat = 0;
	for (int i = 0 ; i < n ; i++)
	{
		total_wt = total_wt + wt[i];
		total_tat = total_tat + tat[i];
		int compl_time = tat[i] + at[i];
		printf("\t %d \t\t %d \t\t %d \t\t %d \t\t %d \t\t %d \n", processes[i], bt[i], at[i], wt[i],
			tat[i], compl_time );
	}

	printf("Average waiting time = %.2f \n", (float)total_wt / (float)n);
	printf("Average turn around time = %.2f \n", (float)total_tat / (float)n);
}

// Driver code
int main()
{
	// Process IDs
	int processes[] = {1, 2, 3, 4};
	int n = sizeof processes / sizeof processes[0];

	// Burst time of all processes
	int burst_time[] = {4, 6, 9, 1};

	// Arrival time of all processes
	int arrival_time[] = {1, 2, 3, 0};

	// short process according to arrival time
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			if(arrival_time[i]>arrival_time[j]){
				int temp = arrival_time[j];
				arrival_time[j] = arrival_time[i];
				arrival_time[i] = temp;
				int temp_p = processes[j];
				processes[j] = processes[i];
				processes[i] = temp_p; 
			}
		}	
	}

	findavgTime(processes, n, burst_time, arrival_time);

	return 0;
}

Output

FCFS

Source code repository: https://github.com/CodewithSudeep/operating-system-c-program

Other C Programs: https://codewithsudeep.com/category/c-program/

Share
Sudeep Mishra

Sudeep Mishra

Healing

%d bloggers like this: