Non-Preemptive Priority Scheduling problem !?

my input file text :

1
2
3
4
5
6
7
6
1 0 yellow
2 1 red
3 1 green
4 2 urgent
5 3 green
6 4 red




my code until now:

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(); 


    }

}



this program until now is giving me this results:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:


1
2
3
4
5
6
7
8
9
10
11
12
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



hope someone can look at this and show me what I did wrong
thanks,

Last edited on
Well the first thing I see is that both sortbyarrival() and sortbypriority() can return garbage. For example, in sortbyarrival(), if s1.arrival == s2.arrival then no return statement executes and the return value will whatever happens to be in the appropriate memory location or register.

Line 32 looks wrong. Line 30 guarantees that it will always return false.

I fixed sortbyarrival() and changes sortbypriority so it sorts by prioirty, or arrival time if priority is equal. After making those changes, the output is:
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 2 is entring
At Time 10 : process 2 is going
At Time 11 : process 2 is entring
At Time 14 : process 2 is going
At Time 15 : process 2 is entring
At Time 18 : process 2 is going
At Time 19 : process 2 is entring
At Time 22 : process 2 is going
At Time 23 : process 2 is entring
At Time 26 : process 2 is going

Topic archived. No new replies allowed.