1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <time.h> /* time */
#include <stdlib.h> /* srand, rand */
#include <algorithm>
#include <queue>
using namespace std;
struct process
{
public:
int pid;
int arrival;
int burst;
int priority;
int status;
};
bool sortbyarrivel(const process & s1, const process & s2)
{
if (s1.arrival != s2.arrival)
return s1.arrival < s2.arrival;
}
bool sortbypriority(const process & s1, const process & s2)
{
if (s1.priority == s2.priority) {
if (s1.arrival > s2.arrival ){
return s1.priority < s2.priority;
}
}
else if (s1.priority != s2.priority) {
return s1.priority < s2.priority;
}
}
int main() {
process prs;
vector <process> pr;
vector <process> r1;
ifstream infile("inputfile.txt"); // ifstream is used to read the input file
string line,btime,numofprocesses,prid,arraiveltime;
int n;
int current_line = 0;
int randNum;
while (getline(infile, line)) //get the lines inside the file, one by one until the end of the file
{
istringstream iss(line); //create string stream and read current line
if ( current_line ==0){ // first line always contains the number of processes
if (iss >> numofprocesses){ // store the contents of the stream into numofprocesses
n =atoi(numofprocesses.c_str());
cout<< "number of processes = "<<numofprocesses<<endl<<endl;
}
}
else {
if (iss >> prid >>arraiveltime >>btime) { // store the contents of the stream into pid,arraiveltime
prs.pid = atoi(prid.c_str());
prs.arrival = atoi(arraiveltime.c_str());
if (btime == "green"){ // if btime , which is the priority type is == regular
//srand (time(NULL));
//prs.burst = (rand()%(15-10))+ 10; // give this process a random burst time from 10 to 15
prs.burst = 1;
prs.priority = 2; // give this process priority = 2 if its regular
}
else if(btime == "yellow"){
// srand (time(NULL));
//prs.burst = (rand()%(30-20))+ 20;
prs.burst = 2;
prs.priority = 1;
}
else if(btime == "red"){
// srand (time(NULL));
//prs.burst = (rand()%(60-40))+ 40;
prs.burst = 3;
prs.priority = 0;
}
prs.status =0;
pr.push_back(prs);
}
}
current_line++; // next line ++1
}
//sorting by arrivel time
sort(pr.begin(), pr.end(), sortbyarrivel);
cout<<endl; cout<<endl; cout<<endl;
cout<<"The patients are scheduled as follow."<<endl;
cout<<"In physician room:"<<endl;
cout<<"Time "<< pr[0].arrival<<" : patient "<<pr[0].pid<<" is coming"<<endl;
cout<<"Time "<< pr[0].burst<<" : patient "<<pr[0].pid<<" is leaving"<<endl;
static int currentbursttime=pr[0].burst;
pr[0].status =1;
int complated_process = 0 ;
while (complated_process <pr.size()){
for(int i=0;i<pr.size();i++){
if( pr[i].arrival <= currentbursttime && pr[i].status ==0 ){
r1.push_back(pr[i]);
}
}
//sorting by priority
sort(r1.begin(), r1.end(), sortbypriority);
for(int i=0;i<r1.size();i++){
if (i == 0){
currentbursttime = currentbursttime+1;
cout<<"At Time "<< currentbursttime<<" : process "<<r1[i].pid<<" is entring"<<endl;
currentbursttime =r1[i].burst+currentbursttime;
cout<<"At Time "<< currentbursttime<<" : process "<<r1[i].pid<<" is going"<<endl;
pr[r1[i].pid].status =1;
complated_process = complated_process +1;
}
}
r1.clear();
}
}
|