C++ Program for CPU scheduling algorithms

Using  C++ program for Implementatin of CPU scheduling algorithms
Please feel free to ask question about it and report the errors.please support by improving the program and share with us.thanx
  •  First Come First Serve
  •  Shortest Job First
  •  Round Robin
:: Dev c++ 4.9.9.2 used



#include<iostream>
#include<conio.h>
#include <windows.h>
using namespace std;
class schedule
{
private:
int no;
int i,j,swap;
int ttl,hover;
float awt;
int quan,sec;
int tmp[10];
int pwt[10];
int pwt2[10];
int pbt[10];

public:

schedule()
{
awt=0.00;
ttl=0;
pwt[0]=0;
}
//Defines gotoxy() for ANSI C compilers.
void gotoxy(short x, short y) {
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void getdata ()
{
cout<<"Enter number of jobs : ";
cin>>no;
for (i=0;i<no;i++)
{
cout<<"Enter Process Time of Job No."<<i+1<<" : ";
cin>>pbt[i];
}
}

void show ()
{
cout<<"Job"<<'\t'<<"Burst Time"<<'\t'<<"Waiting Time"<<endl;
for (i=0;i<no;i++)
cout<<i+1<<'\t'<<pbt[i]<<"\t\t"<<pwt[i]<<endl;
cout<<endl<<endl<<"Average Waiting Time : "<<awt<<endl;
getch();
}


void fsfc ()
{
getdata();

for (i=0;i<no;i++)
ttl=ttl+pbt[i];
for (i=1;i<no;i++)
pwt[i]=pwt[i-1]+pbt[i-1];
for (i=0;i<no;i++)
awt=awt+pwt[i];
awt=awt/no;
show();
}

void sjf ()
{
getdata();
for (i=0;i<no;i++)
tmp[i]=pbt[i];
for (j=no;j>0;j--)
for (i=0;i<j-1;i++)
{
if (tmp[i]>tmp[i+1])
{
swap=tmp[i];
tmp[i]=tmp[i+1];
tmp[i+1]=swap;
}
}
for (i=1;i<no;i++)
pwt[i]=pwt[i-1]+tmp[i-1];
for (i=0;i<no;i++)
ttl=ttl+pbt[i];
int k=0;
for(i=0;i<no;i++)
{
for(j=0;j<no;j++)
{
if(tmp[i]==pbt[j])
{
pwt2[j]=pwt[k];
k++;
}
}
}
for (i=0;i<no;i++)
pwt[i]=pwt2[i];
for (i=0;i<no;i++)
awt=awt+pwt[i];
awt=awt/no;
show();
}

void rr ()
{
getdata();
cout<<"Enter quantum number : ";
cin>>quan;

for (i=0;i<no;i++)
tmp[i]=pbt[i];

for (i=0;i<no;i++)
ttl=ttl+pbt[i];

hover=ttl;
sec=quan;
while(hover!= 0)
{
for(i=0;i<no;i++)
{
if(tmp[i]>0)
{
if(tmp[i]>quan)
{
tmp[i]=tmp[i]-quan;
hover=hover-quan;
sec=sec+quan;
}
else
{
pwt[i]=sec;
hover=hover-tmp[i];
sec=sec+tmp[i];
tmp[i]=0;
}
}
}
}
for (i=0;i<no;i++)
awt=awt+pwt[i];
awt=awt/no;
show();
}

void mainmenu()
{
gotoxy(30,5);
cout<<"CPU Scheduling Program";
gotoxy(36,9);
cout<<"Main Menu";
gotoxy(24,17);
cout<<"Press F for First Come First Serve.";
gotoxy(24,19);
cout<<"Press S for Shortest Job First.";
gotoxy(24,21);
cout<<"Press R for Round Robin.";
gotoxy(24,23);
cout<<"Press Esc to Exit from Program.";
}

void mainfunc()
{
char op;
while (op!=27)
{
system("cls");
mainmenu();
op=getch();
system("cls");
if (op=='f'||op=='F')
fsfc();
else if (op=='s'||op=='S')
sjf();
else if (op=='r'||op=='R')
rr();
}
}

};

int main ()
{
system("cls");
schedule cpu;
cpu.mainfunc();
getch();
return(0);
}

3 comments:

Anonymous said...

thanks pal!

umair khalid said...

its not working for me plzzz help...

Arians said...

umair khalid plz use turbo 3 for it. it to old make of OS Subject... thanx or write Error here

Post a Comment