Fcfs Program In C

The operating system is required to select a process from the pool of processes ready to be executed whenever the CPU becomes available. This task is carried out by a temporary CPU scheduler which picks a memory process ready for execution to allocate the CPU.

In this tutorial, we will discuss the First-Come-First-Served (FCFS) scheduling algorithm.

The goal is to apply the FCFS scheduling algorithm to calculate the mean waiting time and mean turnaround time for a set of n processes with their respective burst durations. The most basic scheduling method is first in, first out (FIFO), also known as first come, first served (FCFS). Processes are lined up in a queue based on the order of their arrival in the ready queue.

Term used:

  • When the execution is finished, it is the completion time.
  • Turnaround Time: The turnaround time is calculated by adding the burst and waiting times.
  • Waiting time: The amount of time a process must wait before receiving the CPU and beginning its execution is referred to as waiting time.

In this scenario, the initial step will be executed first, and the following task will not commence until the first one has been fully completed.

Here, we are assuming that all operations arrive simultaneously or at zero time.

How can I use software to calculate the Round Robin timings below?

  • Time when a process reaches the end of its execution.
  • Turnaround time is the interval of time between completion and arrival. Turn Around Time = Arrival Time - Completion Time
  • Waiting Time (W.T): The interval between burst and turn-around times.
  • Turnaround time minus burst time equals waiting time.
  • Implementation

  • Enter the processes together with their burst time in step one (bt).
  • Calculate the waiting time (wt) for each step.
  • Because the first process won't have to wait, the waiting time for process 1 will be zero, or wt[0] = 0.
  • Determine all other processes' waiting times, i.e., for process I wt[i] = bt[i-1] + wt[i-1].
  • Calculate the turnaround time for each process as waiting time + burst time.
  • Calculate the average delay time as follows: total wait time / number of processes.
  • Calculate the average turnaround time as follows: total turnaround time / number of procedures.

Program Code:

Example

// C program for implementation of FCFS
// scheduling
#include < stdio.h >
// Function to find the waiting time for all
// processes
void findWaitingTime( int processes[ ] , int n ,
					 int bt[ ], int wt[ ])
{
	// waiting time for first process is 0
	wt[ 0 ] = 0;

	// calculating waiting time
	for ( int i = 1 ; i < n ; i++ )
		wt[ i ] = bt[ i - 1 ] + wt[ i - 1 ] ;
}
// 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 time
void findavgTime(int processes[], int n, int bt[])
{
	int wt[n], tat[n], total_wt = 0, total_tat = 0;

	// Function to find waiting time of all processes
	findWaitingTime(processes, n, bt, wt);

	// Function to find turn around time for all processes
	findTurnAroundTime(processes, n, bt, wt, tat);

	// Display processes along with all details
	printf("Processes Burst time Waiting time Turn around time\n");

	// Calculate total waiting time and total turn
	// around time
	for (int i = 0; i < n; i++)
	{
		total_wt = total_wt + wt[i];
		total_tat = total_tat + tat[i];
		printf(" %d ", (i + 1));
		printf("	 %d ", bt[i]);
		printf("	 %d", wt[i]);
		printf("	 %d\n", tat[i]);
	}
	int s = (float)total_wt / (float)n;
	int t = (float)total_tat / (float)n;
	printf("Average waiting time = %d", s);
	printf("\n");
	printf("Average turn around time = %d ", t);
}

// Driver code
int main()
{
	// process id's
	int processes[] = {1, 2, 3};
	int n = sizeof processes / sizeof processes[0];

	// Burst time of all processesN
	int burst_time[] = {10, 5, 8};

	findavgTime(processes, n, burst_time);
	return 0;
}

Output:

Output

Processes  Burst time  Waiting time  Turn around time
 1                   10                    0                         10
 2                    5                     10                       15
 3                   8                      15                       23
Average waiting time = 8.33333
Average turn around time = 16
................................................................................................
Process executed in 2.11 seconds
Press any key to continue.

NOTE:

  • Non-preemptive
  • Average Waiting is not a good idea.
  • Cannot use resources concurrently: results in the Convoy effect (Take into consideration a scenario where there are several IO-bound processes and one CPU-bound one. When a CPU-bound process acquires CPU, the IO-bound processes must wait for it. Better would have been for the IO-bound activity to use the CPU for a while before switching to IO devices.

Input Required

This code uses input(). Please provide values below: