input file text :
Code:
6
1 0 yellow
2 1 red
3 1 green
4 2 urgent
5 3 green
6 4 red
my program until now:
Code:
#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 count = 0 ;
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]);
count++;
}
}
//sorting by priority
sort(r1.begin(), r1.end(), sortbypriority);
for(int i=0;i<count;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();
}
return 0 ;
}
this program until now is giving me this results:
Code:
number of processes = 6
The patients are scheduled as follow.
In physician room:
Time 0 : patient 1 is coming
Time 2 : patient 1 is leaving
At Time 3 : process 2 is entring
At Time 6 : process 2 is going
At Time 7 : process 6 is entring
At Time 10 : process 6 is going
At Time 11 : process 6 is entring
At Time 14 : process 6 is going
At Time 15 : process 6 is entring
At Time 18 : process 6 is going
At Time 19 : process 6 is entring
At Time 22 : process 6 is going
At Time 23 : process 6 is entring
At Time 26 : process 6 is going
and the result is wrong , its repeating process #6 over and over again .. the result I want is something like this:
Code:
At Time 0 : process 1 is entring
At Time 2 : process 1 is going
At Time 3 : process 2 is entring
At Time 6 : process 2 is going
At Time 7 : process 6 is entring
At Time 10 : process 6 is going
At Time 11 : process 4 is entring
At Time 13: process 4 is going
At Time 14: process 3 is entring
At Time 15: process 3 is going
At Time 16: process 5 is entring
At Time 17 : process 5 is going
The problem has to do with the loops inside while loop. Tried to find a solution but I couldn't.
Hope someone can look at this and show me what I did wrong.