FCFS CPU scheduling CPP Code

This code implements the First come First Served Scheduling Algorithm. It gets the number of processes and the process name, burst time and arrival time for each user from the user. It then sorts the processes depending upon the arrival time. Calculates and displays the average turn around time and waiting time.Then it will display the Gantt chart.

// Name: FCFS Scheduling Algorithm
// Description:This code implements the
//     First come First Served Scheduling Algor
//     ithm. It gets the number of processes an
//     d the process name, burst time and arriv
//     al time for each user from the user. It
//     then sorts the processes depending upon
//     the arrival time. Calculates and display
//     s the average turn around time and waiti
//     ng time.Then it will display the Gantt c
//     hart.

//
// Inputs:1. Number of processes

/*

2. Process name
3. Burst Time
4. Arrival Time

*/

/* FIRST COME FIRST SERVED SCHEDULING ALGORITHM*/
//PREPROCESSOR DIRECTIVES
#include<stdio.h>
#include<conio.h>
#include<string.h>
//GLOBAL VARIABLES - DECLARATION
int n,Bu[20],Twt,Ttt,A[10],Wt[10],w;
float Awt,Att;
char pname[20][20],c[20][20];
//FUNCTION DECLARATIONS
void Getdata();
void Gantt_chart();
void Calculate();
void fcfs();
//GETTING THE NUMBER OF PROCESSES AND TH
//     E BURST TIME AND ARRIVAL TIME FOR EACH P
//     ROCESS
void Getdata()

    {
    	int i;
    	printf("\n Enter the number of processes: ");
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)

        	{
        		fflush(stdin);
        		printf("\n\n Enter the process name: ");
        		scanf("%s",&pname[i]);
        		printf("\n Enter The BurstTime for Process %s =	",pname[i]);
        		scanf("%d",&Bu[i]);
        		printf("\n Enter the Arrival Time for Process %s =	",pname[i]);
        		scanf("%d",&A[i]);
        	}
    }

    //DISPLAYING THE GANTT CHART
    void Gantt_chart()

        {
        	int i;
        	printf("\n\n\t\t\tGANTT CHART\n");
        	printf("\n-----------------------------------------------------------\n");
        	for(i=1;i<=n;i++)
        		printf("|\t%s\t",pname[i]);
        	printf("|\t\n");
        	printf("\n-----------------------------------------------------------\n");
        	printf("\n");
        	for(i=1;i<=n;i++)
        		printf("%d\t\t",Wt[i]);
        	printf("%d",Wt[n]+Bu[n]);
        	printf("\n-----------------------------------------------------------\n");
        	printf("\n");
    }

    //CALCULATING AVERAGE WAITING TIME AND A
    //     VERAGE TURN AROUND TIME
    void Calculate()

        {
        	int i;
        	//For the 1st process
        	Wt[1]=0;
        	for(i=2;i<=n;i++)

            	{
            		Wt[i]=Bu[i-1]+Wt[i-1];
            	}
            	for(i=1;i<=n;i++)

                	{
                		Twt=Twt+(Wt[i]-A[i]);
                		Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
                	}
                	Att=(float)Ttt/n;
                	Awt=(float)Twt/n;
                	printf("\n\n Average Turn around time=%3.2f ms ",Att);
                	printf("\n\n AverageWaiting Time=%3.2f ms",Awt);
            }

            //FCFS Algorithm
            void fcfs()

                {
                	int i,j,temp, temp1;
                	Twt=0;
                	Ttt=0;
                	Printf(“\n\n FIRST COME FIRST SERVED ALGORITHM\n\n”);
                	for(i=1;i<=n;i++)

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

                        		{
                        			if(A[i]>A[j])

                            			{
                            				temp=Bu[i];
                            				temp1=A[i];
                            				Bu[i]=Bu[j];
                            				A[i]=A[j];
                            				Bu[j]=temp;
                            				A[j]=temp1;
                            				strcpy(c[i],pname[i]);
                            				strcpy(pname[i],pname[j]);
                            				strcpy(pname[j],c[i]);
                            			}
                            		}
                            	}
                            	Calculate();
                            	Gantt_chart();
                        }

                        void main()

                            {
                            	int ch;
                            	clrscr();
                            	Getdata();
                            	fcfs();
                            	getch();
                        }

FCFS CPU SCHEDULING ALGORITHMS

This is the example  code for First come first serve CPU SCHEDULING ALGORITHMS. This is so simple to use and its fully optimized. If need any help please do not hesitate to contact me…

#include <stdio.h>
#include <conio.h>
#include <dos.h>
main()
{
_setcursortype(_NOCURSOR);
clrscr();
int x, y=5, z=1;
float pro, burst[100], burst2=1, gantt=0, gantt2=0, gantt3=0;
int lo, di;
char menu, proc;

sleep(2);
textcolor(15);
gotoxy(43, 13); cprintf(”2″);
delay(100);
gotoxy(43, 13); cprintf(” “);
gotoxy(41, 13); cprintf(”P”);
delay(100);
gotoxy(41, 13); cprintf(” “);
gotoxy(40, 13); cprintf(”U”);
delay(100);
gotoxy(40, 13); cprintf(” “);
gotoxy(39, 13); cprintf(”O”);
delay(100);
gotoxy(39, 13); cprintf(” “);
gotoxy(38, 13); cprintf(”R”);
delay(100);
gotoxy(38, 13); cprintf(” “);
gotoxy(37, 13); cprintf(”G”);
delay(100);
gotoxy(37, 13); cprintf(” “);
delay(100);
textcolor(15);
gotoxy(37, 13); cprintf(”G”);
delay(100);
textcolor(8);
gotoxy(37, 13); cprintf(”G”);
textcolor(15);
gotoxy(38, 13); cprintf(”R”);
delay(100);
textcolor(8);
gotoxy(38, 13); cprintf(”R”);
textcolor(15);
gotoxy(39, 13); cprintf(”O”);
delay(100);
textcolor(8);
gotoxy(39, 13); cprintf(”O”);
textcolor(15);
gotoxy(40, 13); cprintf(”U”);
delay(100);
textcolor(8);
gotoxy(40, 13); cprintf(”U”);
textcolor(15);
gotoxy(41, 13); cprintf(”P”);
delay(100);
textcolor(8);
gotoxy(41, 13); cprintf(”P”);
textcolor(15);
gotoxy(43, 13); cprintf(”2″);
delay(100);
textcolor(8);
gotoxy(43, 13); cprintf(”2″);
sleep(2);
clrscr();
textcolor(15);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
delay(100);
gotoxy(28, 13); printf(” “);
gotoxy(52, 13); printf(” “);
textcolor(15);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
delay(100);
gotoxy(29, 13); printf(” “);
gotoxy(51, 13); printf(” “);
textcolor(15);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
delay(100);
gotoxy(30, 13); printf(” “);
gotoxy(50, 13); printf(” “);
textcolor(15);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
delay(100);
gotoxy(32, 13); printf(” “);
gotoxy(49, 13); printf(” “);
textcolor(15);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
delay(100);
gotoxy(33, 13); printf(” “);
gotoxy(48, 13); printf(” “);
textcolor(15);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
delay(100);
gotoxy(34, 13); printf(” “);
gotoxy(47, 13); printf(” “);
textcolor(15);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
delay(100);
gotoxy(35, 13); printf(” “);
gotoxy(46, 13); printf(” “);
textcolor(15);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
delay(100);
gotoxy(36, 13); printf(” “);
gotoxy(45, 13); printf(” “);
textcolor(15);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
delay(100);
gotoxy(37, 13); printf(” “);
gotoxy(44, 13); printf(” “);
textcolor(15);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
delay(100);
gotoxy(38, 13); printf(” “);
gotoxy(43, 13); printf(” “);
textcolor(15);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
delay(100);
gotoxy(39, 13); printf(” “);
gotoxy(41, 13); printf(” “);
gotoxy(40, 13); cprintf(”N”);
delay(100);
gotoxy(40, 13); printf(” “);
textcolor(8);
gotoxy(40, 13); cprintf(”N”);
textcolor(15);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
delay(100);
textcolor(8);
gotoxy(39, 13); cprintf(”I”);
gotoxy(41, 13); cprintf(”G”);
textcolor(15);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
delay(100);
textcolor(8);
gotoxy(38, 13); cprintf(”L”);
gotoxy(43, 13); cprintf(”A”);
textcolor(15);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
delay(100);
textcolor(8);
gotoxy(37, 13); cprintf(”U”);
gotoxy(44, 13); cprintf(”L”);
textcolor(15);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
delay(100);
textcolor(8);
gotoxy(36, 13); cprintf(”D”);
gotoxy(45, 13); cprintf(”G”);
textcolor(15);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
delay(100);
textcolor(8);
gotoxy(35, 13); cprintf(”E”);
gotoxy(46, 13); cprintf(”O”);
textcolor(15);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
delay(100);
textcolor(8);
gotoxy(34, 13); cprintf(”H”);
gotoxy(47, 13); cprintf(”R”);
textcolor(15);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
delay(100);
textcolor(8);
gotoxy(33, 13); cprintf(”C”);
gotoxy(48, 13); cprintf(”I”);
textcolor(15);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
delay(100);
textcolor(8);
gotoxy(32, 13); cprintf(”S”);
gotoxy(49, 13); cprintf(”T”);
textcolor(15);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
delay(100);
textcolor(8);
gotoxy(30, 13); cprintf(”U”);
gotoxy(50, 13); cprintf(”H”);
textcolor(15);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
delay(100);
textcolor(8);
gotoxy(29, 13); cprintf(”P”);
gotoxy(51, 13); cprintf(”M”);
textcolor(15);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
delay(100);
textcolor(8);
gotoxy(28, 13); cprintf(”C”);
gotoxy(52, 13); cprintf(”S”);
sleep(2);

for(x=28; x<=52; x++)
{
gotoxy(x, 13); printf(” “);
delay(100);
}
clrscr();

menu_back:
textcolor(8);
gotoxy(1, 25); cprintf(”Best viewed in maximize window”);
gotoxy(76, 1); cprintf(”FCFS”);
gotoxy(1, 1); cprintf(”ÚÄÄÄÄÄÄÄ¿”);
gotoxy(1, 2); cprintf(”³  nter ³”);
gotoxy(1, 3); cprintf(”ÀÄÄÄÄÄÄÄÙ”);
gotoxy(69, 23); cprintf(”ÚÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(69, 24); cprintf(”³  redits ³”);
gotoxy(69, 25); cprintf(”ÀÄÄÄÄÄÄÄÄÄÙ”);
textcolor(15);
gotoxy(3, 2); cprintf(”E”);
gotoxy(71, 24); cprintf(”C”);
menu=getch();

if(menu==’E’ || menu==’e')
{
clrscr();
for(lo=28; lo<=53; lo++)
{
textcolor(8);
gotoxy(lo,14);cprintf(”ÛÛ”);
}

for(lo=0, di=28; lo<=100; lo+=4, di++)
{
textcolor(15);
gotoxy(28,12);cprintf(”initializing schedule…”);
gotoxy(28,13);cprintf(”%d%”, lo);
gotoxy(di,14);cprintf(”ÛÛ”);
delay(200);
}
sleep(2);
goto go;
}
else if(menu==’C’ || menu==’c')
goto credits;
else
goto menu_back;

go:
clrscr();
_setcursortype(_SOLIDCURSOR);
back:
textcolor(8);
gotoxy(16, 13); cprintf(”ow many processes would you like to put? (1-9)”);
textcolor(15);
gotoxy(15, 13); cprintf(”H”);
gotoxy(62, 13); cprintf(”:”);
gotoxy(64,13); cscanf(”%c”, &proc);
clrscr();

if(proc==’1′)
pro=1;
else if(proc==’2′)
pro=2;
else if(proc==’3′)
pro=3;
else if(proc==’4′)
pro=4;
else if(proc==’5′)
pro=5;
else if(proc==’6′)
pro=6;
else if(proc==’7′)
pro=7;
else if(proc==’8′)
pro=8;
else if(proc==’9′)
pro=9;
else
goto back;

_setcursortype(_SOLIDCURSOR);
textcolor(8);
gotoxy(28, 2); cprintf(”ÚÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(28, 3); cprintf(”³ Process ³            ³”);
gotoxy(28, 4); cprintf(”ÃÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(40, 3); cprintf(”Burst Time”);

for(x=1; x<=pro; x++)
{
textcolor(8);
gotoxy(28, y); cprintf(”³         ³            ³”);
gotoxy(28, y+1); cprintf(”ÃÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(32, y); cprintf(”P%d”, x);
y+=2;
}

textcolor(8);
gotoxy(28, y-1); cprintf(”ÀÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÙ”);

y=5;
limits:

do
{
textcolor(15);
gotoxy(44, y); cscanf(”%c”, &proc);

if(proc==’1′)
burst[burst2]=1;
else if(proc==’2′)
burst[burst2]=2;
else if(proc==’3′)
burst[burst2]=3;
else if(proc==’4′)
burst[burst2]=4;
else if(proc==’5′)
burst[burst2]=5;
else if(proc==’6′)
burst[burst2]=6;
else if(proc==’7′)
burst[burst2]=7;
else if(proc==’8′)
burst[burst2]=8;
else if(proc==’9′)
burst[burst2]=9;
else
{
textcolor(8);
gotoxy(44, y); printf(” “);
goto limits;
}

burst2++;
y+=2;
z++;
}
while(z<=pro);

_setcursortype(_NOCURSOR);
gotoxy(1, 25); cprintf(”P”);
gotoxy(26,25);cprintf(”…”);
textcolor(8);
gotoxy(40, 3); cprintf(”Burst Time”);
gotoxy(2, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

textcolor(8);
gotoxy(2, 1); cprintf(”he Gantt Char”);
textcolor(15);
gotoxy(1, 1); cprintf(”T”);
gotoxy(15, 1); cprintf(”t”);
textcolor(8);
gotoxy(1, 3); cprintf(”Ú”);
gotoxy(1, 4); cprintf(”³”);
gotoxy(1, 5); cprintf(”À”);

y=2;
for(x=1; x<=pro; x++)
{
textcolor(8);
gotoxy(y, 3); cprintf(”ÄÄÄÄÄÄ”);
gotoxy(y, 4); cprintf(”      ³”);
gotoxy(y, 5); cprintf(”ÄÄÄÄÄÄÁ”);
textcolor(15);
gotoxy(y+1, 4); cprintf(”P%d”, x);
y+=7;
}

textcolor(8);
gotoxy(y-1, 3); cprintf(”¿”);
gotoxy(y-1, 4); cprintf(”³”);
gotoxy(y-1, 5); cprintf(”Ù”);

y=1;
burst2=1;
for(x=1; x<=pro+1; x++)
{
textcolor(15);
gotoxy(y, 6); cprintf(”%g”, gantt);
gantt+=burst[burst2];
burst2++;
y+=7;
}

gotoxy(52, 25); cprintf(”P”);
gotoxy(77, 25);cprintf(”…”);
textcolor(8);
gotoxy(53, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

y=5;
gotoxy(20, 2); cprintf(”ÚÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿”);
gotoxy(20, 3); cprintf(”³     ³ Waiting Time ³ Turnaround Time ³”);
gotoxy(20, 4); cprintf(”ÃÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”);

for(x=1; x<=pro; x++)
{
textcolor(8);
gotoxy(20, y); cprintf(”³     ³              ³                 ³”);
gotoxy(20, y+1); cprintf(”ÃÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”);
textcolor(15);
gotoxy(22, y); cprintf(”P%d”, x);
y+=2;
}

textcolor(8);
gotoxy(20, y-1); cprintf(”ÀÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ”);

y=5;
gantt=0;
burst2=1;
for(x=1; x<=pro; x++)
{
textcolor(15);
gotoxy(28, y); cprintf(”%g – 0 = %g”, gantt, gantt);
gantt2+=gantt;
gantt+=burst[burst2];
burst2++;
y+=2;
}

y=5;
burst2=1;
gantt=burst[burst2];
for(x=1; x<=pro; x++)
{
textcolor(15);
gotoxy(44, y); cprintf(”%g – 0 = %g”, gantt, gantt);
gantt3+=gantt;
burst2++;
gantt+=burst[burst2];
y+=2;
}

gotoxy(1, 25); cprintf(”P”);
gotoxy(26,25);cprintf(”…”);
textcolor(8);
gotoxy(2, 25); cprintf(”ress any key to continue”);
getch();
clrscr();

textcolor(8);
gotoxy(2, 1); cprintf(”verage Waiting Tim”);
gotoxy(2, 5); cprintf(”verage Turnaround Tim”);
gotoxy(1, 3); cprintf(”(”);
textcolor(15);
gotoxy(1, 1); cprintf(”A”);
gotoxy(20, 1); cprintf(”e”);
gotoxy(1, 5); cprintf(”A”);
gotoxy(23, 5); cprintf(”e”);

gotoxy(1, 3); cprintf(”%g”, gantt2/pro);
gotoxy(1, 7); cprintf(”%g”, gantt3/pro);

gotoxy(56, 25); cprintf(”P”);
gotoxy(77,25);cprintf(”…”);
textcolor(8);
gotoxy(57, 25); cprintf(”ress any key to exit”);
getch();
goto end;

credits:
clrscr();
gotoxy(2, 1); cprintf(”rogrammer:”);
gotoxy(14, 1); cprintf(”Shahid Siddiuqe”);
gotoxy(2, 3);cprintf(”pecial thanks to: My friends”);
gotoxy(2, 25); cprintf(”ress any key to exit”);
textcolor(15);
gotoxy(1, 1); cprintf(”P”);
gotoxy(13, 1); cprintf(”E”);
gotoxy(20, 1); cprintf(”S”);
gotoxy(1, 3); cprintf(”S”);
gotoxy(20, 3); cprintf(”J”);
gotoxy(25, 3); cprintf(”J”);
gotoxy(1, 25); cprintf(”P”);
gotoxy(22, 25); cprintf(”…”);
getch();
clrscr();
goto menu_back;

end:
clrscr();
textcolor(4);
gotoxy(37,13);cprintf(”THE END!”);
return 0;
}