How to write a c Program to Simulation program for FCFS ( First Come First Serve ) in C Programming Language ?
Solution:/*
Write the simulation program for FCFS. The arrival time and first
CPU bursts of different jobs should be input to the system.
The output should give the Gantt Chart, Turnaround Time and Waiting time for each process and average
times.
*/
#include<stdio.h>
struct Input
{
char pname[10];
int bt,at,ct,tbt;
}tab[5];
struct Sequence
{
int start,end;
char pname[10];
}seq[100],seq1[20];
int finish,time,n,k,prev;
void getinput()
{
int i;
clrscr();
printf("\nEnter No.of Processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Process name:");
scanf("%s",tab[i].pname);
printf("Burst time:");
scanf("%d",&tab[i].bt);
printf("Arrival time:");
scanf("%d",&tab[i].at);
tab[i].tbt = tab[i].bt;
}
}
void printinput()
{
int i;
printf("\n\n\nProcess\tBT\tAT");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d",tab[i].pname,tab[i].tbt,tab[i].at);
getch();
}
void bubble()
{
struct Input t;
int i,j;
for(i=0;i<n;i++)
for(j=0;j< (n-1)-i;j++)
if(tab[j].at>tab[j+1].at)
{
t = tab[j];
tab[j] = tab[j+1];
tab[j+1] = t;
}
}
void printoutput()
{
int i;
float AvgTAT=0,AvgWT=0;
clrscr();
printf("\n *******Final Table*********");
printf("\n\nProcess\tAT\tBT\tCT\tTAT\tWT");
for(i=0;i<n;i++)
{
printf("\n\n%s\t%d\t%d\t%d\t%d\t%d",tab[i].pname,
tab[i].at,
tab[i].bt,
tab[i].ct,
tab[i].ct-tab[i].at,
tab[i].ct-tab[i].at-tab[i].bt);
AvgTAT += tab[i].ct-tab[i].at;
AvgWT += tab[i].ct-tab[i].at-tab[i].bt;
}
AvgTAT/=n;
AvgWT/=n;
printf("\n\nAverage TAT = %f",AvgTAT);
printf("\n\nAverage WT = %f",AvgWT);
getch();
}
int arrived(int t)
{
int i;
for(i=0;i<n;i++)
if(tab[i].at<=t && tab[i].tbt!=0)
return 1;
return 0;
}
void processinput()
{
int i=0,j;
finish = k = 0;
time=tab[0].at;
while(finish!=n)
{
if(arrived(time))
{
if(tab[i].tbt!=0)
{
for(j=0;j<tab[i].bt;j++)
{
time++;
tab[i].tbt--;
printinput();
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,tab[i].pname);
prev = time;
tab[i].ct=time;
if(tab[i].tbt==0)
{
finish++;
break;
}
}
}
}
else
{
time++;
seq[k].start=prev;
seq[k].end = time;
strcpy(seq[k++].pname,"*");
prev = time;
}
if(time < tab[(i+1)%n].at)
{
i=0;
}
else
i = (i+1)%n;
}
}
void ganttchart()
{
int i,j=1;
clrscr();
seq1[0] = seq[0];
printf("\n ******Gantt Chart*******");
for(i=1;i<k;i++)
{
if(strcmp(seq1[j-1].pname,seq[i].pname)==0)
seq1[j-1].end = seq[i].end;
else
seq1[j++] = seq[i];
}
for(i=0;i<j;i++)
printf("\n\n%d\t%s\t%d",seq1[i].start,seq1[i].pname,seq1[i].end);
getch();
}
void main()
{
int i;
getinput();
printf("\nEntered data-: ");
printinput();
bubble();
printf("\n\nData after sorting according to arrival time-: ");
printinput();
processinput();
printoutput();
ganttchart();
}
Learn More :
FCFS
Input
- C Program to Get User Input and Compare
- C Program Basic logging of user input/output
- C Program File Input & Output Example
- C Program To Input & Print More Than One Words In Single Line
- Take microphone input and send to headphones
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program That in a Given Natural Number x Insert Figure c at Position p
- Input reads in the array positions and range for random generation.
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program to Input Number by the user between 1-100
- How to Gets User Input For Height in C Programming
- DEMULTIPLEXER CO ANALOG INPUTS
- C Program to Input Student Details into a File For Two Subjects
- Program to Display Pie Chart Accepting User Input C Program
- Input a Number and Check if it's Palindrome or not C Program
- Input Number and Print it's Reverse C Program
- Input Number and Check if it's Armstrong C Program
- Input Number and Check if it's Even or Odd C Program
- Input Two Numbers and Print Greater Number C Program
- Input Number and Calculate Sum of it's Digits C Program
Simulation