Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <stdlib.h>
- using namespace std;
- struct procs
- {
- int pName =0;
- int arrTime = 0;
- int bTime = 0;
- int wTime=0;
- int tTime=0;
- int eTime=0;
- int sTime=0;
- int iTime=0;
- int prio=0;
- };
- void print_gantt_chart(procs P[], int numProc);
- int main(procs P[])
- {
- int numProc = 0;
- int choice = 0;
- top:
- cout<< "Welcome to CPU Scheduling Algorithm, please pick an option.\n[1]First Come First Serve\n[2]Shortest Job First\n[3]Priority"<<endl;
- cout<<"\nOption: ";
- cin>>choice;
- cin.ignore();
- while(!cin)
- {
- cout<<"Invalid Input, Please try again\n";
- cin.clear();
- cin.ignore();
- system("pause");
- system("cls");
- goto top;
- }
- if(choice==1)
- {
- cout<<"\nRedirecting to FCFS"<<endl;
- system("pause");
- system("cls");
- cout<<" ======================"<<endl;
- cout<<"|First Come First Serve|"<<endl;
- cout<<" ======================"<<endl;
- }
- else if(choice==2)
- {
- cout<<"\nRedirecting to SJF"<<endl;
- system("pause");
- system("cls");
- cout<<" =================="<<endl;
- cout<<"|Shortest Job First|"<<endl;
- cout<<" =================="<<endl;
- }
- else if(choice==3)
- {
- cout<<"\nRedirecting to Prio"<<endl;
- system("pause");
- system("cls");
- cout<<" ========"<<endl;
- cout<<"|Priority|"<<endl;
- cout<<" ========"<<endl;
- }
- else
- {
- cout<<"\nInvalid Input, Please try again"<<endl;
- system("pause");
- system("cls");
- goto top;
- }
- proc:
- cout << "\nEnter number of processes: ";
- cin>>numProc;
- while(!cin)
- {
- cout<<"Invalid\nEnter number of processes: ";
- cin.clear();
- cin.ignore();
- cin>>numProc;
- cin.ignore();
- }
- if (numProc<=10)
- {
- procs P[numProc];
- for (int i=0; i<numProc;i++)
- {
- P[i].pName = i+1;
- cout << "Process #: "<<P[i].pName<<endl;
- cout << "Arrival Time: ";
- cin >> P[i].arrTime;
- while(!cin)
- {
- cout<<"Invalid\nArrival Time: ";
- cin.clear();
- cin.ignore();
- cin>>P[i].arrTime;
- cin.ignore();
- }
- cout << "Burst Time: ";
- cin >> P[i].bTime;
- while(!cin)
- {
- cout<<"Invalid\nBurst Time: ";
- cin.clear();
- cin.ignore();
- cin>>P[i].bTime;
- cin.ignore();
- }
- if(choice==3)
- {
- cout<<"Priority: ";
- cin>>P[i].prio;
- while(!cin)
- {
- cout<<"Invalid\nPriority: ";
- cin.clear();
- cin.ignore();
- cin>>P[i].prio;
- cin.ignore();
- }
- }
- cout<<endl;
- }
- //FCFS Sorting
- for (int j=0; j<numProc-1;j++)
- {
- for (int k=0;k<numProc-j-1;k++)
- {
- if (P[k].arrTime>P[k+1].arrTime)
- {
- swap(P[k].arrTime,P[k+1].arrTime);
- swap(P[k].pName,P[k+1].pName);
- swap(P[k].bTime,P[k+1].bTime);
- swap(P[k].prio,P[k+1].prio);
- }
- }
- }
- //SJF Sorting
- if(choice==2)
- {
- for (int i=0; i<numProc-1;i++)
- {
- for (int j=1;i<numProc-j-1;j++)
- {
- if(P[j].bTime>=P[j+1].bTime)
- {
- swap(P[j].arrTime,P[j+1].arrTime);
- swap(P[j].pName,P[j+1].pName);
- swap(P[j].bTime,P[j+1].bTime);
- }
- }
- }
- }
- else if(choice==3)
- {
- //Prio Sorting
- int p;
- //Same Arrival Time Condition
- for (int i=0; i<numProc-1;i++)
- {
- for (int j=1;j<numProc;j++)
- {
- if(j!=i)
- {
- if(P[i].arrTime == P[j].arrTime)
- {
- p= true;
- }
- }
- }
- }
- if(p==true)
- {
- for (int i=0; i<numProc-1;i++)
- {
- for (int j=1;j<numProc-i-1;j++)
- {
- if(P[j].arrTime<=P[j-1].eTime&&P[j].prio>=P[j+1].prio)
- {
- swap(P[j].prio,P[j+1].prio);
- swap(P[j].arrTime,P[j+1].arrTime);
- swap(P[j].pName,P[j+1].pName);
- swap(P[j].bTime,P[j+1].bTime);
- }
- }
- }
- }
- //No same Arrival Time
- else
- {
- for (int i=0; i<numProc-1;i++)
- {
- for (int j=1;i<numProc-j-1;j++)
- {
- if(P[j].bTime>=P[j+1].bTime)
- {
- swap(P[j].arrTime,P[j+1].arrTime);
- swap(P[j].pName,P[j+1].pName);
- swap(P[j].bTime,P[j+1].bTime);
- swap(P[j].bTime,P[j+1].bTime);
- }
- }
- }
- }
- }
- //Idle Condition
- for (int i =0;i<numProc;i++)
- {
- if(i==0)
- {
- P[0].sTime = P[0].arrTime;
- P[0].eTime = P[0].arrTime + P[0].bTime;
- }
- else
- {
- P[i].sTime = P[i-1].eTime;
- if(P[i].arrTime>P[i].sTime)
- P[i].sTime = P[i].arrTime;
- P[i].eTime = P[i].sTime + P[i].bTime;
- }
- }
- if(choice==1||choice==2)
- {
- cout << "P#\t\tAT\t\tBT\t\tWT\t\tTT\t\t" <<endl;
- for (int l=0; l<numProc;l++)
- {
- cout << P[l].pName<<"\t\t"<<P[l].arrTime<<"\t\t"<<P[l].bTime<<"\t\t"<<P[l].sTime<<"\t\t"<<P[l].eTime<<endl;
- }
- }
- else if(choice==3)
- {
- cout << "P#\t\tAT\t\tBT\t\tWT\t\tTT\t\tP" <<endl;
- for (int j=0; j<numProc;j++)
- {
- cout << P[j].pName<<"\t\t"<<P[j].arrTime<<"\t\t"<<P[j].bTime<<"\t\t"<<P[j].sTime<<"\t\t"<<P[j].eTime<<"\t\t"<<P[j].prio<<endl;
- }
- }
- cout<< "\nGantt Chart" <<endl;
- print_gantt_chart(P,numProc);
- }
- else
- {
- cout <<"Invalid Input! must not be greater than 10"<<endl;
- goto proc;
- }
- system("pause");
- return 0;
- }
- void print_gantt_chart(procs P[], int numProc)
- {
- int i, j;
- cout <<" ";
- for(i=0; i<numProc; i++)
- {
- for(j=0; j<P[i].bTime; j++) cout <<"--";
- cout <<" ";
- }
- cout <<"\n|";
- for(i=0; i<numProc; i++) {
- for(j=0; j<P[i].bTime - 1; j++) cout <<" ";
- cout<<P[i].pName;
- for(j=0; j<P[i].bTime - 1; j++) cout<< " ";
- cout<<" |";
- }
- cout<<"\n ";
- for(i=0; i<numProc; i++) {
- for(j=0; j<P[i].bTime; j++) cout<<"--";
- cout<<" ";
- }
- cout<<"\n";
- int diffindex = P[0].sTime - 0;
- int diff = 0;
- int hold = 0;
- if (diffindex==0)
- cout<<"0";
- else
- cout<<"0"<<"[I]"<< 0+diffindex;
- for(i=0; i<numProc; i++)
- { hold=0;
- if (i<3)
- diff = P[i+1].sTime - P[i].eTime;
- if (diff!=0)
- {
- hold = 1;
- }
- if (hold==1)
- {
- for(j=0; j<P[i].bTime-2; j++) cout<<" ";
- cout<<P[i].eTime<< "[I]"<<P[i].eTime+diff;
- }
- else
- {
- for(j=0; j<P[i].bTime-1; j++) cout<<" ";
- cout<<P[i].eTime<<"";
- }
- }
- cout<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement