Implementing FCFS Scheduling Algorithm in C++

Here is C++ implementation of First Come First Serve (FCFS) algorithm.

```#include<iostream.h>

#include<conio.h>

#include <stdlib.h>

void main()

{

clrscr();

int pn = 4;

int at[4];

int wt[4];

int et[4];

int tt[4];

int i;

for (i = 0; i < pn; i++)

{

cout << "\n Enter arrival time of P" << i + 1 << ":";

cin >> at[i];

cout << "\n Enter execution time of P" << i + 1 << ":";

cin >> et[i];

}

wt[0] = 0;

tt[0] = et[0];

int tte = et[0];

for (i = 1; i < pn; i++)

{

wt[i] = wt[i - 1] + et[i - 1] - (at[i] - at[i - 1]);

tt[i] = wt[i] + et[i];

tte = tte + et[i];

}

cout << " \n Result";

cout << "\n PNO\tAT\tWT\tET\tTT";

for (i = 0; i < pn; i++)

{

cout << "\n P" << i + 1 << "\t" << at[i] << "\t" << wt[i] << "\t" << et[i] << "\t" << tt[i];

}

cout << "\n Total time:" << tte;

int twt = 0;

int ttt = 0;

for (i = 0; i < pn; i++)

{

twt += wt[i];

ttt += tt[i];

}

float awt = (float) twt / pn;

cout << "\n Average Waiting time:" << awt;

float att = (float) ttt / pn;

cout << "\n Average Turnaround Time:" << att;

getch();

}

/* OUTPUT

Enter arrival time of P1:0

Enter execution time of P1:2

Enter arrival time of P2:1

Enter execution time of P2:6

Enter arrival time of P3:2

Enter execution time of P3:9

Enter arrival time of P4:3

Enter execution time of P4:1

Result

PNO    AT      WT      ET      TT

P1     0       0       2       2

P2     1       1       6       7

P3     2       6       9       15

P4     3       14      1       15

Total time:18

Average Waiting time:5.25

Average Turnaround Time:9.75

*/
```

This site uses Akismet to reduce spam. Learn how your comment data is processed.